简体   繁体   中英

script in C is not adding Floats correctly

This is my first time posting here so this post might be a little bit of a mess but I'll try my best to explain everything.

I have to make a program that acts kinda like Self-checkout in a store. However i run into two issues when people are inserting money into the machine. People insert money like this: 10 20 50 0.10 .... and the paying ends with 0 or by using ctrl+d. (people can only use coins of this value: 100, 50, 20, 10, 5, 2, 1, 0.50, 0.20, 0.10, 0.05, 0.02 a 0.01 as you can see in the code)

Well when I end the payment with 0 program exits normally.(like this: 50 20 0)

However when I end it with ctrl+d it causes infinite loop and i don't understand why.

And the second issue is that for some reason it won't add numbers like 0.10, 0.20 and so on. Any ideas how to fix this or what might be causing the error?

And dont mind the printf in the middle that was just me checking the value.

float bill,x,payment=0,k=0;



printf("coins ");
while(k==0)
{
    scanf("%f", &x);
    if(x==0 )
    {
        goto END;
    }

    if(x ==100 || x ==50 || x ==20 || x ==10 || x ==5 || x ==2 || x ==1 || x ==0.50 || x==0.10 || x ==0.20 || x ==0.05 || x ==0.02|| x ==0.01 )
        {   
            payment += x;
            printf("==============");
            printf("%.2f  \n",payment);
        }

    else{
        printf("%.2f is invalid",x);
        k = 1;      
        goto END2;
    }
}
END:
printf("%.2f  \n", payment);

END2:
return 0;

When you send Ctrl+D in your terminal, you register an EOF character to stdin , therefore scanf won't read anything anymore, and x will never be set to 0 , so you won't escape your loop.

You would need to check the result of scanf , reading man scanf you can see that you are expecting a return value > 0 in case of success.

if (scanf("%f", &x) < 1 || x = 0) // Check return value of scanf, then check x
    GOTO: END;

Edit: Note that if scanf was reading input items, you would need to check that the return value of scanf is not less that the number of input items.

Fixing this answer based on hyde's and alamit's comments

The reason your program "refuses" to add small numbers like 0.10 is because your variable x and the decimal literals like 0.10 you are comparing it to have different precision .

So, to fix the problem, add f to the values x == 0.10f , or switch to using double and reading and printing with %lf

If you want to know the math behind the problem, read this .

When making comparisons with floating point numbers, one must be careful. Floating point representation of 0.1 is different between 32 bits and 64 bits( float vs double ). You have defined x to be float which is compared with 0.1(which is double ). So the comparison will not work correctly. Either you must define x as double and change nothing else in your code or, make x == 0.1 to x == 0.1f

Further information about this topic can be found this answer and this topic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM