rock, paper, scissors game

What am i doing wrong with my code?
#include <iostream>
using namespace std;

#include <ctime>
#include <cstdlib>

char usersMove()
{

char user = 'Q';
do
{
cout << "Welcome to the game of Rock, Paper, Scissors." << endl;
cout << "Please chose your weapon: [R for rock, P for paper, S for scissors, and Q to quit]: " << endl;
cin >> user;
cin.ignore(1000, 10);

if (user == 'R' || user == 'r' || user == 'P' || user == 'p' || user == 'S' || user == 's')
if (user == 'Q' || user == 'q') break;

{
return tolower(user);
}
}
}
while (true)
{
char comp()

{

int compChoice = rand() % 3;
if (compChoice == 0) return 'r';
else if (compChoice == 1) return 'p';

return 's';
}

int whoWins(char userPick, compPick)
{
if (userPick == 'R' || userPick == 'r')
{
if (compPick == 'R' || compPick == 'r')
return 2;

else if (compPick == 'P' || compPick == 'p')
return 1;
else
return 0;
}

else if (userPick == 'P' || userPick == 'p')
{

if (compPick == 'P' || compPick == 'p')
return 2;

else if (compPick == 'S' || compPick == 's')
return 1;
else
return 0;
}

else if (userPick == 'S' || userPick =='s')
{

if (compPick == 'S' || compPick == 's')
return 2;

else if (compPick == 'R' || compPick == 'r')
return 1;
else
return 0;
}

return 0;
}}

int main()
{
srand(time(0));

char user, comp;
int winner;

user = usersMove();
comp = comp();

cout << "The computer chose " << comp;
winner = whoWins(user, comp);

if (whoWins == 0)
cout << "You win!";

else if (whoWins == 1)
cout << "You lost.. ";

else
cout << "You tied";


return 0;

}

Please place your code within [code][/code] tags.
ok now I have fixed everything except now I am getting a ISO C++ forbids comparison between pointers and integers heres the new code

#include <iostream>
using namespace std;

#include <ctime>
#include <cstdlib>

char usersMove()
{

char user = 'Q';
do
{
cout << "Welcome to the game of Rock, Paper, Scissors." << endl;
cout << "Please chose your weapon: [R for rock, P for paper, S for scissors, and Q to quit]: " << endl;
cin >> user;
cin.ignore(1000, 10);

if (user == 'R' || user == 'r' || user == 'P' || user == 'p' || user == 'S' || user == 's');
if (user == 'Q' || user == 'q') break;

{
return tolower(user);
}
}

while (true);
}
char comp()

{

int compChoice = rand() % 3;
if (compChoice == 0) return 'r';
else if (compChoice == 1) return 'p';

return 's';
}

int whoWins(char userPick,char compPick)
{
if (userPick == 'R' || userPick == 'r')
{
if (compPick == 'R' || compPick == 'r')
return 2;

else if (compPick == 'P' || compPick == 'p')
return 1;
else
return 0;
}

else if (userPick == 'P' || userPick == 'p')
{

if (compPick == 'P' || compPick == 'p')
return 2;

else if (compPick == 'S' || compPick == 's')
return 1;
else
return 0;
}

else if (userPick == 'S' || userPick =='s')
{

if (compPick == 'S' || compPick == 's')
return 2;

else if (compPick == 'R' || compPick == 'r')
return 1;
else
return 0;
}

return 0;
}

int main()
{
srand(time(0));

char user, ai;
int winner;

user = usersMove();
ai = comp();

cout << "The computer chose " << ai;
winner = whoWins(user, ai);

if (whoWins == 0)
cout << "You win!";

else if (whoWins == 1)
cout << "You lost.. ";

else
cout << "You tied";


return 0;

}

For the second time, please use [CODE] tags...
They never care about code tags... they must all think we have like code vision or something.. Ill put it into an editor and take a look...
ok yeah no. your code is really messed up from what i see. Clean it up and post in using the [code] tags, but from my glance your parenthesis are all over the place.
1
2
3
4
5
if (whoWins == 0)
cout << "You win!";

else if (whoWins == 1)
cout << "You lost.. ";


Here you are comparing whoWins, a function pointer, with an integer.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
using namespace std;

#include <ctime>
#include <cstdlib>

char rockPaperScissors()
{

  char user = 'Q';
  while (true)
  {
    cout << "This is the game rock, paper, scissors." << endl;
    cout << "Choose: (0 for rock, 1 for paper, 2 for scissors or Q to quit):" << endl;
    cin >> user;
    cin.ignore(1000, 10);

    if (user == '0' || user == '1' || user == '2') break;

    if (user == 'Q' || user == 'q') break;

    cout << user << " This was not a valid choice, please select again. " << endl;
    
    return user;
   }
}

   int comp = rand() % 3;

