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

Hi I’m having trouble with this.
It’s saying I’m losing even when I should win.

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

int main() {
  
  /*
Rock Paper Scissors Lizard Spock
*/

  srand (time(NULL));  
  int computer = rand() % 5 + 1;
  int user = 0;

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

/*
 1 rock > scissors
 1 scissors > paper
 1 paper > rock
 1 lizard > spock
 1 spock > scissors
 2 scissors > lizard
 2 lizard > paper
 2 paper > spock
 2 spock > rock
 2 rock > lizard
*/

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

  std::cout << "shoot! ";

  std::cin  >> user;

  switch (user) {
    case 1:
    if (computer == 2 || 5) {
    std::cout << "You lose!\n";
    } else if (computer == 3 || 4) {
    std::cout << "You win!\n";
    } else {
    std::cout << "you tied!!!\n";
    }
    std::cout << computer <<"\n";
    break;

    case 2:
    if (computer == 3 || 4) {
    std::cout << "You lose!\n";
    } else if (computer == 1 || 5) {
    std::cout << "You win!\n";
    } else {
    std::cout << "you tied!!!\n";
    }
    std::cout << computer <<"\n";
    break;

    case 3:
    if (computer == 1 || 5) {
    std::cout << "You lose!\n";
    } else if (computer == 2 || 4) {
    std::cout << "You win!\n";
    } else {
    std::cout << "you tied!!!\n";
    }
    std::cout << computer <<"\n";
    break;

    case 4:
    if (computer == 1 || 3) {
    std::cout << "You lose!\n";
    } else if (computer == 2 || 5) {
    std::cout << "You win!\n";
    } else {
    std::cout << "you tied!!!\n";
    }  
    std::cout << computer <<"\n";
    break;

    case 5:
    if (computer == 2 || 4) {
    std::cout << "You lose!\n";
    } else if (computer == 1 || 3) {
    std::cout << "You win!\n";
    } else {
    std::cout << "you tied!!!\n";
    }
    std::cout << computer <<"\n";
    break;
  }  
}

Sorry for the briefness I’m not much of a talker. I don’t know if it’s the cursed switch else if (it feels like I shouldn’t use them like this) or the logical operator || , it should take either as true or neither as false but it’s clearly not doing that.
Edit: I did the regular rps like this and it worked (without the operator), so it’s probably that. I’m just not sure why it’s wrong.
Edit2: nvm I Just had specify computer again like this

  switch (user) {
    case 1:
    if (computer == 2 || computer == 5) {
    std::cout << "You lose!\n";
    } else if (computer == 3 || computer == 4) {
    std::cout << "You win!\n";
    } else {
    std::cout << "you tied!!!\n";
    }
    std::cout << computer <<"\n";
    break;

Duh

thanks in advance