C++ 在switch语句中调用函数时出现的似乎是永无止境的循环问题(即使函数不包含循环)
我试图使用switch语句为类赋值创建一个非常简单的文本菜单。问题是,当调用我在switch语句的不同情况下所做的各种函数时,似乎永远不会结束 考虑到我的switch语句中的所有函数似乎都出现了同样的问题,我认为这可能与switch语句本身有关,而不是与单个函数有关,尽管我现在还不清楚 免责声明:我知道我的代码还有很多其他问题,但为了简单起见,我只是想挑一个大问题。我也希望得到关于代码其他部分的建议。(从未完成的案例中,您可能会看到它尚未完成) 示例输出:C++ 在switch语句中调用函数时出现的似乎是永无止境的循环问题(即使函数不包含循环),c++,loops,switch-statement,infinite-loop,fstream,C++,Loops,Switch Statement,Infinite Loop,Fstream,我试图使用switch语句为类赋值创建一个非常简单的文本菜单。问题是,当调用我在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(){
学生stuAr[尺寸];
int选项、密码、用户输入;
int numStu=0;
布尔有效;
做{
用户菜单();
cin>>选择;
cout在您的内部循环中,您永远不会重新阅读选项
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;
if(有效){
adminMenu();
cin>>choice;//只执行一次,因此do循环永远不会终止
不能选择;
cout您永远不会更改内部do..while
循环中的choice
,因此条件choice!=6
将始终为真,除非您第一次输入它。最好将第一个cin>>选项
拉入循环,而不是在开关盒
sta之后添加第二个选项泰门特。