作为无限循环运行



当我试图调用这个函数并为变量grade提供一个值而不是integer时,do-while循环会继续执行,甚至不会提示输入char类型的变量。请帮我弄清楚为什么循环一直在执行。

//User Input function
int userInput(){
int grade,question;
char choice='y';
srand(time(0));
do{
//displayMenu();
cout<<endl;
cout<<"Please select grades, use number 1 to 5: ";
cin>>grade;
/*if(grade<1 || grade>5){
cout<<"You have entered an invalid grade!"<<endl;
}
else{
cout<<"Enter number of questions you want to generate: ";
cin>>question;
while(question<1){
cout<<endl;
cout<<"You have entered an invalid number"<<endl;;
cout<<"Enter number of questions you want to generate: ";
cin>>question;
}
cout<<endl;
questionGenerator(grade,question);
cout<<endl;
cout<<"Press n/N to Quit or Press any key and then Enter";
cin>>choice;
system("cls");
}*/
cout<<"Type N/n to Quit or Press Any Key and then Enter"<<endl;
cout<<"Your choice? : ";
cin>>choice;
system("cls");
}while(choice!='n' && choice!='N');
return 0;
}

你的等级变量是一个整数,所以它不能接受任何其他类型的变量。如果你要输入一个字符,那么为什么等级是一个整数,或者如果你要输出一个字符串?如果您需要实现这一点,那么可以尝试使用数组,并开始将它们从数组转换为:integers浮点布尔值字符串或字符。还要小心,因为可以将字符值分配给数字(字符代码(。

最新更新