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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int dollars,quarters,dimes,nickels,pennies;
double due,payment,change;
// Get the amount due
cout << "Please enter the price:\n";
cin >> due;
// Check the amount due for problems
while (due <= 0.01) {
// Somebody entered zero or less, probably trying to get some free money
cout << "Sorry cheapskate, you must spend at least $0.01\n\n";
// Ask again for the real price
cout << "Please enter the ACTUAL price:\n";
cin >> due;
cout << endl;
}
// Get the payment amount
cout << "Please provide payment amount (using numbers only):\n";
cin >> payment;
// Check payment amount for problems
while (payment < due) {
// Somebody is trying to pay less than what's due
// but we're not accepting ANY money unless we get it all at once
cout << "No, you owe $" << due << ", not $" << payment << ".\n\n";
cout << endl << "Please provide FULL payment:\n";
cin >> payment;
}
if (payment > due){
// They paid more than what's due, time to give out change
change = payment - due;
cout << "----------------------------------------------\n";
cout << "Your change is $" << change << endl;
cout << "You get ";
// dollars
for (dollars = 0; change >= 1.00; dollars++){
change -= 1.00;
//cout << dollars << " dollars, ";
// quarters
for (quarters = 0; change >= 0.25; quarters++){
change -= 0.25;
//cout << quarters << " quarters, ";
// dimes
for (dimes = 0; change >= 0.10; dimes++){
change -= 0.10;
//cout << dimes << " dimes, ";
// nickels
for (nickels = 0; change >= 0.05; nickels++){
change -= 0.05;
//cout << nickels << " nickels, ";
// pennies, just for good measure
for (pennies = 0; change > 0.00; pennies++){
change -= 0.01;
//cout << pennies << " pennies.\n\n";
for (;change > 0;){
cout << dollars << " dollars, ";
cout << quarters << " quarters, ";
cout << dimes << " dimes, ";
cout << nickels << " nickels, ";
cout << pennies << " pennies.\n\n";
}
else {
// They must have given exact change
cout << "Thanks for using exact change!\n";
}
}
}
}
}
}
return 0;
}
|