String not defined in scope. Unsure why

Viewed 115

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?

1 Answers

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

Related