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:
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?
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.
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.)
He does not "have many". He updates the value of one variable multiple times.
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
cout << "Please enter your 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?
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.
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.