Simple Rock, Paper, Scissors game

My code won't print until line 21. Also if anyone see's mistakes, it would be most helpfull if you point them out since im just starting.

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
51
52
53
54
 #include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
	char wantToPlay = 'y';
	char rock;
	char paper;
	char scissors;
	char compu;
	string choice;
	


	cout << "This is an old fashion Rock, Paper, Scissors game, want to play? (Y/N)" << endl;
	cin >> wantToPlay;

	 if (wantToPlay == 'y' || wantToPlay == 'Y')
	 {
		cout << "Good luck! and have fun." << endl;
	
	 }
	 else
	 {
		cout << "Hope you can play soon." << endl;
	 }
 
	
	 cout << "Choose: Rock, Paper Or Scissors." << endl;
	 cin >> choice;
	 cout << "Heres mi draw." << endl;
	 compu = rand() % 3 + 1;
	 

	 if (choice == rock && compu = scissors) || (choice == scissors && compu == paper) || 
		(choice == paper && compu == rock)) 
          {
		cout << "You win!!" << endl;
          }
	else
	 {
	    cout << "You lose" << endl;
	 }
     
     system("PAUSE");
	 return EXIT_SUCCESS;
}





Last edited on
@anaird

A few problems.
Your using chars, but looking for a string. You declare the char variables, but don't assign them to anything, and you also looking for a tie. I got your code working, but had to change a few things.

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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
	char wantToPlay = 'y';
	string YourChoice;
	string rock = "Rock";
	string paper = "Paper";
	string scissors = "Scissors";
	string compu;
	string CompuChoice[3] = { "Rock", "Paper", "Scissors" };



	cout << "This is an old fashion Rock, Paper, Scissors game, want to play? (Y/N)" << endl;
	cin >> wantToPlay;

	if (wantToPlay == 'y' || wantToPlay == 'Y')
	{
		cout << "Good luck! and have fun." << endl;

	}
	else
	{
		cout << "Hope you can play soon." << endl;
	}
	
	cout << "Choose: Rock, Paper Or Scissors." << endl;
	cin >> YourChoice;
	cout << "Heres my draw." << endl;
	compu = CompuChoice[rand() % 3];
	cout << compu << endl;
	if (YourChoice == compu)
		cout << "It's a tie!!" << endl;
	else if ((YourChoice == rock && compu == scissors) || (YourChoice == scissors && compu == paper) ||
		(YourChoice == paper && compu == rock))
	{
		cout << "You win!!" << endl;
	}
	else
	{
		cout << "You lose" << endl;
	}

	system("PAUSE");
	return EXIT_SUCCESS;
}
@whitenite1

The code runs perfectly but the computer always chooses scissors.
@anaird

That's because each time you run the program, rand() uses the same sequence of numbers. Try adding #include <ctime> after #include <string> , then put srand((unsigned)time(0)); right after int main() {

That will truly randomize the number.
Last edited on
Topic archived. No new replies allowed.