Why won't my functions print in main?

closed account (LbX1AqkS)
I have to functions in a class that print out dice, but the dice do not print when I run it in main()

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>   
using namespace std;

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

public:

	void 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 " << endl << endl;
	}

	void 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 " << endl << endl;
	}
 
};


int main()
{
	srand((unsigned int)time(NULL));
	Dice* rolling = new Dice;
	int rollNum = 6;
      
	do
	{
		cout << "Ready to roll some dice?" << endl << endl;


		void rollDicePlayer();

		system("pause");

		void rollDiceComputer();

		system("pause");

		rollNum--;

	} while (rollNum > 0);





	system("pause");
	return 0;
}
 

Sorry for the lengthy code
Last edited on
code length is fine.
Can you please edit with indentation and put [code] ... [/code] tags around it?

Edit: oh, i see the issue. You're making declarations in main() instead of just calling the methods. Since you have a pointer, use "->", and don't forget to delete it afterwards (or use a smart pointer). Alternately, you could just create an instance of the object and call its method with
1
2
Dice d;
d.SomeMethodInDice();


Example with roll counter ouput:
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
#include <iostream>
//#include <ctime>

using namespace std;

class Dice
{
private:
    int dice1Roll, dice2Roll, total1, total2 = 0;

public:
    void 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 " << endl << endl;
    }

    void 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 " << endl << endl;
    }
};

int main()
{
    // srand((unsigned int) time(NULL));
    Dice* d = new Dice();
    int total_rolls = 6;
    int roll = 1;
    cout << "Ready to roll some dice?" << endl << endl;
    do 
    {
        cout << "Roll #" << roll << endl << endl;
        d->rollDicePlayer();
        d->rollDiceComputer();

    } while (roll++ < total_rolls);

    delete d;

    return 0;
}
Last edited on
closed account (LbX1AqkS)
Thanks for that, I tried using the pointers earlier but put Dice-> instead of rolling->, I feel pretty dumb.
ok, I edited my previous response with a fix ^.

Next you can look for repetitions and figure out if you can refactor to reduce them by moving that into its own method ;D

There are two major repetitions that can be reduced:
1. Player and Comp are rolling exactly the same, except for a message of who rolled
2. Each of the two dice roll exactly the same

(anticipating ~4x code length reduction!)

gl.
Last edited on
and 3)
you can probably condense the ascii art die faces if you wanted to. Might be good practice.
consider that..

If I am correct (check me) you only need 4 total print statements that can be reused.
space space space
dot space space
dot space dot
space dot space

(6 total if you count the top and bottom bars).


a nice little function...
void printface(int top, int middle, int bottom) //index into a table of canned strings.
{
cout << " ----- " << endl; //optionally look these up too?
cout << stringtable[top]<< endl;
cout << stringtable[middle]<< endl;
cout << stringtable[bottom]<< endl;
cout << " ----- " << endl;
}
Last edited on

yep, quite right approach, jonnin. 7 in all.
- header
- space space space (top/bot of "1", mid of "2", "4")
- dot space space (top of "3")
- dot space dot (top/bot of "4", "5", "6", mid of "6")
- space dot space (top/bot of "2", mid of "1", "3", "5")
- space space dot (bot of "3")
- footer

As extra challenge, you can use this to help print the two dice next to each other instead of above/below.
Last edited on
Showed a possible implementation with: one of the C++ random devices, const strings, dice next to each other, separator between rolls.
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
#include <iostream>
#include <string>
#include <random>
#include <array>

using namespace std;

class Dice
{
public:
    void RollTwoDice(bool human=true)
    {
        int one = rd_() % 6 + 1;
        int two = rd_() % 6 + 1;
        int i = one-1;
        int j = two-1;

        cout << LINES << SPACE << LINES << endl;
        cout << FACES[i][0] << SPACE << FACES[j][0] << endl;
        cout << FACES[i][1] << SPACE << FACES[j][1] << endl;
        cout << FACES[i][2] << SPACE << FACES[j][2] << endl;
        cout << LINES << SPACE << LINES << endl;
        
        cout << (human ? "You have " : "The computer has ") << 
                "rolled a " << one+two << endl << endl;       
    }
    
private:
    random_device rd_;
    const string LINES = " ------- "; // Top and bottom bars
    const string S_S_S = "|       |";
    const string D_S_S = "| 0     |";
    const string D_S_D = "| 0   0 |";
    const string S_D_S = "|   0   |";
    const string S_S_D = "|     0 |";
    const string SPACE = "    ";  // Whitespace between dice

    const array<array<string,3>, 6> FACES = 
    {{
        {S_S_S, S_D_S, S_S_S}, // 1
        {S_D_S, S_S_S, S_D_S}, // 2
        {D_S_S, S_D_S, S_S_D}, // 3
        {D_S_D, S_S_S, D_S_D}, // 4
        {D_S_D, S_D_S, D_S_D}, // 5
        {D_S_D, D_S_D, D_S_D}  // 6
    }};
};

int main()
{
    Dice d;
    int total_rolls = 6;
    int roll = 1;
    cout << "Ready to roll some dice?" << endl << endl;
    do 
    {
        cout << "-----------------------------------------\n";
        cout << "Roll #" << roll << endl << endl;
        d.RollTwoDice();
        d.RollTwoDice(false);

    } while (roll++ < total_rolls);

    return 0;
}


Running at https://repl.it/repls/GrandioseIcyDeletions
Last edited on
Topic archived. No new replies allowed.