-1
printf("Insert money for payment: ");

while( !(scanf("%g\n", &insertMoney) == EOF || !insertMoney) ){

    if(insertMoney == 0.05f ||insertMoney == 100 || insertMoney == 50 || insertMoney == 20 || insertMoney == 10 || insertMoney == 5 || insertMoney == 2 || insertMoney == 1 || insertMoney == 0.50 || insertMoney == 0.2 || insertMoney == 0.20 || insertMoney == 0.1 || insertMoney == 0.02 || insertMoney == 0.01 || insertMoney == 0 )           
        totalSum += insertMoney;
    else{
        fake = insertMoney;
        printf("%g is invalid!\n", fake);
        return 1;       
        }   
     }

If i insert 100, 50 or any number from if, works normaly. If i insert 0.20 or 0.05 (any number with 0.xx) say me is invalid.

PS: insertMoney is float.

l1den
  • 89
  • 1
  • 10
  • 1
    This question [Why not use Double or Float to represent currency?](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) may be of interest to you. – Weather Vane Nov 12 '16 at 16:00
  • 1
    Read documentation of [scanf](http://en.cppreference.com/w/c/io/fscanf) and of every function you are using. Read also http://floating-point-gui.de/ – Basile Starynkevitch Nov 12 '16 at 16:01
  • 1
    Hmm, `scanf("%g\n", &insertMoney) == EOF` ??? The return value of scanf here can only be 0 or 1. What are you trying to do here? – Serge Ballesta Nov 12 '16 at 16:02
  • Sorry guys, but still understand :( – l1den Nov 12 '16 at 16:11
  • 1
    Another link: [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken). Please note that `0.1` is not *exactly representable* in floating point format. So if you multiply that by `10` it *does not equal `1.0`*. – Weather Vane Nov 12 '16 at 16:15
  • I understand where is the problem, but how can resolve it? Thanks – l1den Nov 12 '16 at 16:27
  • By using integer types: work in cents. – Weather Vane Nov 12 '16 at 16:49
  • @SergeBallesta: I don't see why `scanf()` can't return EOF in contex, though I agree that it could return 0 or 1 too, and the program should only continue when the return value is 1. The `if (!(condition_1 || !condition_2))` notation is decidedly more complex than it should be, too. – Jonathan Leffler Nov 12 '16 at 17:03

1 Answers1

0
int fequal(float a, float b)
{
    float epsilon = 0.000000001;

    if (fabs(a - b) < epsilon)
        return 1;

    return 0;
}


fequal(0.5, insertMoney)

this is the answer :)

l1den
  • 89
  • 1
  • 10