0

在下面的代码中,如果用户输入 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);

}
4

2 回答 2

1

只需在 // 切换结束后立即将调用移至 clearKeyboardBuffer() ,如

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
        clearKeyboardBuffer();
        }// end of else if
        else{

        }// end of else


}// end of do 
while(choiceint !=3);
于 2013-07-05T08:58:43.223 回答
0

你的if else循环有问题。如果选择不是数字或 >3,您想重新询问用户

#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 < 1 || choiceint > 3){
            printf("You did not enter an accepted digit.\n");
        }
        // If 0 > choice > 3
        else {
            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;
                    case 3:
                        break;
                    default:
                        break;
            }// end of switch
        }// end of else


    clearKeyboardBuffer();

    }// end of do 
    while(choiceint !=3);

}
于 2013-07-05T08:58:28.267 回答