### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` ``````#include "stdafx.h" #include #include #include #include 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
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````#include "stdafx.h" #include #include #include #include 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
 ``12345`` `````` 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.