2

im getting the error for 41 cents. The answer I get when i run the program is three coins i cannot figure out why. Can someone let me know if i did something wrong here?

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

#define QUARTER 25;
#define DIME 10;
#define NICKEL 5;


int main(void)
{
    float change= 0;
    int cent_amount=0;
    int quarter_amount=0;
    int dime_amount=0;
    int nickel_amount=0;
    int leftover_amount=0;
    int coin_amount=0;

    do {
    printf("How much change needed? ");
    change = GetFloat();
    if (change == 0 || change <= 0)
    printf ("Number should be greater than 0!\n");
    }
while (change < 0);

cent_amount= (int)(change*100);

quarter_amount= cent_amount / QUARTER;
leftover_amount= cent_amount % QUARTER;

dime_amount= leftover_amount/ DIME;
leftover_amount= cent_amount % DIME;

nickel_amount= leftover_amount/ NICKEL;
leftover_amount= cent_amount % NICKEL;

coin_amount= quarter_amount + dime_amount + nickel_amount + leftover_amount;

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

return 0;
}

1 Answer 1

2

You're using cent_amount again and again, without updating. I think you meant to use something else instead at those places I've marked below:

quarter_amount= cent_amount / QUARTER;
leftover_amount= cent_amount % QUARTER;

dime_amount= leftover_amount/ DIME;
leftover_amount= cent_amount % DIME;

nickel_amount= leftover_amount/ NICKEL;
leftover_amount= cent_amount % NICKEL;

EDIT: Then you'll also have a problem with float imprecision, which will need the round() function. But let's leave that for another question.

1
  • 1
    Thank you!. Now it works
    – Mare
    Commented Oct 29, 2016 at 19:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .