how to have full words instead of initials as the input in this code

The following is the "rock, paper, scissors" game. The user has to enter initials (r, p, s) instead of the full words to play the game. Can anyone change the code so that full words be inserted?

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
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
int main(int argc, char** argv) {
    enum object {rock, paper, scissors};
    object player1, player2;
    cout << "Enter two objetcs (objects include rock, paper or scissors):";
    char p1, p2;
    cin >> p1 >> p2;
    if (p1 == 'r') {
        player1 = rock;
    }
    else if (p1 == 'p') {
        player1 = paper;
    }
    else if (p1 == 's') {
        player1 = scissors;
    }
    else {
        cout << "invalid input, play again" << endl;
        exit(1);
    }

    if (p2 == 'r') {
        player2 = rock;
    }
    else if (p2 == 'p') {
        player2 = paper;
    }
    else if (p2 == 's') {
        player2 = scissors;
    }
    else {
        cout << "invalid input, play again" << endl;
        exit(1);
    }

    if (player1==player2) cout <<"objects are equal";
    else if (player1==rock && player2==paper) cout << "player 2 is the winner";
    else if (player1==rock && player2==scissors) cout<<"player 1 is the winner";
    else if (player1==paper && player2==rock) cout << "player 1 is the winner";
    else if (player1==paper && player2==scissors) cout <<"Palyer 2 is the winnder";
    else if (player1==scissors && player2==paper) cout << "Player 1 is winner";
    else cout <<"Player 2 is the winner";
    cout << endl;
}
Last edited on
Can anyone change the code so that full words be inserted?

Yes, but we're not going to do your homework for you.

Just use strings rather than single characters.
why in the heck would a user want to type all those letters to play? r/p/s seems much more user friendly.

regardless change line 10 to
string p1,p2
and check
if (p1 == "rock") //you can toupper in a transform etc to ensure all possible inputs of the letters "RoCk" work if you want to be robust
Last edited on
Topic archived. No new replies allowed.