A program the dispenses change. The program should read the amount and then display the number of dollars, quarters, dimes, nickels, and pennies. Use the % operator to determine how many dollars to pay. Subtract that number from the change due and and continue the process for quarters, dimes etc.
Can someone write this C++ program for me?
#include %26lt;iostream.h%26gt;
int
make_change(int cents, int coin_val, const char* coin_string)
{
if (cents %26gt;= coin_val) {
int num_coins = cents / coin_val;
cents -= (num_coins * coin_val);
cout %26lt;%26lt; num_coins %26lt;%26lt; " " %26lt;%26lt; coin_string %26lt;%26lt; "\n";
}
return cents;
}
int
main(int argc, char* argv[])
{
double amount = argc%26gt;1 ? atof(argv[1]) : 0.0;
int in_pennies = amount * 100.0;
int cents = in_pennies % 100;
int dollars = (in_pennies - cents) / 100;
if (dollars %26gt; 0)
cout %26lt;%26lt; dollars %26lt;%26lt; " dollars\n";
cents = make_change(cents, 25, "quarters");
cents = make_change(cents, 10, "dimes");
cents = make_change(cents, 5, "nickels");
cents = make_change(cents, 1, "pennies");
return 0;
}
$ CC change.C
$ a.out 11.73
11 dollars
2 quarters
2 dimes
3 pennies
Reply:Sorry i suck at computer programming! Only class i ever dropped in college!
statice
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment