Question

I can't figure out why this works for 90% of the inputs, but not the others. It is meant to tell you how many coins you would get back in change. Most test amounts work fine, but if you enter 4.20 (or $4.20), it returns 23 coins... it should be 18 coins (16 quarters and 2 nickels). Where is the bug? Here is my code:

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

int main(void){

    float change = 0.00;

    printf("How much change is owed? ");
    change = GetFloat();

    float quarters = change/.25;
    change-= (int)quarters*.25;

    float dimes = change/.10;
    change-= (int)dimes*.10;

    float nickels = change/.05;
    change-= (int)nickels*.05;

    float pennies = (change+.005)/.01;
    change-=(int)pennies*.01;

    int total = (int)quarters+(int)dimes+(int)nickels+(int)pennies;

    printf("%d\n", total);

    return 0;
    }
Was it helpful?

Solution

Throw out the floating-point calculations. this is all based on hundredths at best, so just use integer division/modulo. Never rely on perfect accuracy in floating point numbers.

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

int main(void){

    float fchange = 0.00;
    int change = 0;

    printf("How much change is owed? ");
    fchange = GetFloat();

    change = (int)roundf(fchange*100.0);

    int quarters = change/25;
    change = change % 25;

    int dimes = change/10;
    change = change % 10;

    int nickels = change/5;
    change = change % 5;

    printf("%d quarters, %d dimes, %d nickels, %d pennies\n", quarters, dimes, nickels, change);

    return 0;
}

OTHER TIPS

The closest float value to 4.20 is slightly smaller than that (4.19999980926513671875, for the usual 32-bit IEEE754 floats). So after you subtracted the $4 from the 16 quarters, you have an amount left that is slightly smaller than 0.2. Dividing that by 0.1 results in a value slightly smaller than 2, so your nickels value is 1. The same happens then after you subtract your nickel, the value is slightly smaller than 0.1, dividing by 0.05 results in a quotient slightly smaller than 2.

You should use integers only for such a computation, calculating in cents.

The other answers have it mostly covered: you should be working with fixed point here, not floating point. Be careful to round properly when going from the floating point input to your fixed point representation, though. Here is a short version I hacked up, which should work for all positive inputs:

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
    float change = atof(argv[1]);
    int work = (int)(100*change+0.5);
    int quarters, dimes, nickels, pennies;
    quarters = work/25; work %= 25;
    dimes    = work/10; work %= 10;
    nickels  = work/5;  work %=  5;
    pennies  = work;
    printf("%.2f dollars = %d quarters, %d dimes, %d nickels and %d pennies: %d coins total\n",
    change, quarters, dimes, nickels, pennies, quarters+dimes+nickels+pennies);
    return 0;
 }

For example:

./change 4.20
4.20 dollars = 16 quarters, 2 dimes, 0 nickels and 0 pennies: 18 coins total
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top