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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
unsigned change = time(0);
int quarters = 0;
int dime = 0;
int nickel = 0;
int penny = 0;
int quartervalue = 0;
int dimevalue = 0;
int nickelvalue = 0;
int pennyvalue = 0;
// Get a random number from 0-100
srand(change);
change = change % 100;
// If change is greater than or equal to one run the program
if (change >= 1)
{
//If change is greater than or equal to 10 skip decimal fix
if (change >= 10)
{
cout << "Change Due:" << setw(6) << "$0." << change << "\n";
cout << "Coin Dispenser will dispense:\n";
}
// Decimal fix for under 10 cents.
else
{
cout << "Change Due:" << setw(6) << "$0.0" << change << "\n";
}
// If change is greater than or equal to 25, get quarters, adjust change, and get the quarter value.
if (change >= 25)
{
quarters = change / 25;
change = change % 25;
quartervalue = quarters * 25;
}
// If change is greater than or equal to 10, get dimes, adjust change, and get the dime value.
if (change >= 10)
{
dime = change / 10;
change = change % 10;
dimevalue = dime * 10;
}
// If change is greater than or equal to 5, get nickels, adjust change, and get the nickel value.
if (change >= 5)
{
nickel = change / 5;
change = change % 5;
nickelvalue = nickel * 5;
}
// If change is less than or equal to 4, get pennies, get value.
if (change <= 4)
{
penny = change / 1;
pennyvalue = penny * 1;
}
if (quartervalue == 0)
{
// If quartervalue is equal to 0, don't display.
}
else
{
cout << " Quarters: " << quarters << " (" << "$0." << quartervalue << ")" << "\n";
}
if (dimevalue == 0)
{
// If dimevalue is = to 0, don't display.
}
else
{
cout << " Dimes:" << setw(5) << dime << " (" << "$0." << dimevalue << ")" << "\n";
}
if (nickelvalue == 0)
{
// If nickelvalue is = to 0, don't display.
}
// If we only have a single nickle, we fix the decimal point output.
else if (nickelvalue == 5)
{
cout << " Nickels:" << setw(3) << nickel << " (" << "$0.0" << nickelvalue << ")" << "\n";
}
else
{
cout << " Nickels:" << setw(3) << nickel << " (" << "$0." << nickelvalue << ")" << "\n";
}
if (pennyvalue == 0)
{
// If pennyvalue is = to 0, don't display.
}
else
{
cout << setw(2) << " Pennies:" << setw(3) << penny << " (" << "$0.0" << pennyvalue << ")" << "\n";
}
}
// If change is = to 0, don't run the entire program, just run this portion.
else
{
cout << "Change Due" << "$0.00" << "\n";
cout << "Coin Dispenser will dispense:\n";
cout << "No Coins\n";
}
}
|