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

int main(void)
{
    // Get float for cents
    // Take inputs of positive floats and repromt user for negative
    float dollars;
    do
    {
        dollars = get_float("Change owed: ");

    }
    while (dollars < 0);

    // divide appropriate amount of cents from change owed
    // take the remainder of previous amount & divide w/ new number

    int cents;

     cents = round(dollars * 100);

     int coins = 1;

     while (cents >= 25)
     {
         cents = cents/25;
         coins ++;
     }
      while (cents >= 10)
     {
         cents %= cents/10;
         coins ++;
     }
      while (cents >= 5)
     {
         cents %= cents/5;
         coins ++;
     }
     while (cents >= 1)
     {
         cents %= cents/1;
         coins ++;
     }
     // print result of all coins being added
     printf("%i\n", coins);
}

What is wrong with my code? For example, when I type 0.75, ill get a result of 3 coins. When I type in 0.50, ill get the same result of 3 coins. Why is this? How can I fix this problem?

0

1 Answer 1

2

Try printing out the value of cents after each calculation and think about the effect of integer division vs. regular division.

Does the code track BOTH the number of coins for each denomination AND the change left over?

Also, why is coins initialized to 1 and not 0?

You must log in to answer this question.

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