iridescent
iridescent

Reputation: 305

Eliminating the <ENTER> required to continue the loop

In the code below, if the user enters 1 or 2, everything proceeds smoothly.

But if I enter anything else besides 1 or 2, the loop runs, but an additional is required to continue the loop.

How do I eliminate the need for this ?

#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);

}

Upvotes: 0

Views: 98

Answers (2)

Stefano Falasca
Stefano Falasca

Reputation: 9097

just move the call to clearKeyboardBuffer() right after //end of switch as in

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);

Upvotes: 1

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

You've got a problem with your if else loop. If the choice is not a digit or >3 you want to re-ask the user

#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);

}

Upvotes: 0

Related Questions