Change Project Need Help

I got this project in class and it works for the most part but there is one issues I have been fiddling around with for about 3 hours now and can't quite seem to get it and I apologize for it being a bit sloppy but a few tips would be nice.
Examples
.70 Price
$1.00 Payed With
Change some reason displays
2 Dime's and no pennies even though it should be 4 pennies


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
 #include <iostream>
#include <iomanip>

using namespace std;

void Money(double Change)
{
	if (Change > 99.995)
	{
		cout << "1 $100 Bill" << endl;
		Change = Change - 100;
	}

	if (Change > 94.995)
	{
		cout << "1 $50 Bill\n2 $20 Bill's\n1 $10 Bill\n1 $5 Bill" << endl;
		Change = Change - 95;
	}

	if (Change > 89.995)
	{
		cout << "1 $50 Bill\n2 $20 Bill's\n1 $10 Bill" << endl;
		Change = Change - 90;

	}

	if (Change > 84.995)
	{
		cout << "1 $50 Bill \n2 $20 Bill's \n1 $5 Bill" << endl;
		Change = Change - 85;
	}

	if (Change > 79.995)
	{
		cout << "1 $50 Bill \n2 $20 Bill's" << endl;
		Change = Change - 80;
	}

	if (Change > 74.995)
	{
		cout << "1 $50 Bill \n1 $20 Bill \n1 $5 Bill " << endl;
		Change = Change - 75;
	}

	if (Change > 69.995)
	{
		cout << "1 $50 Bill \n1 $20 Bill " << endl;
		Change = Change - 70;
	}

	if (Change > 64.995)
	{
		cout << "1 $50 Bill \n1 $10 Bill \n1 $5 Bill" << endl;
		Change = Change - 65;
	}

	if (Change > 59.995)
	{
		cout << "1 $50 Bill \n1 $10 Bill" << endl;
		Change = Change - 60;
	}

	if (Change > 54.995)
	{
		cout << "1 $50 Bill \n1 $5 Bill" << endl;
		Change = Change - 55;
	}

	if (Change > 49.995)
	{
		cout << "1 $50 Bill" << endl;
		Change = Change - 50;
	}

	if (Change > 44.995)
	{
		cout << "2 $20 Bill's \n1 $5 Bill" << endl;
		Change = Change - 45;
	}

	if (Change > 39.995)
	{
		cout << "2 $20 Bill's" << endl;
		Change = Change - 40;
	}

	if (Change > 34.995)
	{
		cout << "1 $20 Bill \n1 $10 Bill \n1 $5 Bill" << endl;
		Change = Change - 35;
	}

	if (Change > 29.995)
	{
		cout << "1 $20 Bill \n1 $10 Bill" << endl;
		Change = Change - 30;
	}

	if (Change > 24.995)
	{
		cout << "1 $20 Bill \n1 $5 Bill" << endl;
		Change = Change - 25;
	}

	if (Change > 19.995)
	{
		cout << "1 $20 Bill" << endl;
		Change = Change - 20;
	}

	if (Change > 14.995)
	{
		cout << "1 $10 Bill \n1 $5 Bill" << endl;
		Change = Change - 15;
	}

	if (Change > 9.995)
	{
		cout << "1 $10 Bill" << endl;
		Change = Change - 10;
	}

	if (Change > 4.995)
	{
		cout << "1 $5 Bill" << endl;
		Change = Change - 5;
	}

	if (Change > .995)
	{
		cout << "1 $1 Dollar" << endl;
		Change = Change - 1;
	}

	if (Change > .94995)
	{
		cout << "3 Quarter's \n2 Dime's" << endl;
		Change = Change - .95;
	}

	if (Change > .89995)
	{
		cout << "3 Quarter's \n1 Dime \n1 Nickel" << endl;
		Change = Change - .90;
	}

	if (Change > .84955)
	{
		cout << "3 Quarter's \n1 Dime" << endl;
		Change = Change - .85;
	}

	if (Change > .79995)
	{
		cout << "3 Quarter's \n1 Nickel" << endl;
		Change = Change - .80;
	}

	if (Change > .74995)
	{
		cout << "3 Quarter's" << endl;
		Change = Change - .75;
	}

	if (Change > .64995)
	{
		cout << "2 Quarter's  \n1 Dime \n1 Nickel" << endl;
		Change = Change - .65;
	}

	if (Change > .59995)
	{
		cout << "2 Quarter's \n1 Nickel" << endl;
		Change = Change - .60;
	}

	if (Change > .54995)
	{
		cout << "2 Quarter's \n1 Nickel" << endl;
		Change = Change - .55;
	}

	if (Change > .49995)
	{
		cout << "2 Quarter's" << endl;
		Change = Change - .50;
	}

	if (Change > .44995)
	{
		cout << "1 Quarter \n2 Dime's" << endl;
		Change = Change - .45;
	}

	if (Change > .39995)
	{
		cout << "1 Quarter \n1 Dime \n1 Nickel" << endl;
		Change = Change - .40;
	}

	if (Change > .34995)
	{
		cout << "1 Quarter \n1 Dime" << endl;
		Change = Change - 35;
	}

	if (Change > .29995)
	{
		cout << "1 Quarter \n1 Nickel" << endl;
		Change = Change - .30;
	}

	if (Change > .24995)
	{
		cout << "1 Quarter" << endl;
		Change = Change - .25;
	}

	if (Change > .19995)
	{
		cout << "2 Dime's" << endl;
		Change = Change - .20;
	}

	if (Change > .14995)
	{
		cout << "1 Dime \n1 Nickel" << endl;
		Change = Change - .15;
	}

	if (Change > .09995)
	{
		cout << "1 Dime" << endl;
		Change = Change - .10;
	}

	if (Change > .05)
	{
		cout << "1 Nickel" << endl;
		Change = Change - .05;
	}
	if (Change >= .05)
	{
		if (Change >= .04)
		{
			cout << "4 Pennies" << endl;
			Change = Change - .04;
		}

		if (Change >= 0.03)
		{
			cout << "3 Pennies" << endl;
			Change = Change - .03;
		}

		if (Change >= 0.02)
		{
			cout << "2 Pennies" << endl;
			Change = Change - .02;
		}

		if (Change > .00995)
		{
			cout << "1 Penny" << endl;
			Change = Change - .01;
		}
	}
	if (Change == 0.0000)
	{
		return;
	}
}


