Input problem

Hi, this is my first project. Its a money change program that calculates how much change in denominations to give back.

The problem is when I input a x.x6 to x.x9 in total, the value is always slightly less than what I inputted. This is causing my program to output one less penny.

Please someone help me.

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
#include <iostream>
#include <iomanip>

using namespace std;

int calculateChange(float *pTotal, float denomination)
{
	int change;
	change = *pTotal / denomination;
	*pTotal = *pTotal - (denomination*change);
	return change;
}

int main()
{
	
	cout << "Enter amount ";
	float total;
	float *pTotal = &total;
	cin >> total;
	int change;

	change = calculateChange(&total, 100);				
	cout << endl << setw(10) << "$100 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 50);
	cout << endl << setw(10) << "$50 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 20);
	cout << endl << setw(10) << "$20 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 10);
	cout << endl << setw(10) << "$10 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 5);
	cout << endl << setw(10) << "$5 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 2);
	cout << endl << setw(10) << "Toonies" << setw(10) << change << endl;

	change = calculateChange(&total, 1);
	cout << endl << setw(10) << "Loonies" << setw(10) << change << endl;

	change = calculateChange(&total, 0.25);
	cout << endl << setw(10) << "Quarters" << setw(10) << change << endl;

	change = calculateChange(&total, 0.10);
	cout << endl << setw(10) << "Dimes" << setw(10) << change << endl;

	change = calculateChange(&total, 0.05);
	cout << endl << setw(10) << "Nickels" << setw(10) << change << endl;

	change = calculateChange(&total, 0.01);
	cout << endl << setw(10) << "Pennies" << setw(10) << change << endl<<endl;

	system("pause");
	return 0;

}
closed account (48T7M4Gy)
Without checking too much, your program is probably suffering rounding errors as a result of using float's. Double's might improve it but the problem doesn't go away completely until you work with integers ie cents.

Instead of $27.57 convert the amount at the start to 2757 cents and work from there. A $20.00 note is simply 2000 cents so you can still work out the change in cents then convert back at the last moment

eg 2757 - 2000 = $20 + 757 - 500 = $20 + $5 + 257 etc ...
Thank you very much. I've made the changes and it works perfectly now.
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
#include <iostream>
#include <iomanip>

using namespace std;

int calculateChange(int *pTotal, int denomination)
{
	int change;
	change = *pTotal / denomination;
	*pTotal = *pTotal - (denomination*change);
	return change;
}

int main()
{
	
	cout << "Enter amount ";
	int total;
	int *pTotal = &total;
	float amount;
	cin >> amount;
	total = amount * 100;

	int change;

	change = calculateChange(&total, 10000);				
	cout << endl << setw(10) << "$100 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 5000);
	cout << endl << setw(10) << "$50 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 2000);
	cout << endl << setw(10) << "$20 Bills" << setw(10) << change << endl;


	change = calculateChange(&total, 1000);
	cout << endl << setw(10) << "$10 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 500);
	cout << endl << setw(10) << "$5 Bills" << setw(10) << change << endl;

	change = calculateChange(&total, 200);
	cout << endl << setw(10) << "Toonies" << setw(10) << change << endl;

	change = calculateChange(&total, 100);
	cout << endl << setw(10) << "Loonies" << setw(10) << change << endl;

	change = calculateChange(&total, 25);
	cout << endl << setw(10) << "Quarters" << setw(10) << change << endl;

	change = calculateChange(&total, 10);
	cout << endl << setw(10) << "Dimes" << setw(10) << change << endl;

	change = calculateChange(&total, 5);
	cout << endl << setw(10) << "Nickels" << setw(10) << change << endl;

	change = calculateChange(&total, 1);
	cout << endl << setw(10) << "Pennies" << setw(10) << change << endl<<endl;

	system("pause");
	return 0;

}
Topic archived. No new replies allowed.