2

so I've been picking up C++ during my free time, and I'm trying to build a simple rock paper scissors game. I get a "not defined in this scope error" for x, y and z when I try to run this.

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


int main(){

  string x,y,z;

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


    x = (computer == 3) ? "You win!" : "You lose.";
    std::cout<<x;

  }else if(user==2){


    y = (computer == 1) ? "You win!" : "You lose.";
    std::cout<<y;

  }else{

    if(user == 3){

      z = (computer == 2) ? "You win!" : "You lose.";
      std::cout<<z;

    }

  }


}

What seems to be the problem?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
kharlezaizen
  • 107
  • 4
  • 1
    Also: In C++ projects, prefer `` to ``. – Kuba hasn't forgotten Monica Apr 07 '20 at 16:28
  • 4
    Recommendation: In future questions copy the complete error message into the question. It makes it easier for answers to tell what went wrong (you get an answer faster) and makes it easier for future askers to find and understand the question and its answers. – user4581301 Apr 07 '20 at 16:29
  • @ReinstateMonica further documentation if you wonder why you should prefer `` over ``. https://stackoverflow.com/questions/2900785/whats-the-difference-between-cstdlib-and-stdlib-h – darclander Apr 07 '20 at 16:34

1 Answers1

5

You have string x,y,z; but string belongs to the namespace std.

Your cout works because you typed std::cout in your code. Do the same thing with these strings because you don't have a using namespace std; in your code.

Solution:

Change string x,y,z; to std::string x,y,z;

Aside:

You are also missing the header <ctime> for srand(time(NULL));

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53