Bassicly i want the user to input “4” and then display the next question but when the user }
q=1;
Bassicly i want the user to input 4 and then display the next question
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An else if might work (depending on if this is in a loop or what) but the most important thing here to learn is how the && operator works. When you have two statements separated by && the one appearing first will execute first.. If the result is false the second statement will not execute at all. However you don’t want to ask the question at all in the second if statement if q == 0
Since you are comparing what the user entered within the if statement, what is happening is that even though the question == 0 the second part of the if is not executing until after you asked the question.
You should be the q == ? condition first in both cases (or if there are only 2 questions use else as shown by brawk1990)
I would at least change your code to this:
There are a lot of better ways to do this though but this looks like some kind of homework so I’m guessing you have to do it a certain way. As someone else suggested I think a case statement would be better, but we really need to see more of the program/assignment to give good advice there.