Advertisement
hfxdesign

Tips and Pennies

Sep 15th, 2014
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. /*
  2. Name:           Nickelis Jarvis
  3. Date:           8.15.14
  4. Program Name:   Tips and Pennies
  5. Description:    Total up tips and split a piggy bank into the smallest amount of coinage.
  6. */
  7.  
  8. #include <cmath>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <string>
  12.  
  13. //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.
  14. #define PENNIES_PER_DOLLAR 100
  15.  
  16.  
  17. using namespace std;
  18.  
  19. int main() {
  20.  
  21.     //Create subtotal as double-precision floating-point. Create string for account name. No subtotal variable as it's not needed (no math performed after).
  22.     double total = 0.0;
  23.     std::string accountName;
  24.  
  25.     //Create a struct for money denominations, first two fields are constant after initialization.
  26.     struct money_t {
  27.         const char* name;
  28.         const double value;
  29.         int units;
  30.     };
  31.  
  32.     //Define first 2 fields for each denomination (name and value), initialize units (number of) to 0
  33.      struct money_t money[5] = {
  34.         { "Dollars",    1.00,   0},
  35.         { "Quarters",   0.25,   0},
  36.         { "Dimes",      0.10,   0},
  37.         { "Nickels",    0.05,   0},
  38.         { "Pennies",    0.01,   0}
  39.      };
  40.  
  41.     //Ask for account name
  42.     cout << "Enter account holder's name: ";
  43.     getline(cin, accountName);
  44.  
  45.     //Ask for number of each denomination and store in 3rd data place of structure (int units), loop through each denomination.
  46.     for (int i = 0; i <= 4; i++) {
  47.         cout << "Enter number of " << money[i].name << ": ";
  48.         cin >> money[i].units;
  49.         total += (money[i].units * money[i].value);
  50.     }
  51.  
  52.     //Output account name and total deposit, set floating point precision to fixed and round to 2 most significant digits.
  53.     cout << "Account Name: " << accountName << endl;
  54.     cout << fixed << setprecision(2) << "Total Deposit: $" << total << endl;
  55.  
  56.      //Ask for number of pennies in bank.
  57.     cout << "\n\nPiggy Bank" << endl;
  58.     cout << "Enter how many pennies are in the bank: ";
  59.     cin >> total;
  60.  
  61.     //Perform a division, followed by changing subtotal to the modulus for further iterations.
  62.     //Multiply value up instead of divide subtotal; reverse causes rounding errors.
  63.     for (int i = 0; i <= 4; i++) {
  64.         money[i].units = total / (money[i].value * PENNIES_PER_DOLLAR);
  65.         total = fmod(total, (money[i].value * PENNIES_PER_DOLLAR));
  66.         cout << "Number of " << money[i].name << ": " << money[i].units << endl;
  67.     }
  68.  
  69.     //Naturally, return no error code on successful execution.
  70.     return(0);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement