/* emprecords5.cpp sorts a sequence of employee records stored in a 
 * file so they are arranged with the last names in alphabetical order.
 * 
 * Input (file):    employee records
 * Output (screen): user messages
 * Output (file):   sorted employee records
 ***********************************************************************/
 
#include <iostream>                           // cout, <<, >>
#include <fstream>                            // ifstream, ofstream
#include <string>                             // string
#include <cassert>                            // assert()
#include <vector>                             // vector<T>
#include <algorithm>                          // sort()
using namespace std;

int main()
{
   cout << "This program sorts the data in a file"
        << " named 'employees.dat'.\n";

   ifstream inStream("employees.dat");       // input stream
   assert(inStream.is_open());
      
   vector<string> empVector;                  // sequence-holder
   
   string empLine;                            // input variable

   for (;;)                                   // loop:
   {
      getline(inStream, empLine);             //   read a name
      if (inStream.eof()) break;              //   quit if eof
      empVector.push_back(empLine);           //   append name
   }                                          // end loop
   
   inStream.close();                          // close stream
   
   sort(empVector.begin(), empVector.end());  // sort vector

   ofstream outStream("payroll.dat");        // output stream
   assert(outStream.is_open());

   for (int i = 0; i < empVector.size(); i++) // display
      outStream << empVector[i] << endl;      //  vector

   outStream.close();

   cout << "\nProcessing complete.\n";

   return 0;
}


