Problem with strings in Rock, paper, scissors

Hello everyone, I'm having trouble with the string input for a rock, paper, scissors game and was wondering if someone could help me out? Whenever I run the program and input a choice such as rock, paper, or scissor, nothing happens. Instead, the program just prints out the beginning prompt.

Here is the assignment:
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows.

1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet.)
2. The user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. (You can use a menu if you prefer.)
3. The computer's choice is displayed.
4. A winner is selected and displayed according to the following rules:

If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.)
If one player chooses scissors and the other player chooses paper, then scissors wins. (The scissors cuts paper.)
If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)
If both players make the same choice, the game must be played again to determine the winner

Be sure to divide the program into functions that perform each major task.
Make sure to allow the user to play the game over and over if he wishes.

Here is the code that I have:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
    srand((unsigned)time(0));
    string userChoice;
    int user;
    int compchoice = (rand()%2)+1;
    cout << "Welcome to Rock, Paper, Scissors I assume you know how to play,\n";
    cout << " so lets begin. You are playing against the computer.\n"; 
    cout<<" Please input either rock, paper, or scissors.\n\n";
    cin>>userChoice;
              
    if (userChoice == "rock")
    {user=0;}
    else if (userChoice=="paper")
    {user=1;}
    else if (userChoice=="scissors")
    {user=2;}

    
    if (user==0)
    {
              if (compchoice==0)
              {cout<<endl<<"Computer chose rock"<<endl;
              cout << "It's a tie!\n\n\n\n";
              }
              else if (compchoice == 1)
               {    cout<<endl<<"Computer chose paper"<<endl;
                   cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
                   }        
              else if (compchoice == 2)
                {
                   cout<<endl<<"Computer chose scissors" <<endl;
                   cout << "Rock beats scissors! You win!\n\n\n\n";
                   }  
    }
    
    
    if (user == 1)
    {
               if (compchoice=0)
               {cout<<endl<<"Computer chose paper"<<endl;
                    cout << "Paper beats rock! You win!\n\n\n\n";
                    }
               else if (compchoice == 1)
               {
               cout<<endl<<"Computer chose paper"<<endl;
               cout << "It's a tie!\n\n\n\n";
               }
               else if (compchoice == 2)
               {
                    cout<<endl<<"Computer chose scissors"<<endl;
                    cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
                    }
}
   
  if (user == 2)
 {  
   
             if (compchoice==0)
                      {cout<<endl<<"Computer chose rock"<<endl;
                   cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
                   }
              else if (compchoice == 2)
              {cout<<endl<<"Computer chose scissors"<<endl;
              cout << "It's a tie!\n\n\n\n";
              }
              else if (compchoice == 1)
                   {cout<<endl<<"Computer chose paper"<<endl;
                   cout << "Scissors beat paper! You win!\n\n\n\n";
                   }
}  
    return main();
}


I've also tried using strcmp but nothing so far has worked. Can someone please give me suggestions as to how this problem can be resolved? Thanks.
Last edited on
1
2
3
4
5
6
    if (userChoice == "rock")
    {user==0;}   /// should be =
    else if (userChoice=="paper")
    {user==1;}   /// should be =
    else if (userChoice=="scissors")
    {user=2;} /// this is correct, program should work for input of "scissors" 


Also, return 0; not return main();
You wrote to type 0 for rock, 1 for paper, and 2 for scissors, but then you compare the user input to "rock", "paper", and "scissors".

Either have the user type "rock", "paper", or "scissors", or keep it as it is and just have the user input be an int.
Thanks vin. The program is running outputs now but when I tested it, I got some strange outputs. For example, when I inputted "rock", the program outputted: "Computer chose rock. Rock beats scissors. You lose."

Edit: Nevermind, see below.
Last edited on
Thanks for the advice, long double main. I just edited the instructions to avoid any future confusion but I'm certain that the user is supposed to input a word rather than a number.

Also vin, return 0 closes out the program since there's no loop. That's why I've kept the return main.

Edit: Fixed output. Everything seems to be working now.
Last edited on
Oh, and if you edited your first post to include the changes you've made, you may have to fix (line 45)
if (compchoice=0) // should be if (compchoice == 0)

Oh, and
my compiler wrote:
warning: ISO C++ forbids taking address of function '::main'

Basically, you can't call main(), so your return main(); line is technically not allowed.

If you want it to loop (indefinitely, until the user force quits), put everything in a while (true) loop.
(except for the first line with the srand((unsigned)time(0)); -- that line should be outside the loop).
Then just return 0 from main() at the end. (outside the loop)
Topic archived. No new replies allowed.