消除继续循环所需的<enter> </enter>

时间:2013-07-05 08:43:58

标签: c loops user-input

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

}

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

}