Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Christian Ballard
- // COSC 1336 - 001
- // February 18th, 2015
- // This program should take the users information and display the customers name,
- // account number, service type, and the amount that is due.
- #include <iostream>
- #include <string>
- #include <climits>
- #include <iomanip>
- using namespace std;
- // Prototypes
- int readInt(string errorMsg = "", int min = INT_MIN, int max = INT_MAX);
- void getData(string&, int&, char&, int&);
- char getChoice(string prompt, char char1, char char2);
- double processData(char service);
- void display(string, int, char, double);
- // Regular service
- const double RCHARGE = 10.00; // Minimum charge for regular service.
- const double RPERMIN = 0.20; // The charge per minutes after 50.
- const int REG_MINS = 50; // The number of free minutes allowed for regular service.
- // Premium service
- const double PCHARGE = 25.00; // Minimum charge for premium service.
- const double P_PER_MIN_DAY = 0.10; // Charge per day minutes after 75.
- const double P_PER_MIN_NIGHT = 0.05; // Charge per night minutes after 100.
- const int P_DAY = 75; // Number of free minutes allowed during the day.
- const int P_NIGHT = 100; // Number of free minutes allowed during the night.
- int main()
- {
- string name; // Stores the users name.
- int accountNumber; // Stores the users account number.
- char serviceType; // Stores the users service type.
- int minutes; // Stores the users minutes.
- bool loopies = true;// Controls the loop while condition is true.
- while (loopies == true)
- {
- getData(name, accountNumber, serviceType, minutes);
- }
- return 0;
- }
- // Checks ints to make sure they are valid.
- int readInt(string errorMsg, int min, int max)
- {
- int someInt; // Stores each integer entered
- bool valid = false; // Loop control flag indicating validity of entry
- // initially assuming not valid.
- do
- {
- // Read an integer delimited by a whitespace character
- cin >> someInt;
- // Valid if within min to max range.
- valid = someInt >= min && someInt <= max;
- // Detect and remove invalid input
- if (cin.fail() || !valid || !isspace(cin.peek()))
- {
- cout << "\nError! Invalid input.\n" << errorMsg << " must be a whole "
- << "number between " << min << " and " << max << ". \nTry Again: ";
- cin.clear(); // Clears the fail state.
- // Remove up to max number of chars in input stream or up to '\n'.
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false; // No valid entry has been read.
- }
- } while (!valid); // Loop again, prompting user to re-enter a valid integer.
- // When valid input received, remove from input stream up to 100 remaining
- // characters or until reaches the '\n' <ENTER> character.
- cin.ignore(100, '\n');
- return someInt; // Returns a valid integer to calling environment.
- }
- // Takes all user data and checks input.
- void getData(string &name, int &account, char &service, int &bill)
- {
- cout << "Enter customer name or \"Exit\" to end program: ";
- getline(cin, name);
- if (name == "Exit" || name == "EXIT" || name == "exit")
- {
- cout << "Program closing..." << endl;
- system("pause");
- exit(EXIT_SUCCESS);
- }
- cout << "Enter account number: ";
- account = readInt("Account", 0, 10000);
- cout << "Enter service type (R for regular or P for premium): ";
- service = getChoice("Service", 'r', 'p' );
- cout << fixed << setprecision(2);
- bill = processData(service);
- display(name, account, service, bill);
- }
- char getChoice(string input, char char1, char char2)
- {
- char character; // Stores the users character.
- bool valid = false; // Controls the loop.
- do
- {
- cout << input << " (" << char1 << " or " << char2 << "): ";
- cin >> character;
- character = tolower(character);
- valid = (character == char1 || character == char2);
- if (!valid || !isspace(cin.peek())) {
- cout << "Invalid input!" << endl;
- cout << input << " needs to be " << char1 << " or " << char2 << endl;
- cout << "Re-enter ";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false;
- }
- } while (!valid);
- cin.ignore(100, '\n');
- return character;
- }
- double processData(char service)
- {
- int dayMin; // Stores the users day minutes.
- int nightMin; // Stores the users night minutes.
- double totalPay; // Stores the total pay.
- double dayPay; // Stores the pay for day minutes.
- double nightPay; // Stores the pay for night minutes.
- if (service == 'r')
- {
- cout << "Enter minutes used: ";
- cin >> dayMin;
- if (dayMin > REG_MINS)
- {
- dayMin -= REG_MINS;
- dayMin = (dayMin * RPERMIN) + RCHARGE;
- totalPay = dayMin;
- }
- else
- {
- totalPay = RCHARGE;
- }
- }
- else
- {
- cout << "Enter day minutes: ";
- cin >> dayMin;
- if (dayMin > P_DAY)
- {
- dayMin -= P_DAY;
- dayMin = (dayMin * P_PER_MIN_DAY) + PCHARGE;
- dayPay = dayMin;
- }
- else
- {
- dayPay = PCHARGE;
- }
- cout << "Enter night minutes: ";
- cin >> nightMin;
- if (nightMin > P_NIGHT)
- {
- nightMin -= P_NIGHT;
- nightMin = (nightMin * P_PER_MIN_NIGHT) + PCHARGE;
- nightPay = nightMin;
- }
- else
- {
- nightPay = PCHARGE;
- }
- totalPay = nightPay + dayPay - PCHARGE;
- }
- return totalPay;
- }
- void display(string name, int account, char service, double bill)
- {
- if(service == 'r')
- {
- cout << endl;
- cout << "Customer Name: " << name << endl;
- cout << "Account number: " << account << endl;
- cout << "Service type: " << "Regular" << endl;
- cout << "Amount due: $" << bill << endl;
- }
- else if (service == 'p')
- {
- cout << endl;
- cout << "Customer Name: " << name << endl;
- cout << "Account number: " << account << endl;
- cout << "Service type: " << "Premium" << endl;
- cout << "Amount due: $" << bill << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment