Change Calculator returning only the change amount
You are to write a program that computes the bills and coins to be
dispensed, minimizing the total number of bills and coins. (That is, for
change totaling $5.50, you should not dispense 5 ones and 50 pennies, but
a $5 bill and a 50-cent piece instead.) The bills and coins available for
you to dispense are as follows: $50 bill, $20 bill, $10 bill, $5 bill, $1
bill, 50-cent coin, 25-cent coin, 10-cent coin, 5-cent coin, 1-cent coin.
The console-based program, which can easily be converted to an internet
CGI program at some future date, prompts the user to input 2 numbers. The
first number is the amount of the purchase, and the second one is the
amount tendered by the customer. You may assume that the amount tendered
is greater than or equal to the amount of purchase. The console output
will be a series of lines showing the amount of change returned and
detailing the number of bills and coins that will be dispensed as change,
in descending order of monetary amount, one unit per line. If a bill/coin
is not needed in the change returned, no output is produced for that
bill/coin. (In other words, do not display "0 $1 bills".) Proper use of
plurals is required, as shown below.
Here is an example -- for a purchase of 42.15, the amount tendered is 50.
There are no $'s or commas in the input -- just positive real numbers that
may or may not contain a decimal. Here is the output:
$7.85
1 $5 bill
2 $1 bills
1 50-cent coin
1 25-cent coin
1 10-cent coin
The program terminates after the output is produced.
Can someone please help me how to make a loop so the program can count how
many 50's or 20's it needs to print back out?
#include <iomanip>
using std::setprecision;
#include <cstdlib>
#include <string>
using std::string;
int main()
{
int dollar50;
int dollar20;
double purchase = 0.0;
double tendered = 0.0;
double change = 0.0;
string buf;
//ask use to enter money owened and money paid
cout<<" Please enter the amount of the purchase, and the amount tendered
\n";
cin>>buf; purchase = atof (buf.c_str());
cin>>buf; tendered = atof (buf.c_str());
cin.ignore (1000, 10);
if (purchase > tendered)
{
cout<<"The amount that was given is not enough, please pay the full
amount.";
cout << endl << endl;
}
else
{
cout.setf(ios::fixed | ios::showpoint);
cout << setprecision(2) << endl;
change = tendered - purchase;
change1 = (int) (change * 100 + 0.5);
dollar50 = change1 / 5000 % 5000;
dollar20 = change1 / 2000 % 2000;
// The loop I tired is not given the correct output
while (true){
if ( change >= 50 )
{
dollar50++;
change -=dollar50;
cout<< dollar50 <<" "<<sign<< "50 bill"<<endl;
}break;
if ( change >= 20 )
{
dollar20++;
change -=dollar20;
cout<< dollar20 <<" "<<sign<< "20 bill"<<endl;
}break;
}
//I wanna put this part in a loop so it can count how many 50's or
20 has to be returned
/*cout << dollar50 << sign << " 50 Bills" << endl;
cout << dollar20 << sign << " 20 Bills" << endl;*/
}
}
No comments:
Post a Comment