int main()
{
  srand(time(0));
  
  while (true)
  {

    char user;
    user = rockPaperScissors();
    if (user == 'Q' || user == 'q') break;
    
    
    if (user == 0)
    {

      if (comp == 0)
        cout << "You tied, rock vs rock. " << endl;
        cout << endl;
      }
      else if (comp == 1)
      {
        cout << "You lose, paper beats rock." << endl;
        cout << endl;
      }
      else if (comp == 2)
      {
        cout << "You win! rock beats scissors." << endl;
        cout << endl;
    }
      
    
    if (user == 1)
    {

      if (comp == 1)
        cout << "You tied, paper vs paper. " << endl;
        cout << endl;
      }
      else if (comp == 0)
      {
        cout << "You win! paper beats rock. " << endl;
        cout << endl;
      }
      else if (comp == 2)
      {
        cout << "You lose, scissors beats paper." << endl;

    }
      
    if (user = 2)
    {
      
      if (comp = 2)
        cout << "You tied, scissors vs scissors. " << endl;
        cout << endl;
      }
      else if (comp = 0)
      {
        cout << "You lose, rock beats scissors." << endl;
        cout << endl;
       }
      else if (comp = 1)
      {
        cout << "You win! scissors beats paper." << endl;
        cout << endl;
    
}
}
    

    
    


  return 0;
}
PROBLEM IS rockPaperScissors() FUNCTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char rockPaperScissors()

  while (true) {
    char user = 'Q';
    cout << "This is the game rock, paper, scissors." << endl;
again:
    cout << "Choose: (0 for rock, 1 for paper, 2 for scissors or Q to quit):" << endl;
    cin >> user;
    cin.ignore(1000, 10);

    if (user == '0' || user == '1' || user == '2') return user; //NOT BREAK AS IT WILL RETURN Q (QUIT)

    if (user == 'Q' || user == 'q') break;  //HERE BREAK IS OK

    cout << user << " This was not a valid choice, please select again. " << endl;
    goto again;   //NOT BREAK AS IT WILL RETURN ANYTHING
   }
Last edited on
fixed that but still getting multiple results in who wins or ties.... get the correct answer followed by a random such as..
Choose: (0 for rock, 1 for paper, 2 for scissors or Q to quit):
0
You lose, paper beats rock.

You tied, scissors vs scissors.

heres new code
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
using namespace std;

#include <ctime>
#include <cstdlib>

char rockPaperScissors()
{

  char user = 'Q';
  while (true)
  {
    cout << "This is the game rock, paper, scissors." << endl;
  again:
    cout << "Choose: (0 for rock, 1 for paper, 2 for scissors or Q to quit):" << endl;
    cin >> user;
    cin.ignore(1000, 10);

    if (user == '0' || user == '1' || user == '2') return user;

    if (user == 'Q' || user == 'q') break;

    cout << user << " This was not a valid choice, please select again. " << endl;
    
    goto again;
   }
}

   int comp = rand() % 3;

int main()
{
  srand(time(0));
  
  while (true)
  {

    char user;
    user = rockPaperScissors();
    if (user == 'Q' || user == 'q') break;
    
    if (user == 0)
    {

      if (comp == 0)
        cout << "You tied, rock vs rock. " << endl;
        cout << endl;
      }
      else if (comp == 1)
      {
        cout << "You lose, paper beats rock." << endl;
        cout << endl;
      }
      else if (comp == 2)
      {
        cout << "You win! rock beats scissors." << endl;
        cout << endl;
    }
      
    
    if (user == 1)
    {

      if (comp == 1)
        cout << "You tied, paper vs paper. " << endl;
        cout << endl;
      }
      else if (comp == 0)
      {
        cout << "You win! paper beats rock. " << endl;
        cout << endl;
      }
      else if (comp == 2)
      {
        cout << "You lose, scissors beats paper." << endl;

    }
      
    if (user = 2)
    {
      
      if (comp = 2)
        cout << "You tied, scissors vs scissors. " << endl;
        cout << endl;
      }
      else if (comp = 0)
      {
        cout << "You lose, rock beats scissors." << endl;
        cout << endl;
       }
      else if (comp = 1)
      {
        cout << "You win! scissors beats paper." << endl;
        cout << endl;
    
}
}
    

    
    


  return 0;
}

