/* registrar.cpp retrieves a student's data from a file 
 *  using their id #.
 * Input (file):     a sequence of Students
 * Input (keyboard): one or more student numbers
 * Output:           that student's data
 *************************************************************/

#include <iostream>                      // cin, cout
#include <fstream>                       // ifstream, ofstream
#include <cassert>                       // assert()
#include <vector>                          // vector<T>
#include <algorithm>                       // find
using namespace std;
#include "Student.h"                       // Student

void fill(vector<Student>& sVec, const string& fileName);

int main()
{
  const string INPUT_FILE = "students.txt";

  cout << "This program provides an information retrieval system\n"
          "  by reading a series of student records from "
       << '\'' << INPUT_FILE << '\''
       << "\n  and then allowing retrieval of any student's data.\n";

  vector<Student> studentVec;
  fill(studentVec, INPUT_FILE);

  long studentID;                               // the student we seek
  vector<Student>::iterator position;           // position of the student

  for (;;)                                      // repeat:
  {
     cout << "\nEnter the ID # of a student (eof to quit): ";
     cin >> studentID;                          //  get the student

     if (cin.eof()) break;                      //  if finished, quit

     position = find(studentVec.begin(),        //  search
                     studentVec.end(),          //   the vector for
                     Student(studentID,         //   this student id #
                             "", "",            //   using placeholder
                             "", 0, 0));        //   arguments 

     if (position != studentVec.end())          //   if found
     {
        cout << '\n';
        (*position).print(cout);                //     display student
        cout << endl;
     }
     else                                       //   otherwise, tell user
        cerr << "\nThere is no student with ID # "
             << studentID  << ".\n";
  }

  return 0;
}

void fill(vector<Student>& sVec, const string& fileName)
{
   ifstream fin( fileName.data() );
   assert( fin.is_open() );
   sVec.reserve(0);
   
   Student aStudent;
   for (;;)
   {
      aStudent.read(fin);
      if ( fin.eof() ) break;
      sVec.push_back(aStudent);
   }
   
   fin.close();
 }
