# Thread: make change program help

1. ## 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";

}```

2. You are very close.

But I wonder what would be the reason that I get this output:
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?!

3. Code:
`while(money[i] < difference)`

4. Does any one know why this doesnt work? Its suppose to return the least amount of bills and coins possible.

5. yes, people know and give you hints. It is now upto you to catch these hints

6. What hints are you talking about?

7. 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?

8. <=, thank you!

9. Good job! You were apparently missing a single byte of source code for perfection

Popular pages Recent additions