Help with an Error: Expected ";" before "{" token

Hello, I am currently working on the Rock, Paper, Scissors, Lizard, Spock challenge and I am getting this error code—Error: Expected “;” before “{” token. I tried looking up similar code, but I not finding a possible solution. Might someone be able to help?

Error Message:

rock_paper_scissors.cpp: In function ‘int main()’:
rock_paper_scissors.cpp:50:18: error: expected ‘;’ before ‘{’ token
else (user == 1) {

Does this look right?

if (user == computer) {
std::cout << "Tie!\n";
}

//Rock
else (user == 1) {
//   ^^^^^^^^^^^ condition?
  if (computer == 3 || computer == 4 ) {
   std::cout << "Player Wins\n";;
  }
  else (computer == 2 || computer == 5) {
//     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ condition?
    std::cout << "Computer Wins\n";;
  }

I forgot to edit that double ; out. That was just one of the many different trouble shooting things I tried. I will take those away and I will double check.

After removing those double ; The same error messaged is still popping up.

You have conditions after your else's. The code in the else block is meant to execute when the condition in the if statement evaluates to falsey. There should be no condition attached. If you need to evaluate another condition, there is something missing after else.

Aside, the double ;; are inconsequential. You could have a hundred if you like, but there is no need for more than one.

Maybe it is a flaw within the chosen logic? The else statements are supposed to be if the user and the RNG do not pick the same number, so it runs little block chains to dictate who would win the Rock, Paper, Scissor match.

You cannot have a condition attached to an else use else if.

Even if I am testing the code to see if it works? The whole logic uses else if statements until the last one.
After testing the change, the same issue occurs. Should I scrap the plan and try doing a different logic?

Your logic has nothing to do with it. Your issue is a syntax error. You cannot follow the word else with a condition. Could you copy your code, and post it in a reply?

Here is the full code. I did add a final, and hopefully proper, else statement at the end to hopefully get that covered.

#include <//iostream>
#include <stdlib.h>

int main() {
/* Who Beats Who
  Rock beats scissors
  rock beats lizard

  paper beats rock
  paper beats spock

  scissors beat paper
  scissors beat lizard

  lizard beats spock
  lizard beats paper

  Spock beats rock
  Spock beats scissors

*/

srand (time(NULL));

int computer = rand() % 5 + 1;

int user = 0;

std::cout << "=========================================\n";
std::cout << "Rock, Paper, Scissors, Lizard, Spock!\n";
std::cout << "=========================================\n";

std::cout << "1) Rock\n";
std::cout << "2) Paper\n";
std::cout << "3) Scissors\n";
std::cout << "4) Lizard\n";
std::cout << "5) Spock\n";

std::cout << "Shoot! \n";

std::cin >> user;



if (user == computer) {
std::cout << "Tie!\n";
}

//Rock
else if (user == 1) {
  if (computer == 3 || computer == 4 ) {
   std::cout << "Player Wins\n";
  }
  else (computer == 2 || computer == 5) {
    std::cout << "Computer Wins\n";
  }

}
  

//Paper
else if (user == 2) {
   if (computer == 1 || computer == 5 ) {
   std::cout << "Player Wins\n";
 }
  else (computer == 2 || computer == 4) {
    std::cout << "Computer Wins\n";
  }
}


//scissors
else if (user == 3) {
   if (computer == 2 || computer == 4 ) {
   std::cout << "Player Wins\n";
 }
  else (computer == 1 || computer == 5) {
    std::cout << "Computer Wins\n";
  }
}

//Lizzard
else if (user == 4) {
   if (computer == 2 || computer == 5 ) {
   std::cout << "Player Wins\n";
 }
  else (computer == 1 || computer == 3) {
    std::cout << "Computer Wins\n";
  }
}

//Spock
else if (user == 5) {
   if (computer == 1 || computer == 3 ) {
   std::cout << "Player Wins\n";
 }
  else (computer == 2 || computer == 4) {
    std::cout << "Computer Wins\n";
  }

}
//other case
else () {
std::cout << "It is undecided\n";

}

}

This throws a syntax error. Your code won’t compile with a syntax error. If it won’t compile, you cannot run it to check the logic. Every place in your code where you follow the word else with anything other than an opening curly brace { is going to throw the same syntax error. There is no requirement for an if to end with an else. If you need an else use it. If you don’t, leave it off. If you need to check a condition after the initial if, you need to use else if not else.

#include <iostream>

int main() {

int num = 5;
if (num < 2) {
  std::cout << "num is less than 2";
} else if (num < 5) {
  std::cout << "num is less than 5";
} else if (num < 8) {
  std::cout << "num is less than 8";
}
// We could add an else if we wanted, but it isn't required
}

Also, if your code includes the following, you’ll have more errors. The // don’t belong.

1 Like

Okay, that makes sense. I thought you had if you had to use more then one statement, you should end it with else. That makes sense and that got the code working. When would be a proper time to use an else statement?

When you have a default action that you want to execute when all of the preceding conditions evaluate to falsey. For example:

1 Like