steamengines

PA - Phone Bill

Feb 26th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. // Christian Ballard
  2. // COSC 1336 - 001
  3. // February 18th, 2015
  4. // This program should take the users information and display the customers name,
  5. // account number, service type, and the amount that is due.
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <climits>
  10. #include <iomanip>
  11. using namespace std;
  12.  
  13. // Prototypes
  14. int readInt(string errorMsg = "", int min = INT_MIN, int max = INT_MAX);
  15. void getData(string&, int&, char&, int&);
  16. char getChoice(string prompt, char char1, char char2);
  17. double processData(char service);
  18. void display(string, int, char, double);
  19.  
  20.  
  21. // Regular service
  22. const double RCHARGE = 10.00;            // Minimum charge for regular service.
  23. const double RPERMIN = 0.20;             // The charge per minutes after 50.
  24. const int REG_MINS = 50;                         // The number of free minutes allowed for regular service.
  25. // Premium service
  26. const double PCHARGE = 25.00;            // Minimum charge for premium service.
  27. const double P_PER_MIN_DAY = 0.10;       // Charge per day minutes after 75.
  28. const double P_PER_MIN_NIGHT = 0.05; // Charge per night minutes after 100.
  29. const int P_DAY = 75;                            // Number of free minutes allowed during the day.
  30. const int P_NIGHT = 100;                         // Number of free minutes allowed during the night.
  31.  
  32. int main()
  33. {
  34.     string name;            // Stores the users name.
  35.     int accountNumber;  // Stores the users account number.
  36.     char serviceType;   // Stores the users service type.
  37.     int minutes;            // Stores the users minutes.
  38.     bool loopies = true;// Controls the loop while condition is true.
  39.  
  40.  
  41.     while (loopies == true)
  42.     {
  43.         getData(name, accountNumber, serviceType, minutes);
  44.     }
  45.  
  46.  
  47.     return 0;
  48. }
  49. // Checks ints to make sure they are valid.
  50. int readInt(string errorMsg, int min, int max)
  51. {
  52.     int someInt;            // Stores each integer entered
  53.     bool valid = false; // Loop control flag indicating validity of entry
  54.     // initially assuming not valid.
  55.     do
  56.     {
  57.         // Read an integer delimited by a whitespace character
  58.         cin >> someInt;
  59.  
  60.         // Valid if within min to max range.
  61.         valid = someInt >= min && someInt <= max;
  62.  
  63.         // Detect and remove invalid input
  64.         if (cin.fail() || !valid || !isspace(cin.peek()))
  65.         {
  66.             cout << "\nError! Invalid input.\n" << errorMsg << " must be a whole "
  67.                 << "number between " << min << " and " << max << ". \nTry Again: ";
  68.  
  69.             cin.clear();            // Clears the fail state.
  70.  
  71.             // Remove up to max number of chars in input stream or up to '\n'.
  72.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  73.  
  74.             valid = false;          // No valid entry has been read.
  75.         }
  76.  
  77.     } while (!valid); // Loop again, prompting user to re-enter a valid integer.
  78.  
  79.     // When valid input received, remove from input stream up to 100 remaining
  80.     // characters or until reaches the '\n' <ENTER> character.
  81.     cin.ignore(100, '\n');
  82.  
  83.     return someInt; // Returns a valid integer to calling environment.
  84. }
  85. // Takes all user data and checks input.
  86. void getData(string &name, int &account, char &service, int &bill)
  87. {
  88.     cout << "Enter customer name or \"Exit\" to end program: ";
  89.     getline(cin, name);
  90.  
  91.     if (name == "Exit" || name == "EXIT" || name == "exit")
  92.     {
  93.         cout << "Program closing..." << endl;
  94.         system("pause");
  95.         exit(EXIT_SUCCESS);
  96.     }
  97.  
  98.     cout << "Enter account number: ";
  99.     account = readInt("Account", 0, 10000);
  100.  
  101.     cout << "Enter service type (R for regular or P for premium): ";
  102.     service = getChoice("Service", 'r', 'p' );
  103.  
  104.     cout << fixed << setprecision(2);
  105.  
  106.     bill = processData(service);
  107.  
  108.     display(name, account, service, bill);
  109.  
  110.  
  111. }
  112.  
  113. char getChoice(string input, char char1, char char2)
  114. {
  115.     char character;     // Stores the users character.
  116.     bool valid = false; // Controls the loop.
  117.  
  118.     do
  119.     {
  120.  
  121.         cout << input << " (" << char1 << " or " << char2 << "): ";
  122.         cin >> character;
  123.         character = tolower(character);
  124.  
  125.  
  126.         valid = (character == char1 || character == char2);
  127.  
  128.  
  129.         if (!valid || !isspace(cin.peek())) {
  130.             cout << "Invalid input!" << endl;
  131.             cout << input << " needs to be " << char1 << " or " << char2 << endl;
  132.             cout << "Re-enter ";
  133.             cin.clear();
  134.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  135.  
  136.             valid = false;
  137.         }
  138.     } while (!valid);
  139.  
  140.     cin.ignore(100, '\n');
  141.  
  142.     return character;
  143. }
  144.  
  145. double processData(char service)
  146. {
  147.     int dayMin;              // Stores the users day minutes.
  148.     int nightMin;    // Stores the users night minutes.
  149.     double totalPay; // Stores the total pay.
  150.     double dayPay;   // Stores the pay for day minutes.
  151.     double nightPay; // Stores the pay for night minutes.
  152.  
  153.     if (service == 'r')
  154.     {
  155.         cout << "Enter minutes used: ";
  156.         cin >> dayMin;
  157.  
  158.         if (dayMin > REG_MINS)
  159.         {
  160.             dayMin -= REG_MINS;
  161.             dayMin = (dayMin * RPERMIN) + RCHARGE;
  162.             totalPay = dayMin;
  163.         }
  164.         else
  165.         {
  166.             totalPay = RCHARGE;
  167.         }
  168.     }
  169.     else
  170.     {
  171.         cout << "Enter day minutes: ";
  172.         cin >> dayMin;
  173.         if (dayMin > P_DAY)
  174.         {
  175.             dayMin -= P_DAY;
  176.             dayMin = (dayMin * P_PER_MIN_DAY) + PCHARGE;
  177.             dayPay = dayMin;
  178.         }
  179.         else
  180.         {
  181.             dayPay = PCHARGE;
  182.         }
  183.  
  184.         cout << "Enter night minutes: ";
  185.         cin >> nightMin;
  186.         if (nightMin > P_NIGHT)
  187.         {
  188.             nightMin -= P_NIGHT;
  189.             nightMin = (nightMin * P_PER_MIN_NIGHT) + PCHARGE;
  190.             nightPay = nightMin;
  191.         }
  192.         else
  193.         {
  194.             nightPay = PCHARGE;
  195.         }
  196.         totalPay = nightPay + dayPay - PCHARGE;
  197.     }
  198.    
  199.  
  200.     return totalPay;
  201. }
  202.  
  203. void display(string name, int account, char service, double bill)
  204. {
  205.     if(service == 'r')
  206.     {
  207.         cout << endl;
  208.         cout << "Customer Name: " << name << endl;
  209.         cout << "Account number: " << account << endl;
  210.         cout << "Service type: " << "Regular" << endl;
  211.         cout << "Amount due: $" << bill << endl;
  212.     }
  213.     else if (service == 'p')
  214.     {
  215.         cout << endl;
  216.         cout << "Customer Name: " << name << endl;
  217.         cout << "Account number: " << account << endl;
  218.         cout << "Service type: " << "Premium" << endl;
  219.         cout << "Amount due: $" << bill << endl;
  220.     }
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment