[Solved] Nested if-else statement not working probably [closed]


In your else-if statement, instead of comparing the two variables, you assigned numberOfPeople to maxRoomCapacity. The assignment evaluates to true, causing the body of that if-else to execute, causing the flow of your program to skip the else statement.

The problem is here:

else if (maxRoomCapacity = numberOfPeople)

change it to:

else if (maxRoomCapacity == numberOfPeople)

Notice that:

  • = is an assignment operator
  • == is a comparison operator

Compile with warnings (e.g. -Wall for GCC), and you should get:

prog.cc: In function 'int main()':
prog.cc:26:26: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 else if (maxRoomCapacity = numberOfPeople)
          ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~

2

solved Nested if-else statement not working probably [closed]