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) {
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.
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?
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.
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?