# Greedy returns zero

I've finished my greedy code and everything works, I passed the cs50 check and greedy compiles for everything until I get to 0.2, from that point it returns 0, even though I have used the roundf() function, multiplied by 100, so I don't understand it. If I multiply by 100, 0.2 = 20 cents, it should be returning 2 instead of 0.

Edit2: Here, when I put cents in the dime loop, it returns correctly when less than 25 cents. However, when I put 0.32, it returns 6. When I put "cents" everywhere, it returns 42 when I type 0.32.

``````if (cents >= 25)
{
quarter = cents / 25;
remainder = cents % 25;
}

if (cents >= 10)
{
dime = cents / 10;
remainder = cents % 10;
}

if (cents >= 5)
{
nickel = remainder / 5;
remainder = remainder % 5;
}

if (cents >= 1)
{
penny = remainder / 1;
}

// tell our customer how much coins we owe him/her
coin_count = quarter + dime + nickel + penny;
printf("%d\n", coin_count);
``````

• Can you please edit your question and add the exact lines of code where you do this? Also include the lines where you declare the variables used in this/these lines. Sep 15 '15 at 16:46
• Voila, but I don't know what went wrong with my code here. Sorry for not being more specific, I thought I'd better try figuring it out on my own, with only a tiny bit of help. :)
– Lana
Sep 15 '15 at 19:35

The logic of your program won't handle anything less than 25 cents. All of the calculations for coins other than quarters depend on the remainder value being passed from the previous calculation. But look what happens when the initial amount is less than \$0.25. The statement `if (cents >= 25)` will prevent `remainder` from being updated, so it will be zero, and that's what is passed through and processed by each subsequent calculation.

You need to use `cents` as a running total to track the remaining change instead of remainder.

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

• I've tried it before, by replacing the remainder and putting cents. Now I've put "cents" only in the second loop and it works for under 0.25 but not for other numbers. :/
– Lana
Sep 15 '15 at 20:48
• Don't know exactly what you did, so I can't tell you what's wrong. However, you should be using cents in every calculation. I'd go into more detail, but that would be doing the assignment for you. ;-) Sep 15 '15 at 21:05
• Exactly. Trust me, I've tried it. I understand perfectly what you are saying, I get the logic, but not why it won't work. When I put cents everywhere, it prints out larger numbers than it should. I've tried all the combinations I could think of. Should I click the check mark or wait a bit if someone could indicate me where the problem hides?
– Lana
Sep 15 '15 at 21:09
• Can you edit your question and put the current code in? Sep 15 '15 at 21:10
• Did it. Thanks a lot. I hope I'm not bothering you too much.
– Lana
Sep 15 '15 at 21:16