【发布时间】:2020-03-06 20:21:59
【问题描述】:
当我尝试调用此函数并为除整数以外的变量等级提供值时,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;
}
【问题讨论】:
-
发布代码时,请将其设为 MCVE (Minimal, Complete, Verifiable Example)(或 MRE 或 SO 现在使用的任何名称;MCVE 已经使用了五年多,不需要更改)或 SSCCE( Short, Self-Contained, Correct Example)。除此之外,这意味着不包括 20 行注释掉的代码。如果代码相关,则不会出现在注释中;如果它不相关,它不应该在问题中。
-
Jonathan Leffler,如果我错了,请原谅我,实际上我是社区的新手,但我仍然不太了解发布规则和规定。就我的问题而言,我只是将相关代码放入 cmets 中,只是出于调试目的。
-
请查看tour 和How to Ask。您将学到:要对评论做出反应,您应该通过前缀
@和名称来称呼评论者(您将在评论字段的左上角获得提案)。这将通知相应的评论者。 (否则,您的重新评论可能会被忽视。) -
感谢亲爱的@Scheff 提供宝贵的反馈意见,我一定会按照你的建议去做。
标签: c++