break things up into functions it will help ALOT. oh and for future reference only put the parts of the code that are getting errors so that other people do not have to wade through your code.
phew.. this took a while to get your code to work.. alot of editing =P be more observant about your brackets, and remember that you made the user a charactor variable, not an int
and characters are put between single quotes, like this 'a'
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

char rockPaperScissors()
{

  char user = 'Q';
  while (true)
  {
    cout << "This is the game rock, paper, scissors." << endl;
    again:
    cout << "Choose: (0 for rock, 1 for paper, 2 for scissors or Q to quit):" << endl;
    cin >> user;
    cin.ignore(1000, 10);

    if (user == '0' || user == '1' || user == '2') return user;

    if (user == 'Q' || user == 'q') break;

    cout << user << " This was not a valid choice, please select again. " << endl;

    goto again;
   }
   return 'q';
}

   int comp = rand() % 3;

int main()
{
  srand(time(0));

  while (true)
  {

    char user;
    user = rockPaperScissors();
    if (user == 'Q' || user == 'q') break;

    if (user == '0')  // charactor, not int like in your program
    {

      if (comp == 0)
      {
        cout << "You tied, rock vs rock. " << endl;
        cout << endl;
      }
      else if (comp == 1)
      {
        cout << "You lose, paper beats rock." << endl;
        cout << endl;
      }
      else if (comp == 2)
      {
        cout << "You win! rock beats scissors." << endl;
        cout << endl;
      }
  }


    if (user == '1')    // charactor, not int like in your program
    {

      if (comp == 1)
      {
        cout << "You tied, paper vs paper. " << endl;
        cout << endl;
      }
      else if (comp == 0)
      {
        cout << "You win! paper beats rock. " << endl;
        cout << endl;
      }
      else if (comp == 2)
      {
        cout << "You lose, scissors beats paper." << endl;
      }
    }

    if (user == '2')    // charactor, not int like in your program
    {

      if (comp == 2)  // you forgot that comparisons use double =='s
      {
        cout << "You tied, scissors vs scissors. " << endl;
        cout << endl;
      }
      else if (comp == 0)  // you forgot that comparisons use double =='s
      {
        cout << "You lose, rock beats scissors." << endl;
        cout << endl;
      }
      else if (comp == 1) // you forgot that comparisons use double =='s
      {
        cout << "You win! scissors beats paper." << endl;
        cout << endl;
      }

    }
}
  return 0;
}
Last edited on
I coded my own rock paper scissors game after reading this just for fun and practice. Let me know what you think and if you think I could have done anything better. I could have broken this down into functions, but I don't see the point with how simple and short this code is.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <ctype.h>
#define EVER ;;

using namespace std;

int main ()
{
	enum GameOptions { ROCK, PAPER, SCISSORS };
	char myChoice, playAgain;
	int compChoice;
	bool shouldQuit = false;
	
	for(EVER)
	{
		if (shouldQuit) 
			break;
		{
			cout << "Pick your weapon [R]ock [P]aper [S]cissors or [Q]uit: ";
			cin >> myChoice;
			myChoice = tolower(myChoice);
			compChoice = rand() % 3;

			if (myChoice == 'q')
			{
				shouldQuit = true;
				break;
			} 

			switch(compChoice)
			{
			case ROCK:
				if (myChoice == 'r') 
					cout << "Computer chose 'ROCK'... it's a tie" << endl;
				if (myChoice == 'p')
					cout << "Computer chose 'ROCK'... you win" << endl;
				if (myChoice == 's')
					cout << "Computer chose 'ROCK'... you lose" << endl;
				break;
			case PAPER:
				if (myChoice == 'r') 
					cout << "Computer chose 'PAPER'... you lose" << endl;
				if (myChoice == 'p')
					cout << "Computer chose 'PAPER'... it's a tie" << endl;
				if (myChoice == 's')
					cout << "Computer chose 'PAPER'... you win" << endl;
				break;
			case SCISSORS:
				if (myChoice == 'r') 
					cout << "Computer chose 'SCISSORS'... you win" << endl;
				if (myChoice == 'p')
					cout << "Computer chose 'SCISSORS'... you lose" << endl;
				if (myChoice == 's')
					cout << "Computer chose 'SCISSORS'... it's a tie" << endl;
				break;
			default:
				cout << "ERROR: Please try again";
				break;
			}

			cout << "Would you like to play again (y or n): ";
			cin >> playAgain;
			playAgain = tolower(playAgain);
			
			switch(playAgain)
			{
			case 'y':
				system("CLS");
				break;
			case 'n':
				shouldQuit = true;
				break;
			default:
				cout << "Invalid choice! Restarting game...";
				system("CLS");
				break;
			}
		}
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.