Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Christian Ballard
- // COSC 1336 - 001
- // 4/5/15
- // Writing to an Output File
- #include "genericCDB.h"
- #include <iostream>
- #include <string>
- #include <ctime>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- // Forward declarations
- void getData(string&, int&, char&);
- int readInt(string = "", int = INT_MIN, int = INT_MAX);
- char getChoice(string, char char1, char char2);
- double calculate(char serviceType);
- void display(string name, int displayAccount, string displayServiceType, double displayServiceCharge);
- void writeBillingInfoToFile(ofstream& fileOut, string custName, int acctNo, char servType);
- int main()
- {
- // Variable declarations
- string name; // Collects the customer name
- int accountNum; // Collects the customer account number
- char serviceType; // Collects customer service type
- bool loop = true; // Keeps the program indefinitely running until prompted to exit
- char sentinial;
- ofstream fout; // fout is a user-defined object of type ofstream
- fout.open("Phone Bill Records.txt"); // attach the file stream to the physical file
- if (fout.fail()) // if the file is not opened successfully;
- {
- cout << "\nError opening file for writing.\n";
- return EXIT_FAILURE; // terminate program from main with returned value of 1
- }
- cout << "Welcome! please type the customer name to continue, " << endl << "or type \"exit\" to end the program." << endl << endl;
- // BEGIN LOOP
- while (loop == true) {
- // Entering the program
- getData(name, accountNum, serviceType);
- sentinial = getChoice("Would you like to save the record to a file? ", 'Y', 'N');
- writeBillingInfoToFile(fout, name, accountNum, serviceType);
- }
- // END LOOP
- fout.close();
- return 0;
- }
- // Program body
- void getData(string& name, int& account, char& service)
- {
- cout << "Customer name: ";
- getline(cin, name);
- if (name == "Exit" || name == "exit" || name == "EXIT") {
- cout << "Program terminated by user, good-bye." << endl;
- system("PAUSE");
- exit(EXIT_SUCCESS);
- // Exits the program
- }
- // Data processing
- cout << "Enter account number: ";
- // Making sure the account number is between 0 and 9999
- account = readInt("Account number", 0, 9999);
- // Making sure the service type is either an R or a P
- service = getChoice("Service type", 'r', 'p');
- // Randomly generates minutes and calculates the total charge
- double serviceCharge = calculate(service);
- // Being passed into display function for aesthetic appeal
- string serviceName;
- if (service == 'p') {
- serviceName = "Premium";
- }
- else {
- serviceName = "Regular";
- }
- cout << endl;
- // Displays the final billing information for the customer
- display(name, account, serviceName, serviceCharge);
- }
- // Validates the account number
- int readInt(string errorMsg, int min, int max)
- {
- int someInt;
- bool valid = false;
- do
- {
- // User entering account number
- cin >> someInt;
- // Verifying the data is valid (0 to 9999)
- valid = (someInt >= min && someInt <= max);
- // Clearing out invalid data and prompting for re-entry
- if (cin.fail() || !valid || !isspace(cin.peek()))
- {
- cout << "Error! Invalid input." << endl;
- cout << errorMsg << " must be whole number between " << min << " and " << max << endl;
- cout << "Try Again: ";
- cin.clear(); // Clearing the fail state
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false;
- }
- } while (!valid);
- // Clears out following incorrect data once correct
- cin.ignore(100, '\n');
- return someInt;
- }
- // Generates minutes and calculates the final charges
- double calculate(char serviceType)
- {
- // Set amounts to be paid
- double payRegular = 10.0, payPremium = 25.0;
- int dayMinutes, // Holds random day minutes
- nightMinutes; // Holds random night minutes
- double finalPay; // Used to return final pay post-processing
- // seeding the random number generator
- srand(time(NULL));
- // Making sure decimals are displayed correctly
- cout << fixed << setprecision(2);
- // randomly generated minute range between 0 and 300. completely arbitrary ceiling
- dayMinutes = 0 + rand() % ((300 - 0) + 1);
- nightMinutes = 0 + rand() % ((300 - 0) + 1);
- // Checking and calculating Regular service
- if (serviceType == 'r') {
- // Day minutes used as standard minutes on regular service
- cout << "Minutes Used: " << dayMinutes << endl;
- if (dayMinutes > 50) { // Checking if overcharge is necessary
- dayMinutes -= 50; // Extracting amount to be charged
- // Applying fee
- payRegular = (dayMinutes * .2) + payRegular;
- // Finalizing payment due
- finalPay = payRegular;
- }
- }
- // Checking and calculating Premium service
- else if (serviceType == 'p') {
- cout << "Day Minutes: " << dayMinutes << endl;
- cout << "Night Minutes: " << nightMinutes << endl;
- if (dayMinutes > 75) // Checking to see if overcharge is necessary
- dayMinutes -= 75; // Pulls minutes to be charged overtime
- else
- dayMinutes = 0; // Applies standard charge if minutes not exceeded
- if (nightMinutes > 100) // Checking if overcharge is necessary
- nightMinutes -= 100; // Pulling minutes to be charged overtime
- // Calculating the final pay based on pre-determined charges depending
- // on using more minutes than allowed.
- // Bases off day cycle as well
- finalPay = (dayMinutes * 0.1) + (nightMinutes * 0.05) + payPremium;
- }
- return finalPay;
- }
- void display(string displayName, int displayAccount, string displayServiceType, double displayServiceCharge) {
- // Displaying final bill
- cout << "Customer Name: " << displayName << endl;
- cout << "Account Number: " << displayAccount << endl;
- cout << "Service Type: " << displayServiceType << endl;
- cout << "Service Charge: " << displayServiceCharge << endl << endl;
- system("PAUSE");
- // Clear screen
- system("CLS");
- cout << "(type EXIT to close program)" << endl;
- }
- void writeBillingInfoToFile(ofstream& fileOut, string custName, int acctNo, char servType)
- {
- fileOut << custName << '\t' << acctNo << '\t' << servType << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment