【问题标题】:Eliminating the <ENTER> required to continue the loop消除继续循环所需的 <ENTER>
【发布时间】:2013-07-05 08:43:58
【问题描述】:

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

}

【问题讨论】:

标签: c loops user-input


【解决方案1】:

只需在 //end of switch 之后立即调用 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);

【讨论】:

    【解决方案2】:

    您的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);
    
    }
    

    【讨论】:

    • 这会缩短代码,但并不能解决问题。无论如何谢谢=)
    • 不用担心,我尝试使用您缩短的 switch 语句来更改 clearKeyboardBuffer() 的位置,如所选答案中所建议的那样,但仍然需要“输入”键......我想知道为什么?它也应该工作=/
    猜你喜欢
    • 1970-01-01
    • 2011-08-19
    • 2010-10-28
    • 2015-08-19
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多