1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <iomanip>
using namespace std;
struct Denom {
const char *name;
int amount;
} denoms[] = {
{"Fifties", 5000},
{"Twenties", 2000},
{"Tens", 1000},
{"Fives", 500},
{"Ones", 100},
{"Quarters", 25},
{"Dimes", 10},
{"Nickels", 5},
{"Pennies", 1},
{nullptr, 0}
};
int main() {
double cost = 0, tendered = 0;
cout << fixed << setprecision(2);
cout << "Enter amount of purchase and amount tendered: ";
cin >> cost >> tendered;
int change = (tendered - cost) * 100 + 0.5;
cout << "For your purchase of $" << cost << " you tendered $" << tendered << endl;
cout << "Your change is $" << change / 100 << '.' << change % 100 << endl;
for (int i = 0; denoms[i].name; i++) {
cout << setw(8) << left << denoms[i].name << ": "
<< setw(4) << change / denoms[i].amount << '\n';
change %= denoms[i].amount;
}
return 0;
}
|