title: Sporadic infinite loop during do-while c++
body: <p>Sometimes when I run the program, it runs perfectly fine.  Sometimes I get an infinite loop that just keeps displaying my first choice menu.  I tried searching for the error and I couldn't figure it out.  I thought there was an error in my input statement when I got the menuChoice, but I was unable to troubleshoot it out.  Should I be checking the value of menuChoice more often? To better specify, I am referring to the second do-while loop, the one that reprints the menu choices after each iteration.</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include "course.hpp"
  using namespace std; 
  
  
  int main() {
      int menuChoice, searchMenuChoice;
      string fileName;
      string line;
      ifstream inStream;
      string searchName;
      string searchPrefix;
      int searchNum;
      vector&lt;Course&gt; courseList;
      vector&lt;int&gt; enrolledClasses;
      do{
          cout &lt;&lt; "Please enter the file that contains othe course data: ";
          cin &gt;&gt; fileName;
          inStream.open(fileName);
      }while (!inStream.is_open());
      cout &lt;&lt; endl;
      while (getline(inStream, line)){
          courseList.push_back(Course(line));
      }   
      cout &lt;&lt; "Welcome to Banner NE, short for Never Existed!" &lt;&lt; endl;
      do {
          cout &lt;&lt; "\n"&lt;&lt; "---------------------------------------------" &lt;&lt; endl;
          cout &lt;&lt; "1 - List all available classes" &lt;&lt; endl;
          cout &lt;&lt; "2 - Search for a course" &lt;&lt; endl;
          cout &lt;&lt; "3 - View your current enrolled courses" &lt;&lt; endl;
          cout &lt;&lt; "4 - Enroll in course" &lt;&lt; endl;
          cout &lt;&lt; "5 - Exit" &lt;&lt; endl;
          cout &lt;&lt; "Make your selection here: ";
          cin &gt;&gt; menuChoice;
          cout &lt;&lt; endl;
          if (menuChoice == 5){
              cout &lt;&lt; "Thank you for using Banner NE and have a nice day!" &lt;&lt; endl;
              return 0;
          }
          if (menuChoice == 2) {
              cout &lt;&lt; endl;
              cout &lt;&lt; "Would you like to search by" &lt;&lt; endl;
              cout &lt;&lt; "1 - Course number" &lt;&lt; endl;
              cout &lt;&lt; "2 - Available seats remaining" &lt;&lt; endl;
              cout &lt;&lt; "3 - Instructor name" &lt;&lt; endl;
              cout &lt;&lt; "4 - Course prefix" &lt;&lt; endl;
              cout &lt;&lt; "Make your selection: ";
              cin &gt;&gt; searchMenuChoice;
              if (searchMenuChoice == 2) {
                  for (int i = 0; i &lt; courseList.size(); i++){
                      if (courseList.at(i).getSeatsRemaining() &gt; 0)
                          courseList.at(i).printCourse();
                  }
              }
              if (searchMenuChoice == 1) {
                  cout &lt;&lt; "Please enter the course number: ";
                  cin &gt;&gt; searchNum;
                  for (int i = 0; i &lt; courseList.size(); i++){
                      if (courseList.at(i).MatchesCourseNumberSearch(searchNum))
                          courseList.at(i).printCourse();
                          break;
                  }
              }
              if (searchMenuChoice == 3) {
                  cout &lt;&lt; "Please enter the name of the instructor: ";
                  cin &gt;&gt; searchName;
                  for (int i = 0; i &lt; courseList.size(); i++) {
                      if (courseList.at(i).MatchesInstructorSearch(searchName))
                              courseList.at(i).printCourse();
                  } 
              }
              if (searchMenuChoice == 4){
                  cout &lt;&lt; "Please enter the prefix you would like to search for: ";
                  cin &gt;&gt; searchPrefix;
                  for (int x = 0; x &lt; courseList.size(); x++)
                      if (courseList.at(x).MatchesPrefix(searchPrefix))
                          courseList.at(x).printCourse();
              }
          }
          if (menuChoice == 1) {
              for (int x = 0; x &lt; courseList.size(); x++){
                  cout &lt;&lt; "ID: " &lt;&lt; x &lt;&lt; "\t";
                  courseList.at(x).printCourse();
                  cout &lt;&lt; endl;
              }
          }
          if (menuChoice == 4) {
              int classEnrollment;
              cout &lt;&lt; "Please enter the ID number of the class you would like to enroll: ";
              cin &gt;&gt; classEnrollment;
              if (courseList.at(classEnrollment).Enroll()){ 
                  enrolledClasses.push_back(classEnrollment);
                  cout &lt;&lt; "You have enrolled in ID " &lt;&lt; classEnrollment &lt;&lt; endl;
              }
              else 
                  cout &lt;&lt; "There was not enough space, sorry." &lt;&lt; endl;
          }
  
          if (menuChoice == 3){
              for (int i = 0; i &lt; enrolledClasses.size(); i++){
                  courseList.at(enrolledClasses.at(i)).printCourse();
                  cout &lt;&lt; endl;
              }
              if (enrolledClasses.size() == 0 )
                  cout &lt;&lt; "You are not currently enrolled in any classes." &lt;&lt; endl;
          }
      } while (menuChoice != 5);
  
      return 0;
  }
  </code></pre>