Need help with this function.

I am trying to make the double change; display what it is equal to in terms of change due in dollars, quarters, dimes, nickels and pennies.

Here is an example of what my code does and what I want it to do in the end.
Price of Item: $15.00
Amount Payed: $20.55
Your change is: $5.55
That is: 5 dollars, 2 quarters, 0 dimes, 1 nickel, and 0 pennies.
(^^^What I want it to do^^^) I don't know how I could make it display the change in that format when change could technically be given in multiple ways.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  void changeCalculator()
{
	double itemPrice;
	double amountPayed;
	double change;
	
	cout << "Price of Item: $";
	cin >> itemPrice;

	cout << "" << endl;

	cout << " Amount Payed: $";
	cin >> amountPayed;
	
	cout << "" << endl;

	change = amountPayed - itemPrice;

	cout.precision(2);
	cout.setf(ios::fixed);

	cout << "The change is: $" << change << endl;
}
What was wrong with this thread?

http://www.cplusplus.com/forum/beginner/142417/
This thread and that thread are not related. This is a different project. Read my whole post and you will understand. I am trying to display the change in digital currency.
I can see why you may have related the two because they both deal with money and I figure you probably saw this "cout << "This reads: 10 Dollars, 1 Quarter, 1 Dime, 1 Nickel, and 10 Pennies. Which equals, $10.50" << endl;". <-- This was only an output for users to understand how to input the values I wanted them to input. It wasn't a formula that converts.

Here is another example:

Change = $5.43

That is: 5 dollar(s), 1 quarter(s), 1 dime(s), 1 nickel(s), and 3 pennie(s)

The bold statement is what I would like to display based off of a function that will convert the change value into a format like the one in bold.
Last edited on
This is a classic programming problem which I know under the name
making change'. With American coins you are lucky and can use a greedy algorithm - just take the biggest coins that fit first and then move to smaller ones. With 12-cent coins added in, the greedy algorithm will not generate the fewest number of coins in all cases, though.

If you only need real US coin support, then use a greedy algorithm - just keep adding larger coins/bills first until it overflows, then move to smaller ones.
I'll give it a shot. Thank you for the wisdom. If anyone else has other solutions as well please post them. Thank you.
I prefer the cent method where 1 dollar = 100 pennies, a quarter = 25 ect..
Though you could use the decimal way like you currently are where 1 dollar = 1 dollar, 1 quarter = .25 dollar ect..

You need to remove the largest amounts first until you run out of money.
I find it easier to work with integers for this.

Here's pseudo code.

1
2
3
4
5
dollars = change(int)
change -= dollars
quarters = change * 4 (int)
change -= dollars quarters
...ect


*fixed logic fail
Last edited on
Yes, you should not use floating point types when dealing with currencies. You will get sued for millions one day due to floating point rounding errors.
Would you recommend altering values using a staticcast then? Or just multiplying by a 1.0 etc.?
Just don't use floating point types anywhere. Stick to storing everything in terms of cents.
Multiplying by 1 would be a bit redundant. I would personally parse it into cent to make it 100% accurate instead of the flawed floating point method. To get change is actually a pretty effortless task with stringstreams. I would personally mimic doubles like this.
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
#include <iostream>

struct Double
{
	int characteristic;
	char decimal;
	int mantissa;
	
	friend std::ostream &operator <<(std::ostream &stm, Double const &rhs)
	{
		return stm << rhs.characteristic << rhs.decimal << rhs.mantissa;
	}
	
	friend std::istream &operator >>(std::istream &stm, Double &rhs)
	{
		return stm >> rhs.characteristic >> rhs.decimal >> rhs.mantissa;
	}
	
	Double operator-(Double const &rhs) const
	{
		Double result = *this;
		if(rhs.mantissa > result.mantissa)
		{
			--result.characteristic;
			result.mantissa += 100;
		}
		
		result.characteristic -= rhs.characteristic;
		result.mantissa -= rhs.mantissa;
		
		return result;
	}
};

int main()
{
	Double itemCost;
	std::cout << "Please enter the item cost: ";
	std::cin >> itemCost;
	
	Double amountPaid;
	std::cout << "Please enter the amount paid: ";
	std::cin >> amountPaid;
	
	Double change = amountPaid - itemCost;
	std::cout << "Change due $" << change << std::endl;
	//now you can get the exact change easy
	
	return 0;
}
Please enter the item cost: 15.0
Please enter the amount paid: 20.55
Change due $5.55

http://ideone.com/mwlr1B

Ps there may be bugs or something you don't like in the code, I was just showing an example how to do all integers with the appearance of doubles if you didn't want to have the user enter it in cents. If you had the user enter as cents 15$ = 1500
Last edited on
Okay so last night after doing the math for awhile I made an algorithm that can make my question possible. @LB as far as your advise to not use floats, I now see why. I noticed that about 5% of the time memory will round the pennies up by 1. I found two ways to solve my problem. One was to use division and mods, and the second was through this algorithm. 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
void changeCalculator()
{
	double itemPrice; //stores the item price
	double amountPayed; //stores the amount payed
	double change; //stores the amount of change

	cout << "Price of Item: $";
	cin >> itemPrice;

	cout << " Amount Payed: $";
	cin >> amountPayed;

	if (amountPayed >= itemPrice)
	{

		cout << "" << endl;

		change = amountPayed - itemPrice;

		cout.precision(2);
		cout.setf(ios::fixed);

		cout << "The change is: $" << change << endl;

		int cents; //stores the converted dollar amount to cent amount
		int hundred; //stores the amount of hundred dollar bills
		int fifty; //stores the amount of fifty dollar bills
		int twenty; //stores the amout of twenty dollar bills
		int ten; //stores the amount of ten dollar bills
		int five; //stores the amount of five dollar bills
		int dollars; //stores the amount of dollar bills
		int quarters; //stores the amount of quarters
		int dimes; //stores the amount of dimes
		int nickels; //stores the amount of nickels
		int pennies; //stores the amount of pennies

		cents = change * 100; //conversion of change into cents (stores the current change)

		hundred = cents / 10000;
		cout << hundred << " Hundred(s)" << endl;
		cents = cents - (hundred * 10000);

		fifty = cents / 5000;
		cout << fifty << " Fifty(s)" << endl;
		cents = cents - (fifty * 5000);

		twenty = cents / 2000;
		cout << twenty << " Twenty(s)" << endl;
		cents = cents - (twenty * 2000);

		ten = cents / 1000;
		cout << ten << " Ten(s)" << endl;
		cents = cents - (ten * 1000);

		five = cents / 500;
		cout << five << " Five(s)" << endl;
		cents = cents - (five * 500);

		dollars = cents / 100; 
		cout << dollars << " Dollar(s)" << endl;
		cents = cents - (dollars * 100); 

		cout << "" << endl; //newline

		quarters = cents / 25; 
		cout << "Quarters: " << quarters << endl;
		cents = cents - (quarters * 25); 


		dimes = cents / 10; 
		cout << "Dimes: " << dimes << endl;
		cents = cents - (dimes * 10); 


		nickels = cents / 5; 
		cout << "Nickels: " << nickels << endl;
		cents = cents - (nickels * 5); 

		pennies = cents / 1; 
		cout << "Pennies: " << pennies << endl;
	}
It can still be slightly off since you are using doubles. 1 could be represented as .999999999999999 or 1.00000000000000000001 or some other arbitrary value.
Topic archived. No new replies allowed.