Not getting desired output

B

Brian

So I have this:

double x = .25;
double y = .5;
double z = .25;
double probability;
int index;

while(index < 30)
{
probability = (1/2)(y/(y+z));
index = index++;
cout << index << " " << probability << endl;
}

return 0;
}

I am getting 0 for probability for some reason. Considering x,z = .25
and y = .5 I dont see how I am getting a 0.
 
A

Alf P. Steinbach

* Brian:
So I have this:

double x = .25;
double y = .5;
double z = .25;
double probability;
int index;

while(index < 30)
{
probability = (1/2)(y/(y+z));
index = index++;
cout << index << " " << probability << endl;
}

return 0;
}

I am getting 0 for probability for some reason. Considering x,z = .25
and y = .5 I dont see how I am getting a 0.

1. Error. You're using an uninitialized variable 'index'. Most likely
your compiler is issuing a warning about that. The effect is Undefined
Behavior.

2. Style. 'probability' should be declared locally in the loop and it
should be 'const'.

3. Error (logical). The expression 1/2 evaluates to zero.

4. Error (syntax). Lacking '*'. C++ does not support implicit
multiplication.

5. Error (syntax). Unmatched parenthesis.

6. Error. You modify the variable 'index' twice with no intervening
sequence point, yielding Undefined Behavior. Replace with '++index'.

7. Style. The loop should be a 'for' loop.

8. Style. Don't write '.25', write '0.25'.

9. Style. Don't write 'cout', write 'std::cout' and get rid of any
'using namespace std'.

10. Style. Don't write everything at the same level of indentation, let
indentation reflect the logical structure of the code (or perhaps
investigate how to post news articles, if it's the posting interface
that has removed indentation).

11. Error. You have posted something that isn't your actual code,
something that doesn't compile and hence can not produce the result you
claim, i.e., you're effectively lying to the group (perhaps
unintentional, but still it's lying). See the FAQ on how to to post.

Hth.,

- Alf
 
M

Marco Wahl

Brian said:
So I have this:

double x = .25;
double y = .5;
double z = .25;
double probability;
int index;

while(index < 30)
{
probability = (1/2)(y/(y+z));
index = index++;
cout << index << " " << probability << endl;
}

return 0;
}

I am getting 0 for probability for some reason. Considering x,z = .25
and y = .5 I dont see how I am getting a 0.

I slightly modified and commented your code and tried to compile it on
my computer.

The program is

[[[
#include <iostream>
using namespace std;
int main()
{
double x = .25;
double y = .5;
double z = .25;
double probability; // Will this variable be initialized?
int index; // Will this variable be initialized?
while(index < 30)
{
probability = (1/2)(y/(y+z)); // 0(y/(y+z)) really wanted? ;-)
index = index++; // Redundance or even danger here?
cout << index << " " << probability << endl;
}
return 0;
}
]]]

and the compiler-run gives

[[[
g++ -c a.cpp -o a.o
a.cpp: In function ‘int main()’:
a.cpp:12: error: ‘0’ cannot be used as a function
]]]

I just saw Alfs post which looks good. Hopefully my posting is of
some use anyway.
 
J

Jack Klein

So I have this:

double x = .25;
double y = .5;
double z = .25;
double probability;
int index;

while(index < 30)
{
probability = (1/2)(y/(y+z));
^^^^^

Aside from the other two posts, you are always going to get a result
of 0. The '1' and '2' in the expression "1/2" are integer literals.
That means that the program will perform integer division, which
discards any remainder. In integer division, 1/2 results in 0.

The type of an arithmetic operation in C++ depends completely on the
types of its operands. Regardless of what you might want to do with
the result of the operation afterwards.

Change this to 1.0/2.0, or even better rewrite the right hand side of
the assignment as:

(y/(y+z))/2;

Note in this case it is not necessary to write 2.0. The subexpression
(y/(y+x)) has type double, and this will cause automatic promotion of
the int value 2 to the double value 2.0.

index = index++;
cout << index << " " << probability << endl;
}

return 0;
}

I am getting 0 for probability for some reason. Considering x,z = .25
and y = .5 I dont see how I am getting a 0.

....and fix all the things that Alf pointed out.
 
B

Brian

9. Style. Don't write 'cout', write 'std::cout' and get rid of any
'using namespace std'.
Why is this necessary? I've been writting cout for every program I've
written and haven't run into any problems. Can you explain why this is
better than just cout?
11. Error. You have posted something that isn't your actual code,
something that doesn't compile and hence can not produce the result you
claim, i.e., you're effectively lying to the group (perhaps
unintentional, but still it's lying). See the FAQ on how to to post.
My appologies I was trying to keep it short and to the point.

