title: How to load .txt file into an object array using >> overloading? C++
body: <p>This is my first time posting on this site so please spare me any mistakes/etiquette screw ups!</p>
  
  <p>Basically I have a project due on Wednesday (yes, for a class). The code is to be written in C++ and it is a program that takes the following data members (as defined in a student class) from a .txt file listed below:</p>
  
  <pre><code>class Student
  {
  public: 
      //other functions such as add/edit/delete, etc... go here
  private:
      string firstname;
      string lastname;
      int grade; //1 for Freshman, 2 for Sophmore, 3 for Junior, 4 for Senior
      int student_id;
      double credit_hours; //credit hours taken
      double GPA; //current GPA
  };
  </code></pre>
  
  <p>I have also attached the .txt file below. Basically I have to read in the .txt file into an array of that class type, i.e. an object of the Student class. Our teacher said we could assume that the maximum size of the array was [100] students. I have tried several variations of a readFile() function but none have worked. I know I have to overload the >> operator to work with the Student class but I'm not sure how to do this. She suggested a friend function?</p>
  
  <p>Here is the .txt file called "StudentRecords.txt" saved in the same directory as the other .cpp file. </p>
  
  <pre><code>Harry
  Smith
  2
  11121321
  100
  3.8
  Mary
  Jones
  1
  43213843
  56
  3.1
  Nicolas
  Dodsworth
  4
  54219473
  120
  2.3
  J.Alfred
  Prufrock
  4
  83746321
  122
  4.0
  T.S.
  Eliot
  1
  99999999
  126
  4.0
  Charlotte
  Webb
  3
  44443333
  98
  3.8
  Don
  Juan
  1
  12345678
  56
  1.2
  John
  Smith
  2
  54234876
  66
  2.85
  Darth
  Vader
  2
  87623450
  49
  2.55
  3
  CPO
  4
  33333333
  100
  4.0
  Emily
  Dickinson
  3
  23456120
  110
  3.6
  James
  Buchanan
  1
  5640012
  30
  2.23
  Carl
  Rove
  1
  12995425
  28
  1.6
  Marie
  Curie
  4
  88888888
  96
  3.5
  Micky
  Mouse
  2
  8222222
  64
  1.85
  James
  Madison
  3
  66633333
  88
  2.96
  Dolly
  Madison
  3
  53423445
  84
  3.24
  Pepe
  LePew
  1
  73737373
  42
  2.47
  Homer
  Simpson
  4
  7223344
  105
  1.03
  Mary
  Jones
  1
  09274726
  28
  2.92
  Bloss
  Sims
  4
  11111111
  100
  1.2
  </code></pre>
  
  <p>Thanks guys! I really appreciate your help. I don't really have a lot of experience with C++ mainly Python, so this would definitely be helping me out. </p>
  
  <p>EDIT: </p>
  
  <p>Code below. The read function is the last function:</p>
  
  <pre><code>    #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  class stdRecord
  {
  public:
      string studentID;
      string studentName;
      int courseCode;
      int creditPoint;
  };
  
  const int MAXRECORD = 100;
  
  int menu();
  void searchStudent(stdRecord[],int);
  void showByCourse(stdRecord[],int);
  //void showEligible(stdRecord[],int);
  void showAll(stdRecord[],int);
  void update(stdRecord[],int );
  void add(stdRecord[],int *);
  void deleteRecord(stdRecord[],int *);
  void findCourses(stdRecord [],int );
  
  int main()
  {
      stdRecord stdRec[MAXRECORD]={"15000000","Joshua Andrew Smith", 3506, 240,"16666666", "Jack Williams", 3506, 180,"17000010", "Lily Jones", 3639, 110};
      int counter=3;
      int choice;
      do
      {
          choice=menu();
          switch(choice)
          {
              case 0: cout&lt;&lt;"Bye for Now"&lt;&lt;endl;
              break;
              case 1: searchStudent(stdRec,counter);
              break;
              case 2:showByCourse(stdRec,counter);
              break;
              case 3://showEligible(stdRec,counter);
              break;
              case 4:showAll(stdRec,counter);
              break;
              case 5:update(stdRec,counter);
              break;
              case 6:add(stdRec,&amp;counter);
              break;
              case 7:deleteRecord(stdRec,&amp;counter);
              break;
          }
      }while(choice!=0);
      system("pause");
  }
  
  int menu()
  {
      int choice;
      cout&lt;&lt;"0. Exit"&lt;&lt;endl
      &lt;&lt;"1. Search for a student"&lt;&lt;endl
      &lt;&lt;"2. List students enrolled in a course"&lt;&lt;endl
      &lt;&lt;"3. List students eligible to graduate"&lt;&lt;endl
      &lt;&lt;"4. List all students"&lt;&lt;endl
      &lt;&lt;"5. Update a student record"&lt;&lt;endl
      &lt;&lt;"6. Add a student record"&lt;&lt;endl
      &lt;&lt;"7. Delete a student record"&lt;&lt;endl    
      &lt;&lt;"Your choice -&gt;";
      cin&gt;&gt;choice;
      return choice;
  }
  
  void searchStudent(stdRecord stdRec[],int counter)
  {
      string ID;
      cout&lt;&lt;"Enter the student ID to search-&gt; ";
      getline(cin,ID);
  
      for(int i=0;i&lt;counter;i++)
      {
          if(stdRec[i].studentID.compare(ID)==0)
          {
              cout&lt;&lt;"The record with the id is:"&lt;&lt;endl;
              cout&lt;&lt;"StudentID\tStudentName\tCourseCoce\tCreditPoint"&lt;&lt;endl;
              cout&lt;&lt;stdRec[i].studentID&lt;&lt;"\t"&lt;&lt;stdRec[i].studentName&lt;&lt;"\t"&lt;&lt;stdRec[i].courseCode&lt;&lt;"\t"&lt;&lt;stdRec[i].creditPoint&lt;&lt;endl;
              cout&lt;&lt;"Have completed the requested process."&lt;&lt;endl;
              system("pause");
              system("cls");
              return;
          }
      }
      char ch;
      cout&lt;&lt;"Not Found"&lt;&lt;endl;
      cout&lt;&lt;"Do you want search another record(Y/N)";
      cin&gt;&gt;ch;
      ch=tolower(ch);
      if(ch=='y')
      searchStudent(stdRec,counter);
      else
      {
      cout&lt;&lt;"Have completed the requested process."&lt;&lt;endl;
      system("pause");
      system("cls");
      }
  }
  void findCourses(stdRecord stdRec[],int counter)
  {
      int courses[500];
      int coursecount=0;
      cout&lt;&lt;" Enter the course code {";
      bool found;
      for(int i=0;i&lt;counter;i++)
      {
          found=false;
          for(int j=0;j&lt;coursecount;j++)
          {
              if(stdRec[i].courseCode==courses[j])
              {
                  found=true;
                  break;
              }
          }
      if(!found)
          courses[coursecount++]=stdRec[i].courseCode;
      }
      cout&lt;&lt;" Enter the course code {";
      for(int j=0;j&lt;coursecount-1;j++)
          cout&lt;&lt;courses[j]&lt;&lt;", ";
      cout&lt;&lt;"or "&lt;&lt;courses[coursecount-1]&lt;&lt;"-&gt;";
  }
  
  void showByCourse(stdRecord stdRec[],int counter)
  {
      int courseCode;
      findCourses(stdRec,counter);
      cin&gt;&gt;courseCode;
      cout&lt;&lt;"The student(s) enrolled in the course is(are):"&lt;&lt;endl;
      cout&lt;&lt;"StudentID\tStudentName\tCourseCoce\tCreditPoint"&lt;&lt;endl;
      int studentsCount=0;
      for(int i=0;i&lt;counter;i++)
      {
          if(stdRec[i].courseCode==courseCode)
          {
              cout&lt;&lt;stdRec[i].studentID&lt;&lt;"\t"&lt;&lt;stdRec[i].studentName&lt;&lt;"\t"&lt;&lt;stdRec[i].courseCode&lt;&lt;"\t"&lt;&lt;stdRec[i].creditPoint&lt;&lt;endl;
              studentsCount++;
          }
      }
      cout&lt;&lt;"There is(are) "&lt;&lt;studentsCount&lt;&lt;" student(s) enrolled in the course."&lt;&lt;endl;
      cout&lt;&lt;"Have completed the requested process."&lt;&lt;endl;
      system("pause");
      system("cls");
  }
  
  
  void showEligible(stdRecord stdRec[],int counter)
  {
      cout&lt;&lt;"The student(s) eligible to graduate is(are):"&lt;&lt;endl; 
      int studentsCount=0;
      for(int i=0;i&lt;counter;i++)
      {
          if(stdRec[i].creditPoint&gt;=240)
          {
              cout&lt;&lt;stdRec[i].studentID&lt;&lt;"\t"&lt;&lt;stdRec[i].studentName&lt;&lt;"\t"&lt;&lt;stdRec[i].courseCode&lt;&lt;"\t"&lt;&lt;stdRec[i].creditPoint&lt;&lt;endl;
              studentsCount++;
          }
      }
      cout&lt;&lt;"There is(are) "&lt;&lt;studentsCount&lt;&lt;" graduate student(s)."&lt;&lt;endl;
      cout&lt;&lt;"Have completed the requested process."&lt;&lt;endl;
      system("pause");
      system("cls");
  }
  
  
  void showAll(stdRecord stdRec[],int counter)
  {
      cout&lt;&lt;"All students are listed below:"&lt;&lt;endl;
      for(int i=0;i&lt;counter;i++)
      {
          cout&lt;&lt;stdRec[i].studentID&lt;&lt;"\t"&lt;&lt;stdRec[i].studentName&lt;&lt;"\t"&lt;&lt;stdRec[i].courseCode&lt;&lt;"\t"&lt;&lt;stdRec[i].creditPoint&lt;&lt;endl;
      }
      cout&lt;&lt;"Have completed the requested process."&lt;&lt;endl;
      system("pause");
      system("cls");
  }
  
  void update(stdRecord stdRec[],int counter)
  {
      char keepGoing;
      string ID;
      do
      {
  
          cout&lt;&lt;"Enter the student ID to update-&gt; ";
          getline(cin,ID);
          bool flag=false;
          char choice;
          for(int i=0;i&lt;counter;i++)
          {
              if(stdRec[i].studentID.compare(ID)==0)
              {
                  cout&lt;&lt;"The record with the id is:"&lt;&lt;endl;                                         cout&lt;&lt;"StudentID\tStudentName\tCourseCoce\tCreditPoint"&lt;&lt;endl;
                  cout&lt;&lt;stdRec[i].studentID&lt;&lt;"\t"&lt;&lt;stdRec[i].studentName&lt;&lt;"\t"&lt;&lt;stdRec[i].courseCode&lt;&lt;"\t"&lt;&lt;stdRec[i].creditPoint&lt;&lt;endl;
                  cout&lt;&lt;"Enter y or Y to update the course code, others to keep the original one."&lt;&lt;endl;
                  cin&gt;&gt;choice;
                  if(choice=='y'||choice=='Y')
                  {
                      int courseCode;
                      findCourses(stdRec,counter);
                      cin&gt;&gt;courseCode;
                      stdRec[i].courseCode=courseCode;
                  }
                  cout&lt;&lt;"Enter y or Y to update the credit";
                  cin&gt;&gt;choice;
                  if(choice=='y'||choice=='Y')
                  {
                      int credits;
                      cout&lt;&lt;"Enter Credit points";
                      cin&gt;&gt;credits;
                      stdRec[i].creditPoint=credits;
                  }
                  flag=true;
                  break;
              }
          }
          if(!flag)
          {
              cout&lt;&lt;"The record with the id "&lt;&lt;ID&lt;&lt;" not Found"&lt;&lt;endl;
          }
              cout&lt;&lt;"Do you want update another record(Y/N)";
              cin&gt;&gt;keepGoing;
              keepGoing=tolower(keepGoing);
      }while(keepGoing=='y');
  
      cout&lt;&lt;"Have completed the requested process."&lt;&lt;endl;
      system("pause");
      system("cls");
  }
  
  void add(stdRecord stdRec[],int *counter)
  {
      string studentID;
      string studentName;
      int courseCode;
      int creditPoint;
      cout&lt;&lt;"Enter Student ID :";
      cin&gt;&gt;studentID;
      bool flag=true;
      for(int i=0;i&lt;*counter;i++)
      {
          if(stdRec[i].studentID.compare(studentID)==0)
          {
          flag=false;
          break;
          }
      }
      if(flag)
      {
          stdRec[*counter].studentID=studentID;
          cout&lt;&lt;"Enter Student Name \n";
          cin &gt;&gt; studentName;
          stdRec[*counter].studentName=studentName;
          cout&lt;&lt;"Enter Course Code \n";
          cin &gt;&gt; courseCode;
          stdRec[*counter].courseCode=courseCode;
          cout &lt;&lt; "Enter Credit Points \n";
          cin &gt;&gt; creditPoint;
          stdRec[*counter].creditPoint=creditPoint;
      }
      else
      {
          cout&lt;&lt;"Student Id Exists"&lt;&lt;endl;
      }
      cout&lt;&lt;"Have completed the requested process."&lt;&lt;endl;
      system("pause");
      system("cls");
  }
  
  void deleteRecord(stdRecord stdRec[],int *counter){}
  
  
  
  void readIn(stdRecord stdRec[])
  {
      ifstream file("StudentRecords.txt");
      if(file.is_open())
      {
          stdRecord stdRec[MAXRECORD];
  
          for(int i = 0; i &lt; MAXRECORD; i++)
          {
              file &gt;&gt; stdRec[i];
          }
      }
  }
  </code></pre>