-4

Can't figure out error in my code when 4.2 is input as change will be then 420 but instead is coming out to be 419. What is wrong in my code?

#include<stdio.h>
#include<cs50.h>

int evaluate(int change){

    int quarter,dime,nickel,penny;
    int coins = 0;

    quarter = change/25;
    change = change%25;
    printf("quarters:%d\n" ,quarter);
    printf("change left: %d\n",change);
    dime = change/10;
    change = change%10;

    printf("dimes:%d\n" ,dime);
    printf("change left: %d\n",change);
    nickel = change/5;
    change = change%5;

    printf("nickels:%d\n" ,nickel);
    printf("change left: %d\n",change);
    penny = change;
    coins = quarter+dime+nickel+penny;

    printf("pennies:%d\n" ,penny);
    printf("change left: %d\n",change);
    return coins;
}


int main()
{
    printf("How much:");
    float input = GetFloat();
    int coin = 0;

    while(true)
    {
        if(input>0)
        {
            int change = (100*input);
                printf("change: %d\n",change);

            coin = evaluate(change);

            printf("%d\n",coin);
            return 0;
        }    

        else
        {
            printf("are you serious? retry:");
            input = GetFloat();
    }    }
}

The program is to evaluate the least number of coins a cashier has to give to customer. Problem is that on getting input 4.2, the output is coming out wrong; also change=100*input is coming out to be 419 for input to be 4.2. Please help me figure out problem.

Code on left side and output on right.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
4rshdeep
  • 490
  • 4
  • 11
  • 4
    _Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example._ – Sourav Ghosh Dec 07 '16 at 12:41
  • Instead of posting link to an image, post your code here. – Yousaf Dec 07 '16 at 12:42
  • Post your code as code, not an image. The folks you want to have help will not waste time with this question. – Neo Dec 07 '16 at 12:42
  • Have you tried rounding 100.0 times input? I am guessing right now your code is truncating 419.99 something. – Jeremy Kahan Dec 07 '16 at 12:53
  • Teaching you the best way to ask your questions ("don't post code as an image", "use a relevant title") *is* helpful, and the people you sneer at for "complaining" *are* attempting to help you. – Jongware Dec 07 '16 at 14:33

1 Answers1

2

4.2 cannot be expressed exactly with the binary floating point format used on you platform. When you multiply it by 100, it gives a number that is just slightly smaller than 420. Converting it to an integer yields 419.

You can prevent this by rounding the number prior to conversion with:

int n = round(number);

Or by adding a rounding adjustment:

int n = (int)(number + 0.5);

Do not use float for this computation, use a properly rounded amount of cents as explained on this answer: Integer overflow in greedy coin counting.

Community
  • 1
  • 1
chqrlie
  • 131,814
  • 10
  • 121
  • 189