int main()
{
Start:
	system("cls");
	double Change = 0, Item_Cost = 0, Money_Given = 0, Tax = 0, Input = 0;

	cout << "Item Cost:";
	cin >> Item_Cost;

	cout << "Money Given:";
	cin >> Money_Given;

	Tax = Item_Cost * .08125;

	Change = Money_Given - (Item_Cost + Tax);

	cout << "Your change is $" << fixed << setprecision(2) << Change << "." << endl;

	Money(Change);

	cout << "(1) Put Another Item In   (2) To Exit " << endl;
	cin >> Input;
	if (Input == 1)
	{
		goto Start;
	}

	if (Input == 2)
	{
		return 0;
	}

	system("pause");
	

}
a) dont use system
b) dont use using namespace std;
c) never use goto or this will happen: http://xkcd.com/292/
d) for the money() function i would use the modulo operator (%)
That was just parts of my many attempts to make this work and I know I just have been desperate to get it to work so I'll try those now. Thank you. Also you explain more on where to put the modulo operator.
Last edited on
You're not getting the pennies because with .043125 left for change - you won't get into this section. I think you meant to make that <= .05.

1
2
3
4
5
6
7
if (Change >= .05)
	{
		if (Change >= .04)
		{
			cout << "4 Pennies" << endl;
			Change = Change - .04;
		}
I have tried that and it caused only a few examples to work because the other example is 12.70 for cost and $100 as payment and a penny will not show up.
For $12.57 as the price and money given is a $100 the change would be 86.41 and the penny does not appear
You're rounding up with the setprecision(2) when you display the change to the user but that's not actually changing the value of the variable Change that's sent to the Money function.

I removed the setprecision(2) so that the change displayed the actual value of the variable. Your change running through the long list of if statements is $86.40, so that explains the missing penny. (Your text for the breakdown is off - I think you meant 1 $20 and 1 $10 instead of 2 $20.)

Item Cost:Money Given:Your change is $86.408687.
1 $50 Bill 
2 $20 Bill's 
1 $5 Bill
1 $1 Dollar
1 Quarter 
1 Dime 
1 Nickel
This doesn't use the modulus operator - but this is another way to separate out the bills for change. I just did the whole dollar bills, but coins could be done in a similar way. (77.20 is just a sample value)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	int remainingdollars, dollars, cents, twenties, tens, fives, ones;
	double change = 77.20;
	
	dollars = floor(change);  // floor needs <cmath>
	cents = (change-dollars)*100;
	cout << "dollars = " << dollars << "\tcents = " << cents << endl;
	
	twenties=dollars/20;
	cout << "twenties = " << twenties << endl;
	remainingdollars = dollars - 20*twenties;
	
	tens=remainingdollars/10;
	cout << "tens = " << tens << endl;
	remainingdollars = remainingdollars - 10*tens;
	
	fives=remainingdollars/5;
	cout << "fives = " << fives << endl;
	remainingdollars = remainingdollars - 5*fives;

	ones=remainingdollars;
	cout << "ones = " << ones << endl;
	remainingdollars = remainingdollars - ones;
dollars = 77	cents = 20
twenties = 3
tens = 1
fives = 1
ones = 2
Last edited on
I know I am having quite an issue with the penny if statement I have tried many different ways of fixing it but I will not work for both examples it will work for one but not the other and 2 $20's work fine if you do the math. I appreciate the feed back though.
1 $50 Bill = 50.00
2 $20 Bill's = 40.00
1 $5 Bill = 5.00
1 $1 Dollar = 1.00
1 Quarter = .25
1 Dime = .10
1 Nickel = .05
-------------------------
this adds up to 96.40 - not 86.40
I apologize about that I had a dunce moment with my math and is there any possible way I could round to fix this issue and wild blue I might try your idea most likely so thank you.
Last edited on
Wildblue am I along the right lines if I do it like this with the code you suggested

1
2
3
4
	quarters = remainingdollars;
	cout << "quarters = " << quarters / 4 << endl;
	remainingdollars = remainingdollars - 4 * 4;
divide by 25, not 4.

1
2
3
quarters = cents/25;
cout << "quarters = " << quarters << endl;
remainingcents = cents - 25*quarters;
So you divide it by how much its worth?
I'm a little slow at times sorry.
Last edited on
Let's say I need to give back 87 cents. What's the maximum number of quarters that I could return - or.. how many times does 25 go into 87?

87 divided by 25 (using integer division) will give 3 as the number of quarters I can give back.

1
2
3
quarters = cents/25;  // so quarters = 87/25 = 3
cout << "quarters = " << quarters << endl; 
remainingcents = cents - 25*quarters; // remainingcents = 87 - (25*3) or 87 - 75 = 12 cents left 
Topic archived. No new replies allowed.