Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Nickelis Jarvis
- Date: 8.15.14
- Program Name: Tips and Pennies
- Description: Total up tips and split a piggy bank into the smallest amount of coinage.
- */
- #include <cmath>
- #include <iostream>
- #include <iomanip>
- #include <string>
- //This is used in the second part of the program, since we're counting by pennies, we need a global variable to define how many pennies are in the highest denomination.
- #define PENNIES_PER_DOLLAR 100
- using namespace std;
- int main() {
- //Create subtotal as double-precision floating-point. Create string for account name. No subtotal variable as it's not needed (no math performed after).
- double total = 0.0;
- std::string accountName;
- //Create a struct for money denominations, first two fields are constant after initialization.
- struct money_t {
- const char* name;
- const double value;
- int units;
- };
- //Define first 2 fields for each denomination (name and value), initialize units (number of) to 0
- struct money_t money[5] = {
- { "Dollars", 1.00, 0},
- { "Quarters", 0.25, 0},
- { "Dimes", 0.10, 0},
- { "Nickels", 0.05, 0},
- { "Pennies", 0.01, 0}
- };
- //Ask for account name
- cout << "Enter account holder's name: ";
- getline(cin, accountName);
- //Ask for number of each denomination and store in 3rd data place of structure (int units), loop through each denomination.
- for (int i = 0; i <= 4; i++) {
- cout << "Enter number of " << money[i].name << ": ";
- cin >> money[i].units;
- total += (money[i].units * money[i].value);
- }
- //Output account name and total deposit, set floating point precision to fixed and round to 2 most significant digits.
- cout << "Account Name: " << accountName << endl;
- cout << fixed << setprecision(2) << "Total Deposit: $" << total << endl;
- //Ask for number of pennies in bank.
- cout << "\n\nPiggy Bank" << endl;
- cout << "Enter how many pennies are in the bank: ";
- cin >> total;
- //Perform a division, followed by changing subtotal to the modulus for further iterations.
- //Multiply value up instead of divide subtotal; reverse causes rounding errors.
- for (int i = 0; i <= 4; i++) {
- money[i].units = total / (money[i].value * PENNIES_PER_DOLLAR);
- total = fmod(total, (money[i].value * PENNIES_PER_DOLLAR));
- cout << "Number of " << money[i].name << ": " << money[i].units << endl;
- }
- //Naturally, return no error code on successful execution.
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement