New to C++, code written, look at code 4 me?

Like the title says I'm a student, new to C++, I've written this app that plays this flippin rock, paper, scissors game but when you run it you have to type in a number to get anything to work. Where did I go wrong.........? Also not sure on where to install the wins = 0/1/2/3/etc.....

Any help is appreciated, if not delete it and I'll figure it out.......thank you for your time.




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
#include <cstdlib>
#include <string>
#include <iostream>
#include <ctime> 


using namespace std;

/*
 * main entry point
 */

char readAndValidateUserMove() {

    char Result; // to capture our result we have to use this........
    do {
        cin >> Result; // get input from user
        if (Result == '1' || Result == '2' || Result == '3') {
            return tolower(Result);
        }
        cin.clear();
        cin.ignore();
    } while (true); // if we get correct input, we'll leave this with the return statement
}
//generate the computer's guess by random.........
char generateCompMove() {
    int nRandNum = rand() % 3; // 0-2
    if (nRandNum == 0)
        return '1';
    else if (nRandNum == 1)
        return '2';

    return '3';
}

//compare moves..............
int CompareMoves(char playerMove, char compMove) {
    // 0 - player wins
    // 1 - ai wins
    // 2 tie
    if (playerMove == '1') {
        if (compMove == '1')
            return 2;
        else if (compMove == '2')
            return 1;
        else
            return 0;
    } else if (playerMove == '2') {
        if (compMove == '2')
            return 2;
        else if (compMove == '1')
            return 1;
        else
            return 0;
    } else if (playerMove == '3') {
        if (compMove == '3')
            return 2;
        else if (compMove == '1')
            return 1;
        else
            return 0;
    }
    return 0; // if we get here, something earlier went wrong
}

int main(int argc, char** argv) {
    srand((unsigned) time(0)); // make sure you do this, or rand will return the same result every time
    //variables declared
    char player, comp;
    int result;
    string name;
    player = readAndValidateUserMove();
    comp = generateCompMove();


    //prompt for users name
    cout << "What is your name?" <<endl;
            cin >> name;
    //output a hello message
    cout << "Hello " << name << "!  Lets play rock, paper scissors!" <<endl;
    cout << "First there are 3 rules: 1. Rock breaks Scissors || Scissors cuts Paper || Paper wraps Rock; Choose: 1 for Rock, 2 for Paper, 3 for Scissors. Enter your choice as a number 1, 2 or 3" <<endl;
    cout << "Please enter your selection now!" <<endl;

    cout << "Computer plays " <<endl;
            cin >> comp;
    result = CompareMoves(player, comp);
    if (result == 0)
        cout << "You win! Would you like to play again?";
    else if (result == 1)
        cout << "You lost. Would you like to play again?";
    else
        cout << "Its a Tie. Would you like to play again?";
    return 0;
}
Last edited on
You be better putting the game part of your code into a loop i.e.

1
2
3
4
5
6
7
while(userInput != "N")
{
//Game code

cout << "would you like to play again??";
cin >> userInput;
}


That way you could implement a system where wach time the player wins you increment the players counter and when the computer wins increase it's counter etc...

hope this helps
Last edited on
I have made a few changes:
*changed some things in the main method
**moved the calls to readAndValidateUserMove() and generateCompMove();
**added a loop around everything to keep it going if the user enters Y or y
**added an if-else if-else block to output the choice the computer made in text form
*changed the compareMoves method to make it more legible
**noticed a bug when playerMove=='2' and compMove=='1' (it made the wrong person win)
*changed readAndValidateUserMove method by moving the cin.clear() and cin.ignore()
*also changed generateCompMove method just to get rid of some if statements

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
106
107
#include <cstdlib>
#include <string>
#include <iostream>
#include <ctime> 


using namespace std;

char readAndValidateUserMove()
{
    char Result; // to capture our result we have to use this........
    do {
        cin >> Result; // get input from user
        cin.clear();
        cin.ignore();
        if (Result == '1' || Result == '2' || Result == '3')
            return tolower(Result);
    } while (true); // if we get correct input, we'll leave this with the return statement
}
//generate the computer's guess by random.........
char generateCompMove()
{
    return 48 + rand() % 3 + 1; // returns 1 to 3 in ASCII character form
}

//compare moves..............
int CompareMoves(char playerMove, char compMove)
{
    // 0 - player wins
    // 1 - ai wins
    // 2 tie
    if (playerMove == compMove)
        return 2;
    else if (playerMove == '1')
    {
        if (compMove == '2')
            return 1;
        else
            return 0;
    }
    else if (playerMove == '2')
    {
        if (compMove == '1')
            return 0;
        else
            return 1;
    }
    else if (playerMove == '3')
    {
        if (compMove == '1')
            return 1;
        else
            return 0;
    }
    return 0; // if we get here, something earlier went wrong
}

/*
 * main entry point
 */

int main(int argc, char** argv)
{
    srand((unsigned) time(0)); // make sure you do this, or rand will return the same result every time
    //variables declared
    char player, comp, choice;
    int result;
    string name;

    do
    {
        //prompt for users name
        cout << "What is your name?" << endl;
                cin >> name;
        //output a hello message
        cout << endl << "Hello " << name << "!  Lets play rock, paper scissors!" << endl;
        //output rules message
        cout << "First there are 3 rules: 1. Rock breaks Scissors || Scissors cuts Paper || Paper wraps Rock"
             << endl << endl << "Now Choose: 1 for Rock, 2 for Paper, 3 for Scissors. Enter your choice as a number 1, 2 or 3" << endl;
        //prompt for an integer between 1 and 3
        cout << "Please enter your selection now!" << endl;
        
        player = readAndValidateUserMove(); //this function gets legal user input between 1 and 3 and stores to player

        cout << endl << "Computer plays ";
        comp = generateCompMove();
        if (comp == '1')
            cout << "Rock";
        else if (comp == '2')
            cout << "Paper";
        else
            cout << "Scissors";
        cout << endl << endl;
        
        result = CompareMoves(player, comp);
        if (result == 0)
            cout << "You win!";
        else if (result == 1)
            cout << "You lost.";
        else
            cout << "Its a Tie.";
            
        cout << endl << endl << "Would you like to play again (Y/N)?";
        cin >> choice;
    } while(tolower(choice) == 'y');
    return 0;
}
Wow, lessons learned..............thank you very much for the help, I had some things messed up for sure. Thank you for the help. Obviously I had things in the wrong spots already, but miraculously it worked, hahahaha.

Where would I ad the wins total stuff within this program.............

Thanks again.
I would add it in the:
1
2
3
4
5
6
7
        result = CompareMoves(player, comp);
        if (result == 0)
            cout << "You win!";
        else if (result == 1)
            cout << "You lost.";
        else
            cout << "Its a Tie.";

Maybe something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        result = CompareMoves(player, comp);
        if (result == 0)
        {
            cout << "You win!";
            totalPlayerWins++;
        }
        else if (result == 1)
        {
            cout << "You lost.";
            totalCompWins++;
        }
        else
        {
            cout << "Its a Tie.";
            totalTies++;
        }

Remember to declare the new variables I referenced at the top:
int totalCompWins = 0, totalPlayerWins = 0, totalTies = 0;
Awesome, thank you very much, I appreciate the help. It looked more complicated for me and staring at this code for a couple of hours I was beat with it.

Thank you again!
Topic archived. No new replies allowed.