When i input anything smaller than .5 the else if statement becomes untrue. So input like .1 .2 and .05 is not recognized.
1. Question: Why?
2. Question: How can i fix this?
Also, This is supposed to be done with a switch case statement, but i can't do a switch on a floating point variable. How can i solve this?
If other users are not able to read a question properly and give answers accordingly, I don't think it's belligerent to criticize their answers.
The first answer is given by someone who did some coding, has had problems with logical expressions and solved them by implementing the opposite operator. Thus his code worked, but the problem I am facing here has nothing to do with the mixing up of logical operators.
It is a problem of input not getting recognized the way it should.
You can see my first question, right?
So i get an answer that disregards that q completely.
Then i get the "how" which is a phrase missing punctuation and personal pronouns.
In what way is that answer not nonsense?
Where is the problem? Isn't it right when the else if statement (line 6) becomes not true and the else branch (line 19) is executed for input of e.g. 0.1?
Oh, and what bluezor said. :-|
By the way.. comparing floating points with == or != is very tricky in C++ (and most other programming languages). Floating points are not stored in an accurate way, so be aware what is possible and what not.
1 2 3 4 5
float f = 10.0/2.0
if (f != 0.5)
{
cout << "SUPRISE!";
}
Ciao, Imi.
PS: b is uninitialized and may be 0. Then your while loop never execute at all.
PPS: line 5 is nonsense! ;-)
PPPS: It may be nonsense, but beeing polite never hurts. "nonsense." is considered not polite in many parts of the world.
Thanks for the tips. I'll report back if it worked.
PS: If I leave out line 5, then I get the output following line 6 when I input 0. I didn't know any better way to prevent this.
EDIT: Using double solved the problem. Thanks a lot guys!