Mike
Mike

Reputation: 25

Yes & No answer in C

I am trying to make a C program where I can ask the following user yes or no questions:

Do you like beer? Y or N

Are you old enough? Y or N

How old are you?

if the user said over 18 print the message: let's go for some beers

but if one of the 2 first questions or the age is N, print: sorry maybe next time

I think the issue is with the If statement , perhaps I am not encapsulating the conditions.

#include <stdio.h>

int main(){

    char answer;
    int age = 0 ;

    printf("Do you like beers Enter Y or N: \n");
    scanf(" %c", &answer);
    printf("\n so your answer is %c\n", answer);

    printf("Are you old enough to have a beer?\n");
    scanf(" %c", &answer);
    printf("\n so your answer is %c\n", answer);


    if (answer == 'Y') {

        printf("how old are you?\n");
        scanf(" %d", &age);

        if (age >= 18)
            printf("\n Let's go for some beers , I will pay the first round \n");

    }if else  (age < 18 && answer == 'N')
        printf("\n sorry my friend , maybe next time \n");



       // printf("You may NOT ");

  return 0;

}

Upvotes: 1

Views: 3546

Answers (4)

BLUEPIXY
BLUEPIXY

Reputation: 40145

I think that you can think that the structure of your program is roughly as follows.

REPLY = PROMPT(Q1_MESSAGE)
IF(REPLY == YES){
    //Narrow down the conditions
    REPLY = PROMPT(Q2_MESSAGE)
    IF(REPLY == YES){
        //Narrow down the conditions
        REPLY = PROMPT(Q3_MESSAGE)
        IF(REPLY >= 18){
            DISPLAY(GOOD_MESSAGE)
        } ELSE {
            DISPLAY(NO_GOOD_MESSAGE)
        }
    } ELSE {
        DISPLAY(NO_GOOD_MESSAGE)
    }
} ELSE {
    DISPLAY(NO_GOOD_MESSAGE)
}

A nested IF can be considered as AND condition as a condition.
So by summarizing the question message and its response part into a function, it is possible to write as follows.

IF(FUNC1() == TRUE AND FUNC2() == TRUE AND FUNC3() == TRUE){//Function call proceeds from left to right (Shortcut evaluated)
    DISPLAY(GOOD_MESSAGE)
} ELSE {
    DISPLAY(NO_GOOD_MESSAGE)
}

As an example, you can write concretely as follows.

#include <stdio.h>

int main(void){
    char YN(const char *prompt);
    int enter_age(const char *prompt);

    if(YN("Do you like beers Enter Y or N: \n") == 'Y' &&
       YN("Are you old enough to have a beer?\n") == 'Y' &&
       enter_age("How old are you?\n") >= 20){
        printf("\nLet's go for some beers, I will take the first round.\n");
    } else {
        printf("\nSorry my friend, maybe next time.\n");
    }

    return 0;
}

char YN(const char *prompt){
    char ans[2], ret, ch;
    int ret_scnf;

    while(1){
        fputs(prompt, stdout);
        if((ret_scnf = scanf(" %1[YNyn]%c", ans, &ch)) == 2 && ch == '\n'){
            if(*ans == 'Y' || *ans == 'y'){
                ret = 'Y';
                break;
            } else if(*ans == 'N' || *ans == 'n'){
                ret = 'N';
                break;
            }
        } else if(ret_scnf == EOF){
                ret = 'N';
                break;
        }
        scanf("%*[^\n]");scanf("%*c");//clear input
    }
    return ret;
}
int enter_age(const char *prompt){
    int ret_scnf, age;
    char ch;

    while(1){
        fputs(prompt, stdout);
        if((ret_scnf = scanf("%d%c", &age, &ch)) == 2 && ch == '\n'){
            if(age < 0)//Can I enter years old at the age of 0?
                continue;
            return age;
        } else if(ret_scnf == EOF){
                age = -1;
                break;
        }
        scanf("%*[^\n]");scanf("%*c");
    }
    return age;
}

Upvotes: 0

user6875715
user6875715

Reputation:

You are using the same variable "answer". So, when you enter the answer to the second question it replace the first answer.

    int main(){

        char answer;
        char answer2;
        int age = 0 ;

        printf("Do you like beers Enter Y or N: \n");
        scanf(" %c", &answer);
        printf("\n so you answer is %c\n", answer);

        printf("Are you older enough to have a beer?\n");
        scanf(" %c", &answer2);
        printf("\n so you answer is also %c\n", answer2);


        if (answer == 'Y' && answer2 == 'Y') {

            printf("how old are yo.\n");
            scanf(" %d", &age);

            if (age >= 18)
                printf("\n lets go for some beers , I will paid the first round \n");

        }

        else if  (answer == 'N' || answer2 == 'N')
            printf("\n sorry my friend , maybe next time \n");

           // printf("You may NOT ");

      return 0;

    }

Hope this is what you needed. Cheers.

Upvotes: 0

welldog777
welldog777

Reputation: 49

From your code snippet above, it looks like your else statement is formatted incorrectly (if else instead of else if). Also, since you are testing whether or not either question was false, you should use the || operand. So you would want to do something like:

else if  (age < 18 || answer == 'N')

Upvotes: 3

Pintang
Pintang

Reputation: 438

The issue is your last if statement is only true if the user is under 18 AND said No. you want it to be either or.

Change the last if statement from

if else  (age < 18 && answer == 'N')

To:

  if else  (age < 18 || answer == 'N')

Upvotes: 0

Related Questions