C编程如果语句不起作用

时间:2015-08-14 07:25:30

标签: c if-statement

我的if语句不起作用,我不知道为什么。有人可以指出我的错误,谢谢。这只是我为练习而做的一个愚蠢的程序,我在这个过程中设置了很多变量。

#include <stdio.h>

main()
{

    //This is a program that determines what circle of hell the user will be put in.  Inspired by Dante's Divine Comedy
    char firstQuestion;

    float total = 0;

    printf("ABANDON ALL HOPE, YOU WHO ENTER HERE\n\n");
    printf("Welcome to the gate of hell. I am going to ask you a series of questions and you will answer them truthfully.\n\n\n");

    printf("I would first like to ask you, do you believe you are a good person?(Y or N)\n");
    scanf_s(" %c", &firstQuestion);
    if (firstQuestion == 'Y'){
        printf("We will see about that.\n");
        total = total + 10;
    }
    else if (firstQuestion == 'N'){
    printf("I'm not surprised.\n");
}
    return 0;

}

1 个答案:

答案 0 :(得分:5)

scanf_s()不是scanf()的替代品。当输入参数是字符或字符串时,您应该包含缓冲区大小。

scanf_s(" %c", &firstQuestion,1);  //For single character

char s[10];

scanf_s("%9s", s, 10);    //For reading a string