Keeping track of variables after being called.

So right now, I'm going through an introduction phase to enumerations, structures and cases with Conio. I'm creating a rock-paper-scissors game and I've almost perfectly got it. I just need one thing to make it complete:

I need the program to be able to remember how many wins, losses or draws that player has had since the program started. Can you offer suggestions on how to make it remember the increments made to the ints playerWins, computerWins and draws? Here's what I have so far:

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! \brief A Conio code recreation of the rock paper scissors game, basic stat tracking of wins while the executable is running and an introduction to enumerations.

//include the c standard library where EXIT_SUCCESS is declared
#include <cstdlib>
#include <ctime>

// include the standard input/output library
#include <iostream>

// declare the functions used from iostream
using std::cout;
using std::endl;

#include <constream>
using namespace conio;

// make an enumeration for the three items - 0, 1, 2
enum RPS
{
    ROCK,
    PAPER,
    SCISSORS
};

enum Player
{
    HUMAN,
    COMPUTER,
    DRAW
};

int main()
{
    // set the bg colour to green and clear the screen
    cout << setbk( BLUE ) << clrscr;
    // output text at x, y location
    cout << setxy( 20, 10 ) << setclr( LIGHTGREEN ) << "Do you want to play Rock Paper Scissors?";
    cout << setxy( 20, 12 ) << setclr( WHITE ) << "Press P to Play.";
    // this will convert a key to the upper case version of it.
    char ch = toupper( getch() );
    if( ch  != 'P' )
    {
        //clean up screen and goodbye
        cout << setbk( BLACK ) << setclr( WHITE ) << clrscr;
        return EXIT_SUCCESS;
    }

    cout << setbk( RED ) << clrscr;
    //the game controller boolean
    bool running = true;

    while( running )
    {
        // RNG to determine if the computer got R, P or S
        int computer = time( NULL ) % 3;
        // switch - can only test integer numbers
        cout << setxy( 12, 8 ) << setclr( YELLOW ) << "Press R, P or S for Rock, Paper or Scissors respectively:  ";
        char ch = toupper( getch() );
        // Print out their choice so they can confirm what they entered
        cout << setclr( WHITE );
        switch ( ch )
        {
        case 'R':
            cout << "ROCK     ";
            break;
        case 'P':
            cout << "PAPER    ";
            break;
        case 'S':
            cout << "SCISSORS ";
            break;
        }

        // calculate the winner
        Player winner = DRAW;
        int playerWins = 0;
        int computerWins = 0;
        int draws = 0;
        // display the computer's draw
        cout << setxy( 20, 12 ) << setclr( LIGHTGREEN ) << "Computer chose: ";
        switch ( computer )
        {
        case ROCK:
            cout << "ROCK      ";
            if( ch == 'P' )
            {
                winner = HUMAN;
                cout << "You win!       ";
                ++ playerWins;
            }
            else if( ch == 'S' )
            {
                winner = COMPUTER;
                cout << "Computer wins! ";
                ++ computerWins;
            }
            else if( ch == 'R' )
            {
                winner = DRAW;
                cout << "It's a draw!   ";
                ++ draws;
            }
            break;

        case PAPER:
            cout << "PAPER    ";
            if( ch == 'S' )
            {
                winner = HUMAN;
                cout << "You win!       ";
                ++ playerWins;
            }
            else if( ch == 'R' )
            {
                winner = COMPUTER;
                cout << "Computer wins! ";
                ++ computerWins;
            }
            else if( ch == 'P' )
            {
                winner = DRAW;
                cout << "It's a draw!   ";
                ++ draws;
            }
            break;

        case SCISSORS:
            cout << "SCISSORS ";
            if( ch == 'R' )
            {
                winner = HUMAN;
                cout << "You win!       ";
                ++ playerWins;
            }
            else if( ch == 'P' )
            {
                winner = COMPUTER;
                cout << "Computer wins! ";
                ++ computerWins;
            }
            else if( ch == 'S' )
            {
                winner = DRAW;
                cout << "It's a draw!   ";
                ++ draws;
            }
            break;
        }
        cout << setxy( 20, 14 ) << "PLAYER WINS: " << playerWins << endl;
        cout << setxy( 20, 15 ) << "COMPUTER WINS: " << computerWins << endl;
        cout << setxy( 20, 16 ) << "DRAWS: " << draws << endl;
    }

    // wait for key press, prevents end of app. message
    getch();
    // return a success value of 0 to the OS
    return EXIT_SUCCESS;
}


Would be much appreciated!
You are creating variables playerWins etc. in your while loop. That means, they would be dectroyed and recreated again each time when you enter the loop. Declare them before the loop and all should work fine.
Last edited on
... Can't believe I didn't see that. Thank you, much appreciated.
Topic archived. No new replies allowed.