How do I get the variable value from class

closed account (LbX1AqkS)
I'm trying to get my total1 and total2 variables from my class functions and use them freely in main, but I don't know how to alter the scope of these variables

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>   
using namespace std;

class Dice		//Dice class
{
private:
	int dice1Roll, dice2Roll = 0;		//all of the private stored variables that will be accessed by later functions
	

public:

	int total2 = 0;
	int total1 = 0;

	int rollDicePlayer()
	{
		int dice1Roll = rand() % 6 + 1;	// 1 to 6
		int dice2Roll = rand() % 6 + 1;

		if (dice1Roll == 1)
		{
			cout <<" ----- " << endl;
			cout <<"|     |" << endl;
			cout <<"|  0  |" << endl;
			cout <<"|     |" << endl;
			cout <<" ----- " << endl;
		}
		else if (dice1Roll == 2)
		{
			cout << " ----- " << endl;
			cout << "|  0  |" << endl;
			cout << "|     |" << endl;
			cout << "|  0  |" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 3)
		{
			cout << " ----- " << endl;
			cout << "|0    |" << endl;
			cout << "|  0  |" << endl;
			cout << "|    0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 4)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|     |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 5)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|  0  |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 6)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}

		if (dice2Roll == 1)
		{
			cout << " ----- " << endl;
			cout << "|     |" << endl;
			cout << "|  0  |" << endl;
			cout << "|     |" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 2)
		{
			cout << " ----- " << endl;
			cout << "|  0  |" << endl;
			cout << "|     |" << endl;
			cout << "|  0  |" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 3)
		{
			cout << " ----- " << endl;
			cout << "|0    |" << endl;
			cout << "|  0  |" << endl;
			cout << "|    0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 4)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|     |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 5)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|  0  |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 6)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		total1 = dice1Roll + dice2Roll;

		cout << "You have rolled " << total1 << endl << endl;

		return total1;
	}

	int rollDiceComputer()
	{
		dice1Roll = rand() % 6 + 1;	// 1 to 6
		dice2Roll = rand() % 6 + 1;

		if (dice1Roll == 1)
		{
			cout << " ----- " << endl;
			cout << "|     |" << endl;
			cout << "|  0  |" << endl;
			cout << "|     |" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 2)
		{
			cout << " ----- " << endl;
			cout << "|  0  |" << endl;
			cout << "|     |" << endl;
			cout << "|  0  |" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 3)
		{
			cout << " ----- " << endl;
			cout << "|0    |" << endl;
			cout << "|  0  |" << endl;
			cout << "|    0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 4)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|     |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 5)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|  0  |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice1Roll == 6)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}

		if (dice2Roll == 1)
		{
			cout << " ----- " << endl;
			cout << "|     |" << endl;
			cout << "|  0  |" << endl;
			cout << "|     |" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 2)
		{
			cout << " ----- " << endl;
			cout << "|  0  |" << endl;
			cout << "|     |" << endl;
			cout << "|  0  |" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 3)
		{
			cout << " ----- " << endl;
			cout << "|0    |" << endl;
			cout << "|  0  |" << endl;
			cout << "|    0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 4)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|     |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 5)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|  0  |" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}
		else if (dice2Roll == 6)
		{
			cout << " ----- " << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << "|0   0|" << endl;
			cout << " ----- " << endl;
		}

		total2 = dice2Roll + dice1Roll;

		cout << "The computer has rolled " << total2 << endl << endl;

		return total2;
	}
 
};


int main()
{
	srand((unsigned int) time(NULL));
	Dice* d = new Dice();
	int total_rolls = 6;
	int roll = 1;
	int playerScore;
	int computerScore;

	cout << "Ready to roll some dice?" << endl << endl;

	do
	{
		cout << "Roll #" << roll << endl << endl;

		system("pause");

		d->rollDicePlayer();

		system("pause");

		d->rollDiceComputer();

		system("pause");

	} while (roll++ < total_rolls);

	if (playerScore > computerScore)
	{
		cout << "You've won, congragulations" << endl << endl;
	}
	else if (computerScore > playerScore)
	{
		cout << "Sorry, the computer won" << endl << endl;
	}
	delete d;



	system("pause");
	return 0;
}
you're already doing so (lines 257, 261) -- these methods are returning ints. Assign your vars in main to these method calls.

Edit: did you want Dice to keep a running tally of total scores for the human and the computer? e.g. player could score a total of 20 points across 6 games.
If so, then the individual roll methods could just print out what was rolled, but be void methods, secretly adding on to the total. Then separate public methods could look like

1
2
int TotalPlayerScore() { return total_player_score_; }
int TotalComputerScore() { return total_computer_score_; }


where total_player_score_ and total_computer_score_ are the private variables (initialized to 0 in the Dice constructor) that were secretly gaining with each roll.
Last edited on
Hello WouldyCaulk,

I am not sure if you are familiar with using separate file for the class and member functions, but it would be better to split them up.

Starting at the top:

<stdio.h> is a C header file and should not be used in a C++ program. <iostream> is all you need to handle input and output.

<stdlib.h> and <time.h> should be <cstdlib> and <ctime>.

using namespace std; should be avoided.

Your class would work better something like this:
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
#ifndef _DICE_ 
#define _DICE_

class Dice		//Dice class
{
private:
	int dice1Roll, dice2Roll;		//all of the private stored variables that will be accessed by later functions

	int total2;
	int total1;

public:

	Dice();  // <--- ctor.
	~Dice();  // <--- dtor.

	int rollDicePlayer();
	int rollDiceComputer();

// ******* Get functions ******
	int GetDice1Roll();
	int GetDice2Roll();
	int GetTotal1();
	int GetTotal2();

// ******* Set functions ******
	//void SetDice1Roll();  // <--- If needed.
};

#endif // end !_DICE_ 

The variables "total1" and "total2" are better put in the private section. As you have them they are in the :public" section and the entire program has access to them. This tends to defeat the purpose of the class.

This will give you an idea what I did with the "Dice.cpp" file:
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
#include <iostream>
#include <cstdlib>  // <--- Put here for "rand()".

#include "Dice.hpp"  // <--- Sorry for any confusion. I use ".hpp" for C++ header files.

Dice::Dice()  // <--- ctor.
{
	dice1Roll = 0;  // <--- Variables are initialized here.
	dice2Roll = 0;
	total1 = 0;
	total2 = 0;

	// <--- As a one liner.
	//dice1Roll = dice2Roll = total1 = total2 = 0;
}

Dice::~Dice() {}  // <--- dtor.

int Dice::GetDice1Roll()
{
	return dice1Roll;
}

int Dice::GetDice2Roll()
{
	return dice2Roll;
}

int Dice::GetTotal1()
{
	return total1;
}

int Dice::GetTotal2()
{
	return total2;
}

//  These next two functions are what you wrote.
//int Dice::rollDicePlayer()
//{ 

This should give you an idea of what needs to be there. It does not matter if you put this all in the main file or separate files.

For what you have accessing the "total" variables is a matter of "d->total1". When I looked at main closer making "d" a "Dice*" is not necessary unless you have a need to do this that I have not seen yet. Defining "d" as "Dice d;" is all you need to create an object/instance of "Dice". Then to access the public members of the class it would be "d.GetDice1Roll()" or "d.GetTotal1();". To access the "private" members of the class you would use the
Get" and "set" functions.

You should try and avoid using "system" anything. First it is specific to windows, so not everyone can use it fo a pause waiting for a key press I use this:
1
2
3
4
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

Mostly I use this right before the "return" in main to keep the console window before "return" has a chance to end the program. This can be used anywhere you have used "system("pause");".

If you would want a timed pause I use this line:
std::this_thread::sleep_for(std::chrono::seconds()); // Requires header files "chrono" and "thread"

The program does compile and run which is a good start. I still need to go through the program and check some of the variables and may have to make some adjustments to what I have changed.

Hope that helps,

Andy
Hello WouldyCaulk,

When I ran the program I found the major problem is that the functions "rollDice??" both return a result, but in main you are using the return value of the function nor are you giving any variable any value.

You have two variables, in main, "playerScore" and "computerScore" that should be capturing the returned value of the functions. Something like: playerScore += d->rollDicePlayer(); is what I did.

Doing this gave a value to the "???Score" variables so that the if statements following the do/while loop would have values to work with.

I found that int total_rolls = 6; would be better if defined this way: constexpr int TOTALROLLS = 3; // <--- Org 6. I also put this as the first line of main so you do not have to look far to find it to make a change. As you can see I changed it to 3 for testing so I would not have to wait as long. The capital letters help to let you know it was defined as a constant. "constexpr" is the newer version of "const". Either one will work.

While working on the program I realized that the class variables "total?" are not needed because i the functions "rollDice???" you can just
1
2
3
std::cout << "The computer has rolled " << dice2Roll + dice1Roll << std::endl << std::endl;

return dice2Roll + dice1Roll;

Thus eliminating the need for the total variables.

One last note int the functions "rollDice??" you have to sets of code that are the same. This would better in a function and I would make it a member function of the class. This way all you would have to do is call the function with the value of the Die roll to print the correct picture. And each function would call this function twice. I am not saying that you need to change it for this program it is more for the future. The real point here is when something like this that is repeated 4 times is a good candidate for its own function.

Hope that helps,

Andy
Topic archived. No new replies allowed.