coder newbie here. As far as I understand, this should work. This is the logic of my code...

I get the float input from the user, and keep asking until I receive a positive number.

I change this into a rounded interger.

For each of the coin values, I check the amount is divisible then take this amount away, repetitively.

Then I print the total number of my coins. However I end up with a bunch of errors. Help?

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

int main(void)
    float amount;

    amount = get_float ("Change requested: ");

while (amount < 0);

    int rounded = roundf(amount*100);
    //check amount of quarters
    for (rounded >= 0.25; rounded = rounded - 0.25; rounded++)

        //check amount of dimes
        for (rounded >= 0.10; rounded = rounded - 0.10; rounded++) 

            //check amount of nickles
           for (rounded >= 0.5; rounded = rounded - 0.05; rounded++)
           //check amount of pennies
          for (rounded >= 0.01; rounded = rounded - 0.01; rounded++)
//print toal number of coins
                printf("%i\n", rounded);

Since you multiplied amount by 100, wouldn't you need to multiply the dollar value of the coins by 100 too?

I'm assuming that this is only partial code, since the for loops don't really have any code blocks associated with them.

I can't see any reason for nesting the for loops. The greedy algorithm says to count the maximum quarters first, then count dimes, nickels and pennies. You need to count the max number of one type of coin before going on to the next. Nesting the loops will merge the processing of different coin denominations and most likely break.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


Firstly you should not write 0.___ because you already changed the number to a whole number so just write 25, 10, etc. Secondly, your "for" condition isn't looking the best so you might want to change that like - for {


You must log in to answer this question.

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