# Cash Program not returning correct value for .41

I'm working on the Cash program in Week 2 and I think I have it all finished up the only problem is when I run check50 I get everything right except for .41 instead of returning 4 coins it gives an answer of 3 coins and I just can't figure out why. It returns the correct value for every other answer but not this one and I'm stumped. I appreciate any help or advice. Here's my code:

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

int main(void)

{
float dollar;

do
{
dollar = get_float("Change: \$");
}
while (dollar <= 0);

int cents = dollar * 100;
int coins = 0;

while (cents >= 25)
{
cents = cents - 25;
coins++;
}
while (cents >= 10)
{
cents = cents - 10;
coins++;
}
while (cents >= 5)
{
cents = cents - 10;
coins++;
}
while (cents > 0)
{
cents = cents - 1;
coins++;
}

printf("You will need at least %i coins\n", coins);

}
``````

Have you tried printing out the value stored in cents before calculating the number of coins? Perhaps print out the value in dollar to 32 decimal places?

The problem lies here:

``````int cents = dollar * 100;
``````

When a float value is assigned to an int, the fractional portion of the number is truncated down. It is never rounded up. So, if you have, say, 40.9999999999976 cents in a float and it is assigned to an int, the result is 40, not 41.

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

• how do I fix it though? because when I tried to change it to a float it still returned the same value `code` float cents = dollar * 100.00; Dec 11 '20 at 2:01
• Well, I could tell you, but this is the central purpose of the assignment. I suggest that you review the lecture and the video shorts related to it. I also hinted at it in my answer. Dec 11 '20 at 2:06
• thanks that wasn't even the problem lol I had it subtracting 10 twice instead of going to 5 cents. Thanks for your help. Dec 11 '20 at 2:31
• Surprised I missed that. It's a problem too. ;-) Dec 11 '20 at 2:33
• thank you for your help though seriously. I appreciate it. Dec 11 '20 at 2:37