
//*****************************************************************************
////  Course:             COMS 2104 03 Foundations of Computer Programming I
////  Semester:           Spring 2004
////  Assignment Number:  #9                             
////  Author Name:        DarC KonQuesT                        
////  Date Written:       April 24, 2004
////                                                         
////  Description of Program:                              
////  Read student records from file into 1 dimensional array.  Print and do search    
////  through records, total records unknown.                             
////*****************************************************************************
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


const int N = 100;


int main()
{
  bool menu, found;	
  ifstream infile;
  string filename;
  int k, q, idprog, lnprog, fnprog, mjprog;
  string selection, fname[N], lname[N], major[N], idnum[N], x, idsearch;
  menu = true;

  cout << "Enter data file name:  ";
  cin >> filename;
  infile.open(filename.c_str());
  if(!infile) {
    cerr << "Error: Can't open file." << endl;
    return 1;
  } 
  
  k = 0;         // initialize array subscript value
  idprog = 0;
  lnprog = 0;
  fnprog = 0;
  mjprog = 0;
  infile >> x;   // read 1st item
  while(infile && k < N) 
  {
   if(k % 4 == 0)
   {
    idnum[idprog] = x;  
    ++idprog;
   }
  else if(k % 4 == 1)
   {
     lname[lnprog] = x;
     ++lnprog;
   }
  else if(k % 4 == 2)
   {
    fname[fnprog] = x;
    ++fnprog;
   }
  else if(k % 4 == 3)
   { 
     major[mjprog] = x;
     ++mjprog;
   }
    ++k;         
    infile >> x;               
  }



while(menu == true)
{
cout << endl << "-- Menu ---------" << endl << "1.  Find a Record" << endl << "2.  Print all Records" << endl << "3.  Quit" << endl << endl << "Your choice (1, 2, or 3) ===> ";
cin >> selection;	
	if(selection == "1")
	{
	found = false;
	cout << endl << endl << "Enter ID number:  ";
	cin >> idsearch;
	for(q = 0; q < idprog; ++q)
	{
	if(idnum[q] ==  idsearch)
	{
	found = true;
	cout << endl << idnum[q] << "  " << lname[q] << " " << fname[q] << "  " << major[q] << endl;
	}
	}
	if(found == false)
	cout << endl << "ID Number not found" << endl;
	}
	else if(selection == "2")
	{
		cout.setf(ios::left);
		cout << "ID   Last        First      Major" << endl;
		cout << "----------------------------------" << endl;
		for(int i = 0; i < idprog; ++i)
		{
		cout << idnum[i] << " " << setw(12) << lname[i] << setw(9) << fname[i] << "  " << major[i] << endl;
		}
		cout << "----------------------------------" << endl << endl;
	}	
	else if(selection == "3")
	{
	menu = false;
	return 0;
	}
	else
	{
	cout << endl << "Incorrect choice, please try again." << endl;
	}
}
}

