如果陈述不符合预期

时间:2015-11-09 19:11:41

标签: c

我们必须为作业制作一个简短的问卷类型程序,当我运行它时,它会在您输入任何内容之前打印第一个if语句。然后它会在收到任何输入之前再次打印其余的语句,包括选项1的if语句。我不确定我做错了什么。

#include <stdio.h

int main()

{

    char reply = 0;
    char yes = 0;
    char no = 0;
    char name = 0;
    int option = 0;


    printf("Hey M!\n");
    printf("Is today your 16th birtday?!\n:yes or no\n");


    if (reply == yes) {
        printf("HAPPY BIRTHDAY!! :)\n");
    }
    else {
        printf("Are you sure??\n");
    }

    scanf("%c", &reply);

    printf("It is time for your super awesome birthday surprise\n\n");
    printf("You must choose between one of two presents\n\n");
    printf("Please choose between the following options:\n Option 1 or Option 2\n (enter a 1 or a 2)\n\n");

    if (option == 1) {
        printf("Go look in Mom and Dad's closet!!");
    }
    else {
        printf("Go look in the storage room!!");
    }
    scanf("%d", &option);

    printf("HAVE A GREAT SWEET 16!");

    return 0;

2 个答案:

答案 0 :(得分:0)

您在scanf语句后撰写if语句,您必须了解char yes与字符串"yes"不同。

#include <stdio.h>

int main()
{
    char reply = 0;
    char no = 0;
    char name = 0;
    int option = 0;


    printf("Hey M!\n");
    printf("Is today your 16th birtday?!\n:yes or no\n");

    scanf("%c", &reply);

    if (reply == 'y') {
        printf("HAPPY BIRTHDAY!! :)\n");
    }
    else {
        printf("Are you sure??\n");
    }

    printf("It is time for your super awesome birthday surprise\n\n");
    printf("You must choose between one of two presents\n\n");
    printf("Please choose between the following options:\n Option 1 or Option 2\n (enter a 1 or a 2)\n\n");

    scanf("%d", &option);

    if (option == 1) {
        printf("Go look in Mom and Dad's closet!!");
    }
    else {
        printf("Go look in the storage room!!");
    }

    printf("HAVE A GREAT SWEET 16!");

    return 0;
}

答案 1 :(得分:0)

首先使用scanf从用户那里获取输入,然后获取该输入,并传入if语句参数,而char只接受单个字符值Ex: - &#39; y&#39;如果你想使用&#34;是&#34;然后使用string或char []。

包括
int main()
{

char reply;
int option;

printf("Hey M!\n");
printf("Is today your 16th birtday?!\n:Yes=Y or No=N\n");
scanf("%c", &reply); \\ Enter Y or y for Yes

if (reply == 'Y' || reply == 'y') {
    printf("HAPPY BIRTHDAY!! :)\n");
}
else {
    printf("Are you sure??\n");
}

printf("It is time for your super awesome birthday surprise\n\n");
printf("You must choose between one of two presents\n\n");
printf("Please choose between the following options:\n Option 1 or Option 2\n (enter a 1 or a 2)\n\n");

scanf("%d", &option);

if (option == 1) {
    printf("Go look in Mom and Dad's closet!!");
}
else {
    printf("Go look in the storage room!!");
}


printf("HAVE A GREAT SWEET 16!");

return 0;