I think I'm mostly stuck on the math, but I can't figure out what I'm doing wrong. I'm trying to make a program that will let the user enter an amount of money and get the fewest amount of bills that adds up to the number back. Program doesn't have to be unbreakable and shouldn't take integers but also doesn't tell the user when they entered an integer.
I've looked at videos online, other forums... I just can't find anything that deals with just dollars, not dollars and coins.
The big problem is that this program does tell how much money is needed for each dollar, but the numbers don't consider each other and add up correctly.
double money;
int result;
cout << "Enter a dollar amount: ";
cin >> money;
result = money;
cout << "The fewest number of bills to get " << money << " is:\n";
cout << "=========================================\n";
result = result % 20;
cout << "$20 bills: " << result / 20 << "\n";
result = result % 10;
cout << "$10 bills: " << result / 10 << "\n";
result = result % 5;
cout << "$5 bills : " << result / 5 << "\n";
result = result % 1;
cout << "$1 bills : " << result / 1 << "\n";
Line 13: You throw out all the $20 bills. You will have at most $19 left.
Line 14: You show how many $20 bills are in $19 (or less). 0 every time, isn't it?
In other words, the order of operations has to change.
I've looked at videos online, other forums... I just can't find anything that deals with just dollars, not dollars and coins.
This is a simple problem that you are meant to solve by thinking about it. Programming is thinking. Memorising the syntax is just learning how to write; programming is thinking.
This problem is particularly easy to think about because it's modelling something you can actually do yourself. How would you do it? If I asked you to give me 84 dollars in as few bills as possible, how do YOU work out the answer? Then code that.
double money;
int result;
cout << "Enter a dollar amount: ";
cin >> money;
result = money;
cout << "The fewest number of bills to get " << money << " is:\n";
cout << "=========================================\n";
cout << "$20 bills: " << result / 20 << "\n";
result = result % 20;
cout << "$10 bills: " << result / 10 << "\n";
result = result % 10;
cout << "$5 bills : " << result / 5 << "\n";
result = result % 5;
cout << "$1 bills : " << result / 1 << "\n";
result = result % 1;