在下面的代码中,如果用户输入 1 或 2,一切都会顺利进行。
但是,如果我输入除 1 或 2 之外的任何其他内容,则循环运行,但需要额外的内容才能继续循环。
我如何消除对此的需求?
#include <stdio.h>
#include <ctype.h>
#include <math.h>
void clearKeyboardBuffer() {
int ch;
while ((ch = getchar() != '\n') && (ch != EOF));
}
void entry1(){
char chArr[BUFSIZ];
char choice, ch;
int choiceint, rating;
do{
printf("1.\tZoo\n2.\tMall\n3.\tExit\n\n");
printf("Choose a place by entering its numerical value: ");
fgets(chArr,sizeof(chArr),stdin);
sscanf(chArr, " %c", &choice);
// converts scanned char to int
choiceint = choice - '0';
if(!isdigit(choice) || choiceint > 3){
printf("You did not enter an accepted digit.\n");
}
else if(choiceint != 3){
switch(choiceint){
case 1:
printf("You chose the Zoo.\n");
printf("your rating : ");
scanf("%d", &rating);
break;
case 2:
printf("You chose the Mall.\n");
printf("your rating : ");
scanf("%d", &rating);
break;
}// end of switch
}// end of else if
else{
}// end of else
clearKeyboardBuffer();
}// end of do
while(choiceint !=3);
}