Reputation: 1
Okay, im just about done with my 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;
I was also told by a class mate 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.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
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;
}
Upvotes: 0
Views: 3101
Reputation: 2075
First of all: you roll two dice, store the results in dice1 and dice2, but then you send those values to two functions that put the value into a variable called faceValue. It is logical that getting the value back will return only the second dice value, because that's what you last entered in faceValue.
That is why the same values are shown for both dice.
Now for the error: your totalScore function returns a void while the << operator expects some kind of type. The totalScore function adds the two dice (correct values by the way) and puts the result in score, but nowhere, the value in score is returned.
Your code is really messy. You shouldn't have one member variable (faceValue) holding a copy of two different values. You shouldn't have this member at all. Just use the diceOne and diceTwo values. When the values are set ( = rand() % 6 + 1 ), they should not be set again by calling the set-function: Either create a correct set-function (because this one isn't correct) and put the random as a parameter in there, or set the member variable diceOne and diceTwo directly in the constructor as you already do. Don't do both. When returning the sum of the two dice, why not just return this sum (hint: the function totalScore should return something of the int-type). Why are you putting the summed result into a member variable? There is no need for that.
I could post the corrected code here, but it seems that you really have to learn this yourself.
Edit: And by the way: as stated above, learn to use the debugger. You will soon discover that the things I am telling you are correct. You will notice that faceValue first gets the value of diceOne and then the value of diceTwo, never getting the diceOne value back.
Upvotes: 5