My method for solving this problem was to use a for loop for each coin, and add the each used to a variable. While it seems that I get the right answer for quarters and dimes, the program ignores nickles when I subtract nickles from the remaining amount. The same goes for pennies. I'm confused because the syntax structure is the same for each loop. Does it have something to do with round(x)?
int main(void)
{
float change;
int quarters;
int dimes;
int nickles;
int pennies;
int coins = 0;
do
{
printf("how much change?");
change = GetFloat();
for (quarters = 0; change >= 0.25; quarters++)
{
change = change - 0.25;
}
coins = quarters + coins;
for (dimes = 0; change >=0.10; dimes++)
{
change = change - 0.10;
}
coins = dimes + coins;
for (nickles = 0; change >= 0.05; nickles++)
{
change = change - 0.05;
}
coins = nickles + coins;
for (pennies = 0; change >= 0.01; pennies++)
{
change = change - 0.01;
}
coins = pennies + coins;
printf("%i\n", coins);
}
while (change < 0);
}
Here are the results of the CS50 check: https://sandbox.cs50.net/checks/41f60d056ae641ae9082a2f8e264b74c