home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch13 / expsum / expmaker / expmaker.cpp
Encoding:
C/C++ Source or Header  |  1995-09-18  |  3.2 KB  |  126 lines

  1. // Get our needed include files
  2. #include <fstream.h>
  3. #include <iomanip.h>
  4. #include "employee.h"
  5. #include "expense.h"
  6.  
  7. void GetNewExpenseItem(ofstream& outfile, const unsigned itemnum)
  8. {
  9.     Expense NewExpense;
  10.     float   ExpenseAmount;
  11.     char    TempBuffer[256], ChompNewline;
  12.  
  13.     // User friendly stuff
  14.     cout << "\nExpense #" << itemnum+1 << "\n";
  15.  
  16.     // Fill in the basics
  17.     cout << "Expense date   : ";
  18.     cin.get(TempBuffer, 256);
  19.     cin.get(ChompNewline);
  20.     NewExpense.SetDate(TempBuffer);
  21.     cout << "Description    : ";
  22.     cin.get(TempBuffer, 256);
  23.     cin.get(ChompNewline);
  24.     NewExpense.SetDescription(TempBuffer);
  25.     cout << "Expense amount : ";
  26.     cin >> ExpenseAmount;
  27.     cin.get(ChompNewline);
  28.     NewExpense.SetExpenseAmount(ExpenseAmount);
  29.  
  30.     // Write the expense record to the file
  31.     outfile << NewExpense;
  32. }
  33.  
  34. void GetNewEmployee(ofstream& outfile)
  35. {
  36.     Employee      NewEmployee;
  37.     unsigned long NewZip;
  38.     unsigned      loop, NewID, NewNumExpenses;
  39.     char          TempBuffer[256], ChompNewline, NewInitial;
  40.  
  41.     // User friendly stuff
  42.     cout << "\nNew employee record\n";
  43.     cout << "===================\n";
  44.  
  45.     // Fill in the basics
  46.     cout << "Employee ID    : ";
  47.     cin >> NewID;
  48.     cin.get(ChompNewline);
  49.     NewEmployee.SetID(NewID);
  50.     cout << "First name     : ";
  51.     cin.get(TempBuffer, 256);
  52.     cin.get(ChompNewline);
  53.     NewEmployee.SetFirstName(TempBuffer);
  54.     cout << "Middle initial : ";
  55.     cin >> NewInitial;
  56.     cin.get(ChompNewline);
  57.     NewEmployee.SetMiddleInitial(NewInitial);
  58.     cout << "Last name      : ";
  59.     cin.get(TempBuffer, 256);
  60.     cin.get(ChompNewline);
  61.     NewEmployee.SetLastName(TempBuffer);
  62.     cout << "Address        : ";
  63.     cin.get(TempBuffer, 256);
  64.     cin.get(ChompNewline);
  65.     NewEmployee.SetAddress(TempBuffer);
  66.     cout << "City           : ";
  67.     cin.get(TempBuffer, 256);
  68.     cin.get(ChompNewline);
  69.     NewEmployee.SetCity(TempBuffer);
  70.     cout << "State          : ";
  71.     cin.get(TempBuffer, 256);
  72.     cin.get(ChompNewline);
  73.     NewEmployee.SetState(TempBuffer);
  74.     cout << "ZIP code       : ";
  75.     cin >> NewZip;
  76.     cin.get(ChompNewline);
  77.     NewEmployee.SetZip(NewZip);
  78.     cout << "\nNumber of expense items for this employee? ";
  79.     cin >> NewNumExpenses;
  80.     cin.get(ChompNewline);
  81.     NewEmployee.SetNumExpenses(NewNumExpenses);
  82.  
  83.     // Write the employee record to the file
  84.     outfile << NewEmployee;
  85.  
  86.     // Now get the expense records
  87.     for (loop = 0; loop < NewEmployee.GetNumExpenses(); loop++)
  88.         GetNewExpenseItem(outfile, loop);
  89. }
  90.  
  91. void main(int argc, char *argv[])
  92. {
  93.     // Check for right number of command-line arguments
  94.     if (argc != 2) {
  95.         cout << "USAGE: expmaker <outfile name>\n\n";
  96.         return;
  97.     }
  98.  
  99.     // Open the outfile
  100.     ofstream outfile(argv[1]);
  101.  
  102.     // Place our dummy employee count
  103.     unsigned EmployeeCount = 0;
  104.     outfile << setw(5) << EmployeeCount << setw(0) << " ";
  105.  
  106.     // Main employee loop
  107.     char Again;
  108.     do {
  109.         char ChompNewline;
  110.         EmployeeCount++;
  111.         GetNewEmployee(outfile);
  112.         cout << "\nEnter another employee (Y/N)? ";
  113.         cin >> Again;
  114.         cin.get(ChompNewline);
  115.     } while (Again == 'y' || Again == 'Y');
  116.  
  117.  
  118.     // Go to the beginning of the file and fill in the
  119.     // real employee count
  120.     outfile.seekp(0, ios::beg);
  121.     outfile << setw(5) << EmployeeCount;
  122.  
  123.     // Close the outfile _ we're done!
  124.     outfile.close();
  125. }