MultiDice Game (each Die rolls random)

Okay, im just about done with my OOP exercise and stuck on how to get each dice to roll their own individual randomly generated number. The program does in fact roll random numbers, it's just every time you re-roll both dice always roll the same exact numbers. And this simple but yet head scratching problem occurred, for some reason i'm also having
 
cout << "Adding both dices up you rolled a total of: " << totalScore() << "." << endl;

giving me an error when the other functions above it are okay.

I was also told by a classmate that my faceValue was an illegal value and should be set to a legal value. I didn't quite get what he meant and I'm sure it'll knock off (not a lot) a few of my grade.

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <time.h>
using namespace std;



class PairOfDice
{


private:

	int diceOne;
	int diceTwo;
	int score;
	int faceVaule;

public:

	PairOfDice(){
		srand(time(NULL));
		roll();

	}
	
	void roll(){
		diceOne = (rand() % 6) + 1;
		diceTwo = (rand() % 6) + 1;
		setdiceOne(diceOne);
		setdiceTwo(diceTwo);
	}
	
	
	void setdiceOne(int value){
			
		faceVaule = value;
			
	}

	int getdiceOne(){
			
		return faceVaule;
		
	}
	
	void setdiceTwo(int value){
			
		faceVaule = value;
				
	}

	int getdiceTwo(){
		return faceVaule;
		
	}

	void totalScore(){

		score = diceOne + diceTwo;
	}

	void display(){

		cout << "The first Dice rolled a " << getdiceOne() << " ." << endl;

		cout << "The second Dice rolled a " << getdiceTwo() << " ." << endl;
		// adding both dices gives an: No operator " < < " matches these operands
		cout << "Adding both dices up you rolled a total of: " << totalScore() << "." << endl;
	}

	
};

int _tmain(int argc, _TCHAR* argv[])
{

	PairOfDice game;
	
	game.roll();
	game.display();
	game.totalScore();
	 

	return 0;
}
Last edited on
cout << "Adding both dices up you rolled a total of: " << totalScore() << "." << endl;
Total score returns void, so this line won't work (Hint, make it return score, an int)

The getDicen() function should return the value of the corresponding dicen data member (where n is the number of the die). You have both returning the same value (thus, outputting them would give the same values).

The setDicen() function should set the value of the corresponding dicen data member (where n is the number of the die). You have both functions changing the same value (namely faceValue).

The roll method assigns values to diceOne and diceTwo directly, so there is no need to call the corresponding set function.

srand(time(0)) should be in the main function. If you were to create multiple objects, the seed would be reset for each new object you create which isn't necessary.

There were a few issues that I had to correct, and I think that was all of them. Try fixing those and see if that works.
Alright, I kind of get what you're saying and while I was trying to figure it out I tidied up my code a little bit. Trying to do two things at once I took out total score for now, but by the location of the srand(time(0)) // srand(time(NULL)); down to the main function. I tried but I'm still getting the same results, I can't see but to make another logic up for the set values.
And sorry for my incompetence it was a bad idea to do this with no sleep
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <time.h>
using namespace std;



class PairOfDice
{


private:

	int dice;
	int faceVaule;

public:

	PairOfDice(){
		srand(time(NULL));
		roll();

	}
	
	void roll(){
		dice = (rand() % 6) + 1;
		setdiceOne();
		setdiceTwo();
	}
	
	
	void setdiceOne(){
			
		faceVaule = dice;
			
	}

	int getdiceOne(){
			
		return faceVaule;
		
	}
	
	void setdiceTwo(){
			
		faceVaule = dice;
				
	}

	int getdiceTwo(){
		return faceVaule;
		
	}



	void display(){

		cout << "The first Dice rolled a " << getdiceOne() << "." << endl;

		cout << "The second Dice rolled a " << getdiceTwo() << "." << endl;
		
		cout << "Adding both dices up you rolled a total of: " << getdiceOne() + getdiceTwo() << endl;
	}

	
};

int _tmain(int argc, _TCHAR* argv[])
{

	PairOfDice game;

	game.roll();
	game.display();
	
	
	 

	return 0;
}
Last edited on
1
2
3
4
5
	void roll(){
		dice = (rand() % 6) + 1;  // generate 1 random number.
		setdiceOne();    // facevalue is set to dice.
		setdiceTwo();    // facevalue is set to dice.
	}


So, why are the two dice the same when they use the same variable for their value and you only generate one number? Because there aren't two dice, and you only generate one random number.
O man I am so stupid. Thank you for hitting me in the nuts my good sir.
I got it working.
Last edited on
Topic archived. No new replies allowed.