incorrect output

when i run the following program and input .99 as the money amount, it tells me that i used 3 quarters, 2 dimes, and 3 pennies. it should use 4 pennies.

cout << "Enter an amount less than $1.00: ";
cin >> moneyamount;
quartercount = moneyamount / quarter;
moneyremain = moneyamount - (quartercount*quarter);
dimecount = moneyremain / dime;
moneyremain = moneyremain - (dimecount*dime);
nickelcount = moneyremain / nickel;
moneyremain = moneyremain - (nickelcount*nickel);
pennycount = moneyremain / penny;

cout << "You used " << quartercount << " quarters, " << dimecount << " dimes, "
<< nickelcount << " nickels, and " << pennycount << " pennies." << endl;

return 0;

if i modify the code so that it doesn't display the value stored in penny count and instead display the value of (moneyremain/penny) it calculates correctly and shows 4. any idea why this is happening?
If you are using float as your type, then you are getting rounding errors. Try using doubles instead.

Another useful function that you could use is the fmod() function in <cmath>.

If that doesn't work, then I suggest multiplying by 100 and casing to an int. Then use integer division and the modulous operator.
Topic archived. No new replies allowed.