steamengines

writing to a file

May 5th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.09 KB | None | 0 0
  1. // Christian Ballard
  2. // COSC 1336 - 001
  3. // 4/5/15
  4. // Writing to an Output File
  5.  
  6. #include "genericCDB.h"
  7. #include <iostream>
  8. #include <string>
  9. #include <ctime>
  10. #include <iomanip>
  11. #include <fstream>
  12.  
  13. using namespace std;
  14.  
  15. // Forward declarations
  16. void getData(string&, int&, char&);
  17. int readInt(string = "", int = INT_MIN, int = INT_MAX);
  18. char getChoice(string, char char1, char char2);
  19. double calculate(char serviceType);
  20. void display(string name, int displayAccount, string displayServiceType, double displayServiceCharge);
  21. void writeBillingInfoToFile(ofstream& fileOut, string custName, int acctNo, char servType);
  22.  
  23.  
  24. int main()
  25. {
  26.     // Variable declarations
  27.     string name;            // Collects the customer name
  28.     int accountNum;         // Collects the customer account number
  29.     char serviceType;       // Collects customer service type
  30.     bool loop = true;       // Keeps the program indefinitely running until prompted to exit
  31.     char sentinial;
  32.  
  33.     ofstream fout;                          // fout is a user-defined object of type ofstream
  34.     fout.open("Phone Bill Records.txt");    // attach the file stream to the physical file
  35.     if (fout.fail())                        // if the file is not opened successfully;
  36.     {
  37.         cout << "\nError opening file for writing.\n";
  38.         return EXIT_FAILURE;    // terminate program from main with returned value of 1
  39.     }
  40.  
  41.     cout << "Welcome! please type the customer name to continue, " << endl << "or type \"exit\" to end the program." << endl << endl;
  42.  
  43.     // BEGIN LOOP
  44.     while (loop == true) {
  45.         // Entering the program
  46.         getData(name, accountNum, serviceType);
  47.         sentinial = getChoice("Would you like to save the record to a file? ", 'Y', 'N');
  48.         writeBillingInfoToFile(fout, name, accountNum, serviceType);
  49.     }
  50.     // END LOOP
  51.  
  52.     fout.close();
  53.    
  54.  
  55.     return 0;
  56. }
  57.  
  58. // Program body
  59. void getData(string& name, int& account, char& service)
  60. {
  61.     cout << "Customer name: ";
  62.     getline(cin, name);
  63.  
  64.     if (name == "Exit" || name == "exit" || name == "EXIT") {
  65.         cout << "Program terminated by user, good-bye." << endl;
  66.         system("PAUSE");
  67.         exit(EXIT_SUCCESS);
  68.         // Exits the program
  69.     }
  70.  
  71.     // Data processing
  72.     cout << "Enter account number: ";
  73.  
  74.     // Making sure the account number is between 0 and 9999
  75.     account = readInt("Account number", 0, 9999);
  76.  
  77.     // Making sure the service type is either an R or a P
  78.     service = getChoice("Service type", 'r', 'p');
  79.  
  80.     // Randomly generates minutes and calculates the total charge
  81.     double serviceCharge = calculate(service);
  82.  
  83.     // Being passed into display function for aesthetic appeal
  84.     string serviceName;
  85.  
  86.     if (service == 'p') {
  87.         serviceName = "Premium";
  88.     }
  89.     else {
  90.         serviceName = "Regular";
  91.     }
  92.  
  93.     cout << endl;
  94.  
  95.     // Displays the final billing information for the customer
  96.     display(name, account, serviceName, serviceCharge);
  97. }
  98.  
  99.  
  100. // Validates the account number
  101. int readInt(string errorMsg, int min, int max)
  102. {
  103.     int someInt;
  104.     bool valid = false;
  105.  
  106.     do
  107.     {
  108.         // User entering account number
  109.         cin >> someInt;
  110.  
  111.         // Verifying the data is valid (0 to 9999)
  112.         valid = (someInt >= min && someInt <= max);
  113.  
  114.         // Clearing out invalid data and prompting for re-entry
  115.         if (cin.fail() || !valid || !isspace(cin.peek()))
  116.         {
  117.             cout << "Error! Invalid input." << endl;
  118.             cout << errorMsg << " must be whole number between " << min << " and " << max << endl;
  119.             cout << "Try Again: ";
  120.             cin.clear(); // Clearing the fail state
  121.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  122.  
  123.             valid = false;
  124.         }
  125.     } while (!valid);
  126.  
  127.     // Clears out following incorrect data once correct
  128.     cin.ignore(100, '\n');
  129.  
  130.     return someInt;
  131. }
  132.  
  133. // Generates minutes and calculates the final charges
  134. double calculate(char serviceType)
  135. {
  136.     // Set amounts to be paid
  137.     double payRegular = 10.0, payPremium = 25.0;
  138.     int dayMinutes,         // Holds random day minutes
  139.         nightMinutes;       // Holds random night minutes
  140.     double  finalPay;       // Used to return final pay post-processing
  141.  
  142.     // seeding the random number generator
  143.     srand(time(NULL));
  144.  
  145.     // Making sure decimals are displayed correctly
  146.     cout << fixed << setprecision(2);
  147.  
  148.     // randomly generated minute range between 0 and 300. completely arbitrary ceiling
  149.     dayMinutes = 0 + rand() % ((300 - 0) + 1);
  150.     nightMinutes = 0 + rand() % ((300 - 0) + 1);
  151.  
  152.     // Checking and calculating Regular service
  153.     if (serviceType == 'r') {
  154.  
  155.         // Day minutes used as standard minutes on regular service
  156.         cout << "Minutes Used: " << dayMinutes << endl;
  157.  
  158.         if (dayMinutes > 50) {  // Checking if overcharge is necessary
  159.             dayMinutes -= 50;       // Extracting amount to be charged
  160.  
  161.             // Applying fee
  162.             payRegular = (dayMinutes * .2) + payRegular;
  163.  
  164.             // Finalizing payment due
  165.             finalPay = payRegular;
  166.         }
  167.     }
  168.     // Checking and calculating Premium service
  169.     else if (serviceType == 'p') {
  170.  
  171.         cout << "Day Minutes: " << dayMinutes << endl;
  172.         cout << "Night Minutes: " << nightMinutes << endl;
  173.  
  174.         if (dayMinutes > 75)    // Checking to see if overcharge is necessary
  175.             dayMinutes -= 75;       // Pulls minutes to be charged overtime
  176.         else
  177.             dayMinutes = 0;         // Applies standard charge if minutes not exceeded
  178.         if (nightMinutes > 100) // Checking if overcharge is necessary
  179.             nightMinutes -= 100;      // Pulling minutes to be charged overtime
  180.  
  181.         // Calculating the final pay based on pre-determined charges depending
  182.         // on using more minutes than allowed.
  183.         // Bases off day cycle as well
  184.         finalPay = (dayMinutes * 0.1) + (nightMinutes * 0.05) + payPremium;
  185.     }
  186.  
  187.     return finalPay;
  188. }
  189.  
  190. void display(string displayName, int displayAccount, string displayServiceType, double displayServiceCharge) {
  191.     // Displaying final bill
  192.     cout << "Customer Name: " << displayName << endl;
  193.     cout << "Account Number: " << displayAccount << endl;
  194.     cout << "Service Type: " << displayServiceType << endl;
  195.     cout << "Service Charge: " << displayServiceCharge << endl << endl;
  196.     system("PAUSE");
  197.    
  198.     // Clear screen
  199.     system("CLS");
  200.     cout << "(type EXIT to close program)" << endl;
  201. }
  202.  
  203. void writeBillingInfoToFile(ofstream& fileOut, string custName, int acctNo, char servType)
  204. {
  205.     fileOut << custName << '\t' << acctNo << '\t' << servType << endl;
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment