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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream.h>

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 $"
     <<endl;

cin>>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<<endl
    <<"$"<<amount<<" would be split into: "<<twenty<<"-$20 bills, "<<ten<<"-$10 dollar bills, "<<five<<"-$5 bills, "<<one<<"-$1 bills, "
	<<quarters<<"-quarters, "<<dimes<<"-dimes, "<<nickels<<"-nickels and "<<pennies<<"-pennies."
	<<endl;


}



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.