Change machine off by a penny... sometimes.

Hello. I'm brand new to the forum. I have been lingering and searching for a while but never opted to join up.

I have to write a program that will determine the change to be given back to a customer based on input values of purchase amount and cash given as payment. The program will also tell the number and type of coins to be dispensed as change. (the purchase amount and payment have be set up sequentially in an input file).

I have used a while loop and if statements so far, though, I'm sure there are much simpler ways. For a few instances it works. However, for others, the coins dispensed are off by a penny.

Here is my code

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

using namespace std;

int
main()
{
	float cost, given, change, dollar=1.00, quarter=.25, dime=.10, nickel=.05, penny=.01, count;
	char p4data[80] = "project4data.txt";
	ifstream infile;
	infile.open(p4data);
	infile>>cost;
	infile>>given;
	while(!infile.eof())
	{
		cout<<setprecision(2)<<fixed;
		change = given-cost;
		cout<<endl<<endl<<" Cost of purchase:\t\t$"<<cost<<endl;
		cout<<" Cash given by customer:\t$"<<given<<endl;
		cout<<" Change:\t\t\t$"<<change<<endl;
		if (change==0)
		{
			cout<<"\t\tNo change required"<<endl;
		}
		if (change<0)
		{
			change=change*(-1);
			cout<<"\t\tCustomer owes: $"<<change<<endl;
			change=0;
		}
		if (change>0)
		{
			count=0;
			cout<<endl<<"\t Coins given:"<<endl;
			while (change>=dollar)
			{
				change=change-dollar;
				++count;
			}
			if (count>1)
			{
				cout<<"\t\t\t"<<count<<" dollars"<<endl;
			}
			if (count==1)
			{
				cout<<"\t\t\t"<<count<<" dollar"<<endl;
			}
			count=0;
			while (change>=quarter)
			{
				change=change-quarter;
				++count;
			}
			if (count>1)
			{
				cout<<"\t\t\t"<<count<<" quarters"<<endl;
			}
			if (count==1)
			{
				cout<<"\t\t\t"<<count<<" quarter"<<endl;
			}
			count=0;
			while (change>=dime)
			{
				change=change-dime;
				++count;
			}
			if (count>1)
			{
				cout<<"\t\t\t"<<count<<" dimes"<<endl;
			}
			if (count==1)
			{
				cout<<"\t\t\t"<<count<<" dime"<<endl;
			}
			count=0;
			while (change>=nickel)
			{
				change=change-nickel;
				++count;
			}
			if (count>1)
			{
				cout<<"\t\t\t"<<count<<" nickels"<<endl;
			}
			if (count==1)
			{
				cout<<"\t\t\t"<<count<<" nickel"<<endl;
			}
			count=0;
			while (change>=penny)
			{
				change=change-penny;
				++count;
			}
			if (count>1)
			{
				cout<<"\t\t\t"<<count<<" pennies"<<endl;
			}
			if (count==1)
			{
				cout<<"\t\t\t"<<count<<" penny"<<endl;
			}
		}

		infile>>cost;
		infile>>given;
	}
	infile.close();
	cout<<endl<<endl;
	return 0;
}


Here is what is in project4data.txt

8.07
10.00
8.06
10.00
15.63
15.63
6.25
5.25
9.01
10.00
8.99
10.00
13.34
20.00
1.95
2.00
3.75
5.00


And here is the output I'm currently getting.



 Cost of purchase:              $8.07
 Cash given by customer:        $10.00
 Change:                        $1.93

         Coins given:
                        1.00 dollar
                        3.00 quarters
                        1.00 dime
                        1.00 nickel
                        3.00 pennies


 Cost of purchase:              $8.06
 Cash given by customer:        $10.00
 Change:                        $1.94

         Coins given:
                        1.00 dollar
                        3.00 quarters
                        1.00 dime
                        1.00 nickel
                        3.00 pennies


 Cost of purchase:              $15.63
 Cash given by customer:        $15.63
 Change:                        $0.00
                No change required


 Cost of purchase:              $6.25
 Cash given by customer:        $5.25
 Change:                        $-1.00
                Customer owes: $1.00


 Cost of purchase:              $9.01
 Cash given by customer:        $10.00
 Change:                        $0.99

         Coins given:
                        3.00 quarters
                        2.00 dimes
                        3.00 pennies


 Cost of purchase:              $8.99
 Cash given by customer:        $10.00
 Change:                        $1.01

         Coins given:
                        1.00 dollar
                        1.00 penny


 Cost of purchase:              $13.34
 Cash given by customer:        $20.00
 Change:                        $6.66

         Coins given:
                        6.00 dollars
                        2.00 quarters
                        1.00 dime
                        1.00 nickel


 Cost of purchase:              $1.95
 Cash given by customer:        $2.00
 Change:                        $0.05

         Coins given:
                        4.00 pennies


 Cost of purchase:              $3.75
 Cash given by customer:        $5.00
 Change:                        $1.25

         Coins given:
                        1.00 dollar
                        1.00 quarter


Press any key to continue . . .


as you can see some answers are correct and some answers are off.

Thanks for any help! and I apologize for the length of the code!
floating points are approximations. You should never use == or != with floating points, and you should always expect some rounding errors.

Instead of using floating points for this, I would use integers. And instead of representing the number of dollars, represent the number of cents instead.

for example, instead of float onedollar = 1.00; do int onedollar = 100;

Then when printing, just divide by 100 to get the dollars, and mod by 100 to get the cents:

1
2
3
4
int money = 436;

// print money as $4.36
cout << '$' << (money / 100) << '.' << setw(2) << setfill('0') << (money % 100);

Last edited on
If this is a real project (not just homework), I suggest you do the hard work of writing your own class in the beginning to save you headaches later on.

For money, ultimately, you will need to write your own class around integer (as suggested by Disch) or around a prepackaged Decimal class like this:

http://speleotrove.com/decimal/

Once you do interest compounding or any kind of general division or multiplication; say, divide $1.00 in 3 ways, you will have to write your own algorithm on how you want to deal with that last penny! In other words, you want to implement your own operator*() and operator/(), possibly with support classes to help you minimize rounding errors.

Best to deal with that in a controlled test-environment, before you end up in debugging-hell, tracking phantom pennies through giant accounting ledgers!
Thank you Disch! All of my pennies are there! But, to be thorough, what exactly is the setfill() command doing?

Again, thanks so much.

And kfmfe04, lucky for me it's just homework. Doing this on a large scale would probably cause massive hemorrhaging in my brain. : )
http://www.cplusplus.com/reference/iostream/manipulators/setfill/

Without it, you will have issues with values less than 10 cents.
Last edited on
Thank you so much!
Topic archived. No new replies allowed.