Express a certain amount of money into dollars and pennies?

Hi guys,

I have to write a program that shows the number of bills and coins for a given amount of money. For example, if the user enters $166.89, then the program will state that it has a one hundred-dollar bill, a fifty-dollar bill, a ten-dollar bill, a one-dollar bill, a five-dollar bill, three quarter coins, a dime, and four pennies. I am not sure how to approach this problem properly, so please give some hints. This is what I have at the moment, mostly just guesses:

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

using namespace std ;
int main ()
{
	// ********** Declare Variable **********
	float    change;
	string    Name;

	// ********** Input Section **********
	cout << "Please enter your name: ";
	getline(cin, Name);
	cout << "Hello" << " " << Name << ", " << "how much do you want to change?";
	cin >> change;

	//******* Processing section *******
	int hundreddollarbill = change / 100;

    int fiftydollarbill = change / 50;

	int twentydollarbill = change / 20;

	int tendollarbill = change / 10;

	int fivedollarbill = change / 5;

	int twodollarbill = change / 2;

	int onedollarbill = change / 1;
    
    int quarters  =  change / .25;

    int dimes = change / .10;

    int nickels = change / .05;

    int pennies = change / .01;

	// ********** Output Section **********
	cout << "Your change is: \n";
	cout << "\nHundred dollar bills: " << hundreddollarbill; 
    cout << "\nFifty dollar bills: " << fiftydollarbill; 
    cout << "\nTwenty dollar bills: " << twentydollarbill;
    cout << "\nTen dollar bills: " << tendollarbill;
    cout << "\nFive dollar bills: " << fivedollarbill; 
	cout << "\nTwo dollar bills: " << twodollarbill;
	cout << "\nOne dollar bills: " << onedollarbill;
	cout << "\nQuarters: " << quarters << endl; 
    cout << "\nDimes: " << dimes << endl; 
    cout << "\nNickels: " << nickels << endl; 
    cout << "\nPennies: " << pennies << endl;

	return 0;
}


Thanks in advance. :)
If I should give you $166.89 and I have already given that one hundred-dollar bill, how much more should I give you? In your current program you say: "$166.89". Can you figure out how to keep track of what has already been given?
I know that I am supposed to be subtracting the values, but I don't know how to set it up.

change - ?

Does what I currently have in the processing section make sense?
You need to make sure you are storing change after it's been changed - It's not hard, you just have to think for a second...Below is the answer. I also suggest making your output more neat - Use iomanip to set the margins left and right aligned.

Also,

change -= (100 * hundreddollarbill);

is the same thing as

change = change - (100 * hundreddollarbill);

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
int hundreddollarbill = change / 100;

    change -= (100 * hundreddollarbill);

    int fiftydollarbill =  change / 50;

    change -= (50 * fiftydollarbill);

	int twentydollarbill = change / 20;

    change -= (20 * twentydollarbill);

	int tendollarbill = change / 10;

	change -= (10 * tendollarbill);

	int fivedollarbill = change / 5;

	change -= (5 * fivedollarbill);

	int twodollarbill = change / 2;

    change -= (2 * twodollarbill);

	int onedollarbill = change / 1;

    change -= (1 * onedollarbill);

    int quarters  =  change / .25;

    change -= (.25 * quarters);

    int dimes = change / .10;

    change -= (.10 * dimes);

    int nickels = change / .05;

    change -= (.05 * nickels);

    int pennies = change / .01;

    change -= (.01 * pennies);
Last edited on
If you had the coefficients, counters, and names in a table, you could go through the table with a loop, rather than have endless repeats of essentially same code. (Let me guess: not yet in curriculum, so even a thought of such a thing is strictly forbidden.)
@leo255 Can you explain to me what you did? Why do you have so many of those change variable? How do they work? :O

@kes Well, yeah, we haven't learned about looping yet. It's better to stick with what we are learning, I guess. :)
Last edited on
He does not "have many". He updates the value of one variable multiple times.

Lets say:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// change happens to equal 42 now
int twentydollarbill = change / 20;
// twentydollarbill  got value 2

change -= (20 * twentydollarbill);
// The expression in parentheses evaluates to 20*2, i.e. 40
// and change -= 40; means same as
// change = change - 40;
// Value of change is 42, so (change - 40) evaluates to
// 42 - 40, i.e. to 2
// Thus, the statement is (this time) equivalent to
// change = 2;

