0

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;

}
8
  • 2
    main() --> int main(void) Commented Aug 14, 2015 at 7:27
  • I am using Microsoft visual studio by the way. That is why my scanf() is scanf_s(). Commented Aug 14, 2015 at 7:28
  • 2
    You should query for =='Y' || ... =='y' because your inputs are case-sensitive Commented Aug 14, 2015 at 7:30
  • Also you might want to add an else-path that returns "invalid input" to the user... Commented Aug 14, 2015 at 7:32
  • 1
    For simple exercise programs you should rather use scanf which is not the same as scanf_s. See Nishant's answer below. Commented Aug 14, 2015 at 7:39

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.