I understand all the other comments, thanks for the help guys.
 
O

osmium

Brian said:
Why is this necessary? I've been writting cout for every program I've
written and haven't run into any problems. Can you explain why this is
better than just cout?

Because someday you may be a programmer in a project that has 1,000
programmers and the main application is 100 MB and it incorporates 57
libraries of various sorts picked up from around the world. And you are too
damned dense to realize that the environment has changed and you won't adapt
properly and can't even be re-trained. Can't you SEE that?
 
T

TB

Brian sade:
Why is this necessary? I've been writting cout for every program I've
written and haven't run into any problems. Can you explain why this is
better than just cout?

'using namespace std' causes heavy pollution in the global* namespace.
If you don't want to write 'std::cout', then import only the necessary
names, e.g.:

using std::cout;

Namespaces exists for a purpose. But of course, in the end, it's
your decision. As he wrote, it's about style.

*Actually, the statement pollutes any namespace that contains it.
 
B

Brian

Because someday you may be a programmer in a project that has 1,000
programmers and the main application is 100 MB and it incorporates 57
libraries of various sorts picked up from around the world. And you are too
damned dense to realize that the environment has changed and you won't adapt
properly and can't even be re-trained. Can't you SEE that?

I do see that. I will change all my couts to std::cout then. Thank you.
 
J

Jim Langston

Brian said:
Why is this necessary? I've been writting cout for every program I've
written and haven't run into any problems. Can you explain why this is
better than just cout?

Namespaces were designed into C++ to fix the problem of name collision. Say
you decided to make a class called string (not neccessarily a good idea, but
just to show my point). Something like this (the #include of <cmath> is
just so it'll compile with the using statement).

#include <cmath>
using namespace std;
class string
{
public:
char data[1000];
};

int main()
{
string MyString;
}

Seems simple enough, and compiles. Now, however, say I want a vector of my
string class. I would attempt to do this:

#include <vector>
using namespace std;
class string
{
public:
char data[1000];
};

int main()
{
vector< string > MyVector;
}

But, it doesn't compile (in VC++ .net 2003 anyway). The error I get is
this:

c:\Source\working\Console2\Console2.cpp(14) : error C2872: 'string' :
ambiguous symbol
could be 'c:\Source\working\Console2\Console2.cpp(6) : string'
or 'c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xstring(1562) : std::string'

That's a name collision. <vector> also includes <string> somewhere, which
is the std::string. Now the compiler doesn't know which vector< string >
I'm talking about. Is it std::string or my local string which is in the
unnamed namespace? To fix it I would have to:

vector< ::string > MyVector;

saying use the string from the unnamed namespace. But, to get rid of all
this finagelling, just don't import every single thing from the std it finds
and either just import the ones you want to use or prefix them with std::.
Personally, I prefix everything with std::, it's not really that difficult.
Which makes the program become:

#include <vector>

class string
{
public:
char data[1000];
};

int main()
{
std::vector< string > MyVector;
}

Please note, I am not suggesting in the least that you make classes with the
same name as templates/classes in the STL. I just used string cause it was
easiest to show.

Namespaces were designed to avoid name collision. using namespace whatever
totally defeats the purpose.
 
D

Daniel T.

"Jim Langston said:
Brian said:
Why is this necessary? I've been writting cout for every program I've
written and haven't run into any problems. Can you explain why this is
better than just cout?

Namespaces were designed into C++ to fix the problem of name collision. Say
you decided to make a class called string (not neccessarily a good idea, but
just to show my point). Something like this (the #include of <cmath> is
just so it'll compile with the using statement).

#include <cmath>
using namespace std;
class string
{
public:
char data[1000];
};

int main()
{
string MyString;
}

Seems simple enough, and compiles. Now, however, say I want a vector of my
string class. I would attempt to do this:

#include <vector>
using namespace std;
class string
{
public:
char data[1000];
};

int main()
{
vector< string > MyVector;
}

But, it doesn't compile (in VC++ .net 2003 anyway). The error I get is
this:

c:\Source\working\Console2\Console2.cpp(14) : error C2872: 'string' :
ambiguous symbol
could be 'c:\Source\working\Console2\Console2.cpp(6) : string'
or 'c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xstring(1562) : std::string'

That's a name collision. <vector> also includes <string> somewhere, which
is the std::string. Now the compiler doesn't know which vector< string >
I'm talking about.

And in that rare instance, you replace the "using namespace std", with
"using std::vector" and all is once again well with the world...

What's the big deal? If YAGNI, then don't bother. However, *never* put
using namespace std in the global scope of a header file, fixing it
becomes much more complicated...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top