Search code examples
c++loopsswitch-statementinfinite-loopfstream

Issues with what appears to be never ending loops when calling functions in switch statement (even if function contains no loop)


I am trying to make a really simple text menu of sorts using switch statements for a class assignment. The issue is when calling the various functions I've made in the different cases of the switch statement, the never seem to end.

Considering the same issue seems to be happening to all the functions in my switch statement I think it might have something to do with the switch statement itself and not the individual functions although I honestly don't know at this point.

Disclaimer: I know I have tons of other issues with my code, but im just trying to pick a big one for simplicity. I would appreciate advice on other parts of my code aswell. (Also its not done yet as you can probably see by the unfinished cases)

Example output:


==================
1. Admission's Office
2. Student
3. End Program
==================
Enter your choice: 1

Enter the password: 123

******************
1. Add a new student
2. Add new students from a file
3. Drop a student
4. View one students info
5. View all students info
******************
Enter your choice: 1

Enter the first name: First Name

Enter the last name: Last Name

Enter the gender: m

Enter the first name: It should only ask me once

Enter the last name: Why is this looping?

Enter the gender: ????

Enter the first name:
Enter the last name: ???????

Enter the gender: a

Enter the first name: ^C

Code

//main.cpp
int main() {
  Student stuAr[SIZE];
  int choice, password, userInput;
  int numStu = 0;
  bool valid;

  do {
    userMenu();
    cin>>choice;
    cout<<endl;

    switch(choice){
    case 1:
      cout<<"Enter the password: ";
      cin>>password;
      cout<<endl;

      valid = checkPass(password);
      if (valid) {
        adminMenu();
        cin>>choice;
        cout<<endl;

        do {
          switch(choice) {
          case 1:
            addStu(stuAr, numStu);
            break;

          case 2:
            addStuFile(stuAr, numStu);
            break;

          case 3:
            cout<<"Enter the student ID: ";
            cin>>userInput;
            cout<<endl;
            dropStu(stuAr, numStu, userInput);
            break;

          case 4:
            cout<<"Enter the student ID: ";
            cin>>userInput;
            cout<<endl;
            viewStu(stuAr, numStu, userInput);
            break;

          case 5:
            viewAllStu(stuAr, numStu);
            break;
          }
        }while(choice != 6);
      }
      else {
        cout<<"The password is wrong."<<endl;
      }
      break;

    case 2:

      break;
    }
  }while(choice != 3);

  return 0;
//still main.cpp
//addStu function for case 1 of the nested switch statement
void addStu(Student stuAr[], int& numStu) {
  cin.ignore();
  cout<<"Enter the first name: ";
  stuAr[numStu].setFN();
  cout<<endl;

  //cin.ignore();
  cout<<"Enter the last name: ";
  stuAr[numStu].setLN();
  cout<<endl;

  cout<<"Enter the gender: ";
  stuAr[numStu].setGender();
  cout<<endl;

  numStu++;
  return;
}

Solution

  • You never change choice in the inner do..while loop, and therefore the condition choice != 6 will always be true, unless you enter it in the first time.