How to make reiterations using yes/no prompt?

I think this should be enough to give you an idea:

#include<stdio.h>

int main(void){
    int     validate;
    char    menu_choice;


        validate = 0;
        do{
            printf("Would you like another go?(y/n):\t" );

            if(scanf(" %c", &menu_choice ) == 1){
                if((menu_choice=='y') || (menu_choice=='Y')){
                    printf("You choosed Yes\n\n\n");
                    validate = 1;
                }else if((menu_choice=='n') || (menu_choice=='N')){
                    printf("You choosed No\n\n\n");
                    validate = 2;
                }else{
                    printf("Wrong Input.\n\n\n");
                    validate = 0;
                }
            }
        }while( validate == 0 || validate == 1);

        printf("Goodbye\n");

    return 0;
}
Would you like another go?(y/n):  1
Wrong Input.
Would you like another go?(y/n):    k
Wrong Input.


Would you like another go?(y/n):    y
You choosed Yes


Would you like another go?(y/n):    Y
You choosed Yes


Would you like another go?(y/n):    N
You choosed No


Goodbye

For things like this i prefer instead something like this:

#include<stdio.h>

int checkInput(int min, int max){
    int option,check;
    char c;

    do{
        printf("Please type a number beetwen %d and %d:\t",min,max);

        if(scanf("%d%c",&option,&c) == 0 || c != '\n'){
            while((check = getchar()) != 0 && check != '\n');
            printf("\tI sayed a Number please\n\n");
        }else if(option < min || option > max){
            printf("\tThe number has to be beetwen %d and %d\n\n",min,max);
        }else{
            break;
        }
    }while(1);

    return option;
}

int main(void){
    int number = checkInput(0,1);

    printf("\nYour number is\t%d\n",number);

    return 0;
}
Please type a number beetwen 0 and 1: 2e
    I sayed a Number please
Please type a number beetwen 0 and 1:   g45
    I sayed a Number please

Please type a number beetwen 0 and 1:   75
    The number has to be beetwen 0 and 1

Please type a number beetwen 0 and 1:   1

Your number is  1

But if you insist to use yes/no instead of Y/N, then;

#include<stdio.h>
#include<strings.h>

int main(void){
    int     validate;
    char    menu_choice[5];
    char *yes = "yes";
    char *no = "no";

        validate = 0;
        do{
            printf("Would you like another go?(yes/no):\t" );

            if(scanf(" %s", menu_choice ) == 1){
                if((strcasecmp(menu_choice, yes) == 0)){
                    printf("You choosed Yes\n\n\n");
                    validate = 1;
                }else if((strcasecmp(menu_choice, no) == 0)){
                    printf("You choosed No\n\n\n");
                    validate = 2;
                }else{
                    printf("Wrong Input.\n\n\n");
                    validate = 0;
                }
            }
        }while( validate == 0 || validate == 1);

        printf("Goodbye\n");

    return 0;
}

Output:

Would you like another go?(yes/no):   sadasdas
Wrong Input.
Would you like another go?(yes/no): 213212
Wrong Input.


Would you like another go?(yes/no): Yes
You choosed Yes


Would you like another go?(yes/no): YeS
You choosed Yes


Would you like another go?(yes/no): YES
You choosed Yes


Would you like another go?(yes/no): No
You choosed No


Goodbye

Please make notice that strcasecmp is found in strings.h and not in string.h.

Leave a Comment