How to change the value of class object

Why is it that when I "cout" out a member variable within a class, it will only "cout" out its default value?...even if it has been changed....at least I think? The value of the member variable: m_hiddenNumber, which is private, changes only in the main function but not inside the class. I have found a way around my problem...[passed the value of the member variable via a function and an object as a second parameter to the function below]. But, I would still like to know if a member variable's value can be "couted" out to a different value besides its initial value. Thanks

m_hiddenNumber is the variable that remains 0

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
int EvaluatePlayersGuess(int x)//lookup/ask programmer how to make x for this function equal zero
{
                if(x == m_hiddenNumber)
                {
                        SetPoints(75);
                        cout<<"+75  ";
                }

                if((x == m_hiddenNumber -1) || (x == m_hiddenNumber +1))
                {
                        SetPoints(25);
                        cout<<"+25  ";
                }

                if((x >= m_hiddenNumber - 5) && (x <= m_hiddenNumber + 5) && (x != m_hiddenNumber))
                {
                        SetPoints(20);
                        cout<<"+20  ";
                }

                if((x >= m_hiddenNumber - 10) && (x <= m_hiddenNumber + 10) && (x != m_hiddenNumber))
                {
                        SetPoints(10);
                        cout<<"+10  ";
                }

                if((x >= m_hiddenNumber - 30) && (x <= m_hiddenNumber + 30) && (x != m_hiddenNumber))
                {
                        SetPoints(5);
                        cout<<"+5  ";
                }
}



These two functions below are what actually changes the member variable...


1
2
3
4
5
6
7
8
9
10
        void SetRandomNumber(int& x)//changes hidden number, but only back in main. m_hiddenNumber = 0
        {
                x = rand() % 20 + 1;
                m_hiddenNumber = x;
        }

        int GetRandomNumber()
        {
                return m_hiddenNumber;
        }



The new code below works but I still would like to know if the
 value of a member variable can be changed



I had to change the call function in main to this for it to work:

p[i].EvaluatePlayersGuess(guessToRandomNumber, universal.GetRandomNumber());



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
        int EvaluatePlayersGuess(int x, int y)
        {
                if(x == y)
                {
                        SetPoints(75);
                        cout<<"+75  ";
                }

                if((x == y -1) || (x == y +1))
                {
                        SetPoints(25);
                        cout<<"+25  ";
                }

                if((x >= y - 5) && (x <= y + 5) && (x != y))
                {
                        SetPoints(20);
                        cout<<"+20  ";
                }

                if((x >= y - 10) && (x <= y + 10) && (x != y))
                {
                        SetPoints(10);
                        cout<<"+10  ";
                }

                if((x >= y - 30) && (x <= y + 30) && (x != y))
                {
                        SetPoints(5);
                        cout<<"+5  ";
                }
        }
yes you can change a member variable
no I don't know why you can't, you didn't show your whole code, just fragments.
The problem is line 31. The function call is on line 221.


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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
class GET_SET_CLASS/**     ______________________________ M.Y   F.I.R.S.T   C.L.A.S.S ______________________________     **/
{
    public:

        void SetPoints(int x)
        {
                m_points = m_points + x;
        }

        int GetPoints()
        {
                return m_points;
        }

        void SetPlayerName()
        {
                cout<<"What is your name: ";
                getline(cin, m_name);
                while(m_name == "")
                {
                    cout<<"Please enter a name: ";
                    getline(cin, m_name);
                }
        }

        string GetPlayerName()
        {
                return m_name;
        }

        int EvaluatePlayersGuess(int x)//make x for this function equal zero. If it can't the script will have to go in main
        {
                if(x == m_hiddenNumber)
                {
                        SetPoints(75);
                        cout<<"+75  ";
                }

                if((x == m_hiddenNumber -1) || (x == m_hiddenNumber +1))
                {
                        SetPoints(25);
                        cout<<"+25  ";
                }

                if((x >= m_hiddenNumber - 5) && (x <= m_hiddenNumber + 5) && (x != m_hiddenNumber))
                {
                        SetPoints(20);
                        cout<<"+20  ";
                }

                if((x >= m_hiddenNumber - 10) && (x <= m_hiddenNumber + 10) && (x != m_hiddenNumber))
                {
                        SetPoints(10);
                        cout<<"+10  ";
                }

                if((x >= m_hiddenNumber - 30) && (x <= m_hiddenNumber + 30) && (x != m_hiddenNumber))
                {
                        SetPoints(5);
                        cout<<"+5  ";
                }
        }

