Precision Errors in a Change Calculator

Hello folks, so the assignment in question is to create a program that accepts two values (amount owed, amount paid) and returns a change value along with the correct amounts of dollars and coins that would make up that change.

My issue is that, in certain cases, the coin output seems to lose a penny! I've been messing around with the code for a few hours now and I can't get it to work. Here's the code without any alterations that presents the issue:

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
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	double amountOwed = 0.0;
	double amountPaid = 0.0;

restart:

	cout << "Enter the amount owed: $";
	cin >> amountOwed;
	cout << "Enter the amount paid: $";
	cin >> amountPaid;

	if (amountOwed > amountPaid)
	{
		cout << "You owe more money than that! Try again.";
		cout << endl << endl;
goto restart;
	}

	if (amountOwed == amountPaid)
	{
		cout << "No change needed. Bye!";
		cout << endl << endl;
goto restart;
	}

	double change = 0.0;
	change = amountPaid - amountOwed;

	cout << endl << "The change for this transaction is: $" << setprecision(2) << fixed << change << endl << endl;

	int dollars = 0;
	int quarters = 0;
	int dimes = 0;
	int nickels = 0;
	int pennies = 0;

	dollars = change / 1;
	change = change - dollars;
	
	quarters = change / .25;
	change = change - (quarters * .25);

	dimes = change / .10;
	change = change - (dimes * .10);

	nickels = change / .05;
	change = change - (nickels * .05);

	pennies = change / .01;
	change = change - (pennies * .01);

	cout << "Dollars:  " << dollars << endl;
	cout << "Quarters: " << quarters << endl;
	cout << "Dimes:    " << dimes << endl;
	cout << "Nickels:  " << nickels << endl;
	cout << "Pennies:  " << pennies << endl;

	cout << endl;

goto restart;

	return 0;
}


I believe the issue arises due to the use of a double value (change) inside calculations for int values, but I can't seem to correct it. I tried converting the change value into an integer value and multiplying by it and the values inside the dollar/coin values by 100, but the issue persisted. I also tried using this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//change has been reduced to penny-level by previous calculations
if (change / .01 > 1)
{
	pennies = change / .01;
	change = change - (pennies * .01);

	if (abs(change - .01) < .001 && abs(change - .01) != 0)
		pennies ++;

	if (pennies == 5)
	{
		pennies = 0;
		nickels ++;
	}
}

But I still got the same vanishing penny errors ($.11 change worked, but $.16 resulted in 1 dime and 1 nickel, no pennies).

Any help is greatly appreciated! I've still got a few days to get this done and in the mean time I'll be able to talk with the professor, but I figured it couldn't hurt to get some outside help seeing as he can be a bit rough with forcing us to figure out issues like this.

On an unrelated note, is there a way to break up an inputted string into its individual characters?

Thanks!
Last edited on
use the % mathmatical function to help you...
closed account (L1AkoG1T)
To answer your string question, yes, it is possible, and you can do it just like the string is an array. This:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	string hello = "hello";

	for(int i = 0 ; i < 5 ; i++)
	{
		cout << hello[i];
	}

	system("PAUSE");
	return 0;
}

Outputs:
 
helloPress any key to continue . . .
Kewl
Topic archived. No new replies allowed.