### I NEED HELP

hi, i am new to C++...
i am working on a graded homework..
its a program that asks to:
*enter an amount in \$
*let the program display the dollar amount as bills and coins..
Note that 1 quarter = \$0.25
1 dime = \$0.1
1 nickel = \$0.05
1 penny = \$0.01
for example:
\$62.30 would be split into 3- 20 dollar bills, 0- 10 dollar bills, 0 - 5 dollar bills, 2- 1 dollar bills, 1-quarters, 0 dimes, 1-nickels and 0-pennies.

*the output of \$62.30 should be exactly the same as the example
and here where my problem is i am not getting the same
i am getting
3- 20 dollar bills, 0- 10 dollar bills, 0 - 5 dollar bills, 2- 1 dollar bills, 1-quarters, 0 dimes, 0-nickels and 4-pennies.

thats what i wrote:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include void main() { int twenty,ten,five,one,quarters,dimes,nickels,pennies,twenty1,ten1,five1,one1; float amount,total,total1,total2,total3,total4,total5,total6,quarters1,dimes1,nickels1; cout <<"ENTER THE AMOUNT IN \$" <>amount; twenty = amount / 20; twenty1 = twenty * 20; total = amount - twenty1; ten = total / 10; ten1 = ten * 10; total1 = total - ten1; five = total1 / 5; five1 = five * 5; total2 = total1 - five1; one = total2 / 1; one1 = one * 1; total3 = total2 - one; quarters = total3 / 0.25; quarters1 = quarters * 0.25; total4 = total3 - quarters1; dimes = total4 / 0.1; dimes1 = dimes * 0.1; total5 = total4- dimes1; nickels = total5 / 0.05; nickels1 = nickels * 0.05; total6 = total5 - nickels1; pennies = total6 / 0.01; cout<

i noticed by outputting all the total variable that total3=0.299999 instead of simply 0.3

what should be the solution in your opinion??

Thank you for your help!
Last edited on
Thanks for replying :) how nice of you!!
Yes, you get rounding problems. Especially when you mingle int and float.
I'd suggest that you use double for all variables.
Base 10 floating point to binary has some issues. Just use double for variables and you should be fine for most things you'll be doing
For this particular problem I wouldn't use double or float variables at all. Rather store your amounts as number of pennies in an integral type, then you're guaranteed accurate representation.
While in a production app that requires real precision (anything dealing with real money) you would use integers, you can likely get away with just setting the precision displayed.
`std::cout >> setprecision(2)`
Topic archived. No new replies allowed.