        void SetRandomNumber(int& x)//this function does not change the hidden number. hidden number is = 0
        {
                x = rand() % 20 + 1;
                m_hiddenNumber = x;
        }

        int GetRandomNumber()
        {
                return m_hiddenNumber;
        }

        GET_SET_CLASS()
        {
                m_hiddenNumber = 0;
                m_points = 0;
        }

    private:

        int m_points;
        int m_hiddenNumber;
        int m_numOfPlayersToPlay;
        string m_name;
};





/* PROBLEMS not solved yet *

- make the game end when someone wins
- points are not tallying up right
- make a function to output hints to the player that guessed close to the number the next time around

*/

int main()/**     ________________________________________ T.H.E   M.A.I.N   F.U.N.C.T.I.O.N ________________________________________     **/
{

    GET_SET_CLASS universal;
    GET_SET_CLASS p[4];
    char returnToMainMenu, playAgain;

    do
    {
        int guessToRandomNumber;
        int numOfPlayersPlayingTheGame;
        int a=0;
        int r=0;///round loop variable
        int i=0;///use this for loops
        int hiddenNumber;

        cout<<"\t________________ Welcome to * Who Guessed The Best * ________________"<<endl<<endl;
        universal.Instructions();
        cout<<endl<<endl;

        cout<<"\"Who Guessed The Best\" is a 4 player game\n\n"
            <<"How many players will be playing? ";
        cin>>numOfPlayersPlayingTheGame;
        cin.ignore(80,'\n');
        cout<<endl;

        while(numOfPlayersPlayingTheGame > 4 || numOfPlayersPlayingTheGame < 1)///fail check for entering number of players
        {
            cout<<endl;
            cout<<"Sorry...Only 1 through 4 players can play\n"
                <<"Enter again: ";
            cin>>numOfPlayersPlayingTheGame;
            cin.ignore(80,'\n');
        }

        cout<<endl;

        ///Loop that will set all of the player[s] names and is coded so PLAYERS CANNOT ENTER THE SAME NAME
        for(i=0; i<numOfPlayersPlayingTheGame; i++)
        {
            cout<<"Player "<<i+1<<" ";
            p[i].SetPlayerName();
            if(i>0)
                while(p[i].GetPlayerName()==p[i-1].GetPlayerName())
                {
                    cout<<endl<<"-- Players cannot have the same name --"<<endl;
                    p[i].SetPlayerName();
                }
            if(i>1)
                while(p[i].GetPlayerName()==p[i-2].GetPlayerName() || p[i].GetPlayerName()==p[i-1].GetPlayerName())
                {
                    cout<<endl<<"-- Players cannot have the same name --"<<endl;
                    p[i].SetPlayerName();
                }
            if(i>2)
                while(p[i].GetPlayerName()==p[i-3].GetPlayerName() || p[i].GetPlayerName()==p[i-2].GetPlayerName()|| p[i].GetPlayerName()==p[i-1].GetPlayerName())
                {
                    cout<<endl<<"-- Players cannot have the same name --"<<endl;
                    p[i].SetPlayerName();
                }
        }

        cout<<endl<<endl<<endl;
        for(i=0; i<numOfPlayersPlayingTheGame; i++)//delete this after I get the program up and running
        {
            cout<<"Player "<<i+1<<"'s name is ";
            cout<<p[i].GetPlayerName();
            cout<<endl<<endl;
        }

        cout<<endl<<endl;
        srand(time(0));

        do
        {
            if(numOfPlayersPlayingTheGame==1)
            {
                cout<<"Press enter when you are ready "<<p[0].GetPlayerName()<<endl<<endl;
                cout<<"-- Press Enter to Continue --";
                if(a > 0)
                    cin.ignore();///DAMAGE  CONTROL! I only used this second cin.ignore() because it will 'pause' for the user the second time around

                cin.ignore();
                a++;
            }

            else if(numOfPlayersPlayingTheGame>1)
            {
                cout<<"-- Press enter to continue when everyone is ready --";
                if(a > 0)
                    cin.ignore();///DAMAGE  CONTROL! same as the above cin.ignore()

                cin.ignore();
                a++;
            }

            cout<<endl<<endl<<endl;
            for(r=0; r<5;r++)
            {
                cout<<"_______________________ ROUND "<<r+1<<" _______________________"<<endl<<endl;

                hiddenNumber = rand() % 20 + 1;
                universal.SetRandomNumber(hiddenNumber);
                cout<<endl<<endl<<"The hidden number is: "<<universal.GetRandomNumber()<<endl<<endl<<endl;//turn the hidden number into [???]

                /**_____________________________ THE   START   OF   THE   PLAYERS   GUESSING   THE   NUMBERS _____________________________**/
                for(i=0; i<numOfPlayersPlayingTheGame;i++)
                {
                    cout<<p[i].GetPlayerName()<<"'s Turn"<<endl<<"Can you guess the hidden number?... ";

                    cin>>guessToRandomNumber;
                    while(guessToRandomNumber > 20 || guessToRandomNumber < 0)
                    {
                        cout<<"The number entered was out of range. Enter a number again: ";
                        cin>>guessToRandomNumber;
                        cout<<endl;
                    }

                    cout<<endl<<endl;

                    p[i].EvaluatePlayersGuess(guessToRandomNumber);

                    if(guessToRandomNumber==universal.GetRandomNumber())
                    {
you mean line 203?

1
2
hiddenNumber = rand() % 20 + 1; //this is already a random number, so why are you randomizing it again anyway?
universal.SetRandomNumber(hiddenNumber);


 
p[i].EvaluatePlayersGuess(guessToRandomNumber);


you haven't set the hidden number for the player, you set it for universal, so how is the player's function supposed to know what it is?
Hey thanks for the reply. I randomized that number again out of pure curiosity to see if that would help change the variable m_hiddenNumber in my class from 0 to whatever number the rand function came up with....but it didn't. m_hiddenNumber is still 0. Line 77 makes it so. I see what you mean though by the player not being able to access the hidden # with the object p[i]EvaluatePlayersGuess(guessToRandomNumber);. But this func was just to take a players guess and pit it up against the hidden number back in my func. This is the problem I cannot figure...for example:



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
        int EvaluatePlayersGuess(int x)
        {
                cout<<endl<<endl<<m_hiddenNumber<<endl<<endl;//This stays 0 
                
                /** hidden number will be equaled to zero if I output it from here which is why whenever I evaluate x [the players guess]
               to the m_hiddenNumber, the player will only recieve points if they enter zero. If I cout out the other variable m_points inside a
               function I will also receive a value of 0 even though it is being changed also.**/
                
                if(x == m_hiddenNumber)
                {
                        SetPoints(75);
                        cout<<"+75  ";
                }

                if((x == m_hiddenNumber -1) || (x == m_hiddenNumber +1))
                {
                        SetPoints(25);
                        cout<<"+25  ";
                }

                if((x >= m_hiddenNumber - 5) && (x <= m_hiddenNumber + 5) && (x != m_hiddenNumber))
                {
                        SetPoints(20);
                        cout<<"+20  ";
                }

                if((x >= m_hiddenNumber - 10) && (x <= m_hiddenNumber + 10) && (x != m_hiddenNumber))
                {
                        SetPoints(10);
                        cout<<"+10  ";
                }

                if((x >= m_hiddenNumber - 30) && (x <= m_hiddenNumber + 30) && (x != m_hiddenNumber))
                {
                        SetPoints(5);
                        cout<<"+5  ";
                }
        }   



How can I get the function to recognize any change in a member variable?
but it isn't a member variable of the player, its a member variable of universal, you can't expect a function inside of a player to know the value of something inside of universal, either pass the hidden number to the player from universal, or figure something else out.
Alright thanks.
Topic archived. No new replies allowed.