在 switch 语句中调用函数时似乎永无止境的循环问题(即使函数不包含循环)

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

我正在尝试使用 switch 语句为 class 赋值制作一个非常简单的文本菜单。问题是当调用我在 switch 语句的不同情况下所做的各种功能时,似乎永远不会结束。

考虑到我的 switch 语句中的所有函数似乎都发生了同样的问题,我认为这可能与 switch 语句本身有关,而不是与各个函数有关,尽管我现在真的不知道。

免责声明:我知道我的代码还有很多其他问题,但为了简单起见,我只是想挑一个大问题。我也希望对我的代码的其他部分提出建议。 (它还没有完成,你可能会从未完成的案例中看到)

示例输出:


==================
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

代码

//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;
}

你永远不会在你的内部循环中重读 choice

if (valid) {
adminMenu();
cin>>choice; // only done once, therefore do loop never terminates
cout<<endl;
do {
  switch(choice) {
  case 1:
    addStu(stuAr, numStu);
    break;
  // ...
  }
  // after switch-case, add:
  cin >> choice;
  cout << endl;

您永远不会在内部 do..while 循环中更改 choice,因此条件 choice != 6 将始终为真,除非您是第一次输入它。