// change equals to 2 
Now I finally get it, I didn't know you can update it like that. But I asked another guy about this, and he said I need to multiply the amount of money by 100. Once I converted it to int, it already lost the decimal part.

1
2
3
4
5
6
float amount; //put the amount you get from the user in this.

amount*=100; //Make it the number of cents.

int moneyToChange = amount; //Operate on this new int-type variable. You've lost accuracy below cents, but it is the number of cents.


And what I added was

1
2
3
4
5
6
7
8
9
10
11
12
float    amount;
cout << "Please enter your name: ";
	getline(cin, Name);
	cout << "\nHello" << " " << Name << ", " << "how much do you want to change?" << setw(20);
	cin >> amount;

	//******* Processing section *******
	amount *= 100;
	int change = amount;
	int hundreddollarbill = change / 100;

    change -= (100 * hundreddollarbill);


The program doesn't work correctly anymore, even if I try to replace change with amount in the processing section. What did I miss?
Last edited on
One hundred dollars is ten thousand pennies. You have to multiply all your constants by 100. You want integers.


The reason to operate with integers is that floating point values are not continuous; they are discrete and lead to unexpected rounding.
I just did this same problem, I was having rounding issues with my change even when multiplying by 100, and it made no sense, I read that using double for money is known to have issues and read a suggestion to multiply by 10,000 to remove rounding errors associated with doubles and moving doubles to ints.

I think 10,000 is a little overkill, but I didn't use double so I can't be sure.

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
amount *= 100;
	int change = amount;
	int hundreddollarbill = (change) / (100 * 100);

    change -= (100 * 100 * hundreddollarbill);

    int fiftydollarbill =  (change) / (100 * 50);

    change -= (50 * 100 * fiftydollarbill);

	int twentydollarbill = (change) / (100 * 20);

    change -= (20 * 100 * twentydollarbill);

	int tendollarbill = (change) / (100 * 10);

	change -= (10 * 100 * tendollarbill);

	int fivedollarbill = (change) / (100 * 5);

	change -= (5 * 100 * fivedollarbill);

	int twodollarbill = (change) / (100 * 2);

    change -= (2 * 100 * twodollarbill);

	int onedollarbill = (change) / (100 * 1);

    change -= (1 * 100 * onedollarbill);

    int quarters  =  (change) / (100 * .25);

    change -= (.25 * 100 * quarters);

    int dimes = (change) / (100 * .10);

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

    int nickels = (change) / (100 * .05);

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

    int pennies = change / (100 * .01);

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


This is what worked for me. :) By the way, since we are on this same problem, may I ask why my setw() won't work? I am trying to align the answers neatly in the center.

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << setw(42);
	cout << "\nYour change is: ";
	cout << setw(52) << "\nHundred dollar bills: " << setw(10) << hundreddollarbill; 
    cout << setw(42) << "\nFifty dollar bills: " << setw(12) << fiftydollarbill; 
    cout << setw(22) << "\nTwenty dollar bills: " << setw(10) << twentydollarbill;
    cout << setw(22) << "\nTen dollar bills: " << setw(12) << tendollarbill;
    cout << setw(22) << "\nFive dollar bills: " << setw(11) << fivedollarbill; 
	cout << setw(22) << "\nTwo dollar bills: " << setw(12) << twodollarbill;
	cout << setw(22) << "\nOne dollar bills: " << setw(12) << onedollarbill;
	cout << setw(22) << "\nQuarters: " << setw(21) << quarters; 
    cout << setw(22) << "\nDimes: " << setw(23) << dimes; 
    cout << setw(22) << "\nNickels: " << setw(22) << nickels; 
    cout << setw(22) << "\nPennies: " << setw(22) << pennies;
There are many formatting options. setw is only one of them.


1
2
int pennies = change / (100 * .01);
change -= (.01 * 100 * pennies);

You do realize that a simple int pennies = change; does about the same?

Are you sure that the compiler is clever enough to precompute the 100*.01, see that change/1 and pennies*1 are no-ops, and that it does not convert the temporary value to a double before storing it into the integer change? You do operate on pennies, because you don't want float math. The use of explicit floats ruins that goal.
Topic archived. No new replies allowed.