user3093536
user3093536

Reputation: 95

If Statement TroubleShooting

The code I have is:

if(seatingChart[row][seat][incorrectQuestion].question==seatingChart[compareRow][compareSeat][compareIncorrectQuestion].question&&seatingChart[row][seat][incorrectQuestion].answerChoice==seatingChart[compareRow][compareSeat][compareIncorrectQuestion].answerChoice||seatingChart[row][seat][incorrectQuestion].answerChoice==compareAnswerChoice)

The data structure I used was:

struct incorrect
{
unsigned int question;
unsigned int answerChoice
};

compareAnswerChoice returns an unsigned integer value. In fact, every single value in the parameters is an unsigned integer value, but the parameter after the final or seems to break the if statement. None of the values are negative, they are translated from characters (lowercase and uppercase) to the unsigned integers between 1 and 26 inclusive. The compilation returned no errors and the parameters in the vectors refer to their correct locations.

When I simply use:

if (seatingChart[row][seat][incorrectQuestion].answerChoice ==
    compareAnswerChoice)

The following code works fine, but when I add any of the anterior parameters from the former if statement, the code inside fails to work.

I understand that I'm doing something incorrectly, but I don't know what it is exactly, and so I cannot correct myself. Could somebody tell me what's wrong about the parameters?

Upvotes: 0

Views: 59

Answers (3)

Adam Liss
Adam Liss

Reputation: 48280

Let's start by cleaning this up a bit so we can see what's happening:

unsigned int question =
    seatingChart[row][seat][incorrectQuestion].question;
unsigned int answer =
    seatingChart[row][seat][incorrectQuestion].answerChoice;

unsigned int compare_question =
    seatingChart[compareRow][compareSeat][compareIncorrectQuestion].question;
unsigned int compare_answer =
    seatingChart[compareRow][compareSeat][compareIncorrectQuestion].answerChoice;

if (question == compare_question && answer == compare_answer ||
    answer == compareAnswerChoice)

The logical AND operator && has higher precedence than the logical OR operator ||, so this is equivalent to:

if ((question == compare_question && answer == compare_answer) ||
    answer == compareAnswerChoice)

What you probably want is this:

if (question == compare_question &&
       (answer == compare_answer || answer == compareAnswerChoice))

Upvotes: 1

4pie0
4pie0

Reputation: 29724

In C++ operator && has a higher (13) precedence than opeator || (14). You need parentheses in your if statement.

@user3093536 it is easy to debug such thing using debugger. Create conditions with known values and test how if() statement works with operators && and || in there. Step into the code.

Upvotes: 1

Mauren
Mauren

Reputation: 1975

I suppose your problem lies in the fact that && operator has higher precedence than ||. Try adding parens your if statement like

if(seatingChart[row][seat][incorrectQuestion].question==seatingChart[compareRow][compareSeat][compareIncorrectQuestion].question &&
    (seatingChart[row][seat][incorrectQuestion].answerChoice==seatingChart[compareRow][compareSeat][compareIncorrectQuestion].answerChoice
    || seatingChart[row][seat][incorrectQuestion].answerChoice==compareAnswerChoice))

Upvotes: 0

Related Questions