question to cin on Floating Point values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
float b/*Monies*/,s/*Sum*/=0;
while (b!=0){
          cout <<"Enter monies:\t";
          cin>>b;
          if (b==0)b=0;
          else if (b!=.05 && b!=.1 && b!=.2 && b!=.5 && b!=1&& b!=2 && b!=5 && b!=10 && b!=20 && b!=50){
             cout <<"Monies bill not recognized. Make right choice:"
                  <<endl<<"\t5 Cent (0.05 Euro)"
                  <<endl<<"\t10 Cent (0.10 Euro)"
                  <<endl<<"\t20 Cent (0.20 Euro)"
                  <<endl<<"\t50 Cent (0.50 Euro)"
                  <<endl<<"\t1 Euro"
                  <<endl<<"\t2 Euro"
                  <<endl<<"\t5 Euro"
                  <<endl<<"\t10 Euro"
                  <<endl<<"\t20 Euro"
                  <<endl<<"\t50 Euro\n";
                  }
          else{
               s+=b;
               cout<<"Sum:\t"<<s<<endl;
               }          
          }

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?
Last edited on
use logical or ( || ) operator instead of and ( && )
Last edited on
nonsense.
I really don't like your attitude of calling suggestions made by other users to help you nonsense.
No one will help you that way.
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.
Last edited on
on second thought.. you comparing floats to doubles. ".1" is a double and b is a float. Try adding "f" behind all the constants in line 6.
Instead of floating points, use a double.
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!
Last edited on
EDIT: Using double solved the problem. Thanks a lot guys!


It alleviated the symptoms, but it did not solve problem.

Your fundamental problem is that you are using absolute (in)equality with floats/doubles. Doing so is flawed and error prone right from the start.
Last edited on
Topic archived. No new replies allowed.