Rock Paper Scissors

Decided to make a game of Rock Paper Scissors and I noticed I haven't learned how to use words as variables, I've only used numbers...hopefully my code can explain what i mean....Here's my code and yes I know the computer's always gonna win lol, but my questions are 1) Am I using the "string" syntax correctly? 2) If so why does it always say I didn't enter Rock paper or scissors when I do 3) Why does my game close so early?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// ConsoleApplication16.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{ std::string Rock, Paper, Scissors, n;

  cout <<"Please enter either Rock, Paper, Scissors and press ENTER:" << endl;
  cin >> n;

  if ( n!=Rock || n!=Paper || n!=Scissors){

	  cout << "You did not enter Rock, Paper, or Scissors" << endl;
	  cout << "Please enter Rock, Paper, or Scissors" << endl;
	  cin >> n;
  }

  if (n==Rock){

	  cout << "I pick Paper" << endl;
	  cout << "I win!";
  }
  
  if (n==Paper){

	  cout << "I Pick Scissors" << endl;
	  cout << "I win!";
  }

  if (n==Scissors){

	  cout << "I pick Rock" << endl;
	  cout << "I win!";
  }

    system("PAUSE");
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{ string Rock = "Rock", Paper = "Paper", Scissors = "Scissors", n;

  cout <<"Please enter either Rock, Paper, Scissors and press ENTER:" << endl;
  getline(cin, n);

  while ( n!=Rock && n!=Paper && n!=Scissors)
  {

	  cout << "You did not enter Rock, Paper, or Scissors" << endl;
	  cout << "Please enter Rock, Paper, or Scissors" << endl;
	  cin >> n;
  }

  if (n==Rock){

	  cout << "I pick Paper" << endl;
	  cout << "I win!";
  }

  if (n==Paper){

	  cout << "I Pick Scissors" << endl;
	  cout << "I win!";
  }

  if (n==Scissors){

	  cout << "I pick Rock" << endl;
	  cout << "I win!";
  }

    system("PAUSE");
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stdafx.h"  //No idea what this is, but if it's not a custom header, it should probably be in angle brackets <>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  //none of these are declared
  //remove "std::"  you're already telling the compiler you're using it
  std::string Rock, Paper, Scissors, n;

  cout <<"Please enter either Rock, Paper, Scissors and press ENTER:" << endl;
  cin >> n;  //use getline(cin, n);


  //Rock, Paper, and Scissors are not declared to anything
  if ( n!=Rock || n!=Paper || n!=Scissors){

	  cout << "You did not enter Rock, Paper, or Scissors" << endl;
	  cout << "Please enter Rock, Paper, or Scissors" << endl;
	  cin >> n;  //use getline(cin, n);
  }

  if (n==Rock){

	  cout << "I pick Paper" << endl;
	  cout << "I win!";
  }
  
  if (n==Paper){

	  cout << "I Pick Scissors" << endl;
	  cout << "I win!";
  }

  if (n==Scissors){

	  cout << "I pick Rock" << endl;
	  cout << "I win!";
  }

    system("PAUSE");  //avoid using this as much as possible.  Read second thread in the beginners forum.
    return 0;
}
Last edited on
if ( n!=Rock || n!=Paper || n!=Scissors){ should be if ( n!=Rock && n!=Paper && n!=Scissors){

also you need to either put something in your string variables Rock = "Rock"; or use a string literal instead
n != "Rock"
Last edited on
to make it random you could make it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{ string Rock = "Rock", Paper = "Paper", Scissors = "Scissors", n, computer_choice;
    srand(time(0));

  cout <<"Please enter either Rock, Paper, Scissors and press ENTER:" << endl;
  getline(cin, n);

  while ( n!=Rock && n!=Paper && n!=Scissors)
  {

	  cout << "You did not enter Rock, Paper, or Scissors" << endl;
	  cout << "Please enter Rock, Paper, or Scissors" << endl;
	  getline(cin, n);
  }

  int random_number = (rand() % 3)+1;

  if (random_number == 1)
{
     computer_choice = "Rock";
}
else if (random_number == 2)
{
     computer_choice = "Paper";
}
else if (random_number == 3)
{
    computer_choice = "Scissors";
}

if (n == "Rock" && computer_choice == "Rock")
{
     cout << "I pick Rock" << endl;
     cout << "We tie" << endl;
}

//Now you can continue the above statements to finish it



    system("PAUSE");
    return 0;
}
Last edited on
All your comments clear up sooo many things!!! Thank you guys sooooo much!!!
Topic archived. No new replies allowed.