Need help with Rock, Paper, Scissors, Lizard, Pock project execrise

Link to Project: https://www.codecademy.com/courses/learn-c-plus-plus/projects/rock-paper-scissors-lizard-spock

I am honestly not sure what exactly to do. When I compile the code using the g++ command I always get this error from it (Error: expected unqualified-id before ‘if’/‘else’). I looked it up on Google and apparently, it is an issue with the syntax of the code which and not exactly sure where. I was hoping someone could point out what I did wrong in the code and help me with it.

Code:

include <iostream>
#include <stdlib.h>

/* Rock Paper Scissors Lizard Spock
(The Big Bang Theory)
*/

int main() {

  // Live long and prosper
  srand (time(NULL));

  int computer = rand() % 3 + 1;

  int user = 0;

  std::cout << "====================\n";
  std::cout << "rock paper scissors!\n";
  std::cout << "====================\n";

  std::cout << "1) ✊\n";
  std::cout << "2) ✋\n";
  std::cout << "3) ✌️\n";

  std::cout << "shoot! ";
  std::cin >> user;

}

  if (user == 1) {
    std::cout << "You choose: ✊\n";
  }

  else if (user == 2) {
    std::cout << "You choose: ✋\n";
  }

  else if {
    std::cout << "You choose: ✌️\n";
  }

  if (computer == 1) {
    std::cout << "The Computer choose: ✊\n";
  }
  else if (computer == 2) {
    std::cout << "The Computer choose: ✋\n";
  }
  else if {
    std::cout << "The Computer choose: ✌️\n";
  }


  if (user == computer) {

    std::cout << "it's a tie!\n";

  }

  // user rock

   else if (user == 1) {

    if (computer == 2) {

      std::cout << "The Computer won!\n";

    }
    if (computer == 3) {

      std::cout << "You won!\n";

    }

  }

  // user paper

   else if (user == 2) {

    if (computer == 1) {

      std::cout << "You won!\n";

    }
    if (computer == 3) {

      std::cout << "The Computer won!\n";

    }

  }

  // user scissors

    else if (user == 3) {

    if (computer == 1) {

      std::cout << "You won!\n";

    }
    if (computer == 2) {

      std::cout << "The Computer won!\n";

    }

}
  • The highlighted (in the screenshot) closing curly brace is in the wrong place. Remove it from there and move it to the very end of all your code. In its current position, it closes out main which means your if statements are outside of main.

  • If you are using else if { then you must specify a condition e.g. else if (computer == 3) { . If you don’t want to specify a condition, then it should be just else {
    if and else if require conditions, whereas the else keyword doesn’t require a condition to be specified.

Thank you very much for your help this fixed the problem that I was having with my code.

1 Like