# make change program help

• 02-19-2008
Rob4226
make change program help
Hi,
I need some help making a program that outputs change in the least amount of dollars and coins. Here is what I have so far but it doesnt work correctly and I don't know why. Thanks.
Code:

```#include <iostream> using namespace std; int main() {   double charged, given, difference;   cout << "Enter amount charged:\n";   cin >> charged;   cout << "Enter amount given:\n";   cin >> given;   difference = given - charged;   double  money[10] = {100,50,20,10,5,1,.25,.10,.05,.01};   double  change[10] = {0,0,0,0,0,0,0,0,0,0};   for(int i =0; i<10; i++)   {     while(money[i] < difference)     {       change[i]++;       difference -=  money[i];     }   }           for(int  i = 0; i<10; i++)     if(change[i] != 0)     cout << "\$" << money[i] << ": " << change[i] << "\n";     }```
• 02-19-2008
anon
You are very close.

But I wonder what would be the reason that I get this output:
Quote:

Enter amount charged:
5
Enter amount given:
10
\$1: 4
\$0.25: 3
\$0.1: 2
\$0.01: 4
I mean, just why it wouldn't return 5 1-dollar bills (or 1 five-dollar bill for that matter)?! What makes it ignore the five-dollar bill, stop at 4 one-dollar bills and move on to smaller values?!
• 02-20-2008
hk_mp5kpdw
Code:

`while(money[i] < difference)`
• 02-20-2008
Rob4226
Does any one know why this doesnt work? Its suppose to return the least amount of bills and coins possible.
• 02-20-2008
vart
yes, people know and give you hints. It is now upto you to catch these hints
• 02-20-2008
Rob4226
What hints are you talking about?
• 02-20-2008
vart
What will happen in a line noted by hk_mp5kpdw
If you pay with 10\$ bill for 5\$ purchase as in a sample anon suggested you to debug?
• 02-20-2008
Rob4226
<=, thank you!
• 02-20-2008
anon
Good job! You were apparently missing a single byte of source code for perfection :)