0
votes

My if statements aren't working and I am not sure why. Can someone point out my error, thank you. This is just a kinda dumb program I am making just for practice, I am setting a lot more variables along the way.

#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
main() --> int main(void)Sourav Ghosh
I am using Microsoft visual studio by the way. That is why my scanf() is scanf_s().Kevin Dunn
You should query for =='Y' || ... =='y' because your inputs are case-sensitiveTobias Knauss
Also you might want to add an else-path that returns "invalid input" to the user...Tobias Knauss
For simple exercise programs you should rather use scanf which is not the same as scanf_s. See Nishant's answer below.Jabberwocky

1 Answers

5
votes

scanf_s() is not a drop-in replacement for scanf(). You should include a buffer size when the input parameter is a character or string.

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

char s[10];

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