/* 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 student's #

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

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

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



  return 0;
}

/* fill sVec with values from fileName.
 * Receive: sVec, a vector<Student>; and
 *          fileName, a string.
 * PRE: fileName is the name of a file containing a sequence of Student values.
 * Input: the Student values from fileName.
 * Send back: sVec, containing the input values.
 */

void fill(vector<Student>& sVec, const string& fileName)
{
 ifstream fin( fileName.data() );
 assert( fin.is_open() );
 sVec.reserve(0);

 Student aStudent;
 for (;;)
 {
    fin >> aStudent;
    if ( fin.eof() ) break;
    sVec.push_back(aStudent);
 }

 fin.close();
}

