-
February 20th, 2013, 03:08 PM
#1
Coin/Money change code doesn't give exact change!
My coin/money change code works when there can be an exact change each time, i.e. when the 1 cent option is available. However, when the change options are only $10, $5, $1, 25 cents and 10 cents, it does not give me what I want for instance, I wanted to get change for $237.80, I was expecting to get:
23 10's, one 5, two 1's and 8 dimes. However, the code below is giving me 23 10's, one 5, two 1's and 3 quarters (there is no option left for the 5 remaining cents).
Any idea on how to fix it?
Code:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void change(double cents, int a[]);
int main()
{
double Dollars;
double cents;
// int a[]={25,10,5,1}; denominations of coins, could have been any numbers in DECREASING order.
int a[]={1000,500,100,25,10};
cout<<"Please enter an amount in the format $x.xx: ";
cin>> Dollars;
cents=100*Dollars;
cout<<endl<<Dollars<<" dollars is equal to "<<cents<<" cents"<<endl<<endl;
change(cents, a);
cin.get();
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
void change(double cents, int a[]){
int money=cents;
double coins[4];
cout<<"The change is respectively [";
for(int i=0;i<5;i++){
double remainder=money%a[i];
coins[i]=(money-remainder)/a[i];
money=remainder;
cout<<coins[i]<<" ";
}
//cout<<"] quarters, dimes, nickels and pennies."<<endl<<endl;
cout<<"] 10's, 5's, 1's, quarters and dimes."<<endl<<endl;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|