Hello cplusplus community, firs post here :). I have run across an issues with one of my functions and was curious to know if there were any hints in regards to where the error lies.
The function is supposed to perform this task :
It should have a method called rollDice that simulates rolling the dice in the cup, returning the total value that was rolled. Remember that if there's more than one die, you need to "roll" each one separately because it's not a uniform distribution (i.e. rolling a single die with the numbers 2 through 12 will not give you the same distribution of numbers as rolling two six-sided dice).
In the client program we than have to:
It should then ask the user how many times they would like to roll those dice and print out the values for that many rolls.
I am not looking for an answer, since for the most part I believe I got everything working correctly, the only issue comes with calling the object in a loop, I keep getting the same value. I even set a random seeding and tried various ways, by including one in the loop, but to no avail I keep getting the same value when the object is called in a loop
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
|
// Class header file
#include <iostream>
#include <ctime>
#include <cstdlib>
#ifndef DICECUP_H
#define DICECUP_H
class DiceCup
{
private:
int diceNumber;
int faceNumber;
public:
DiceCup();
DiceCup(int, int);
void setDiceNumber(int);
void setFaceNumber(int);
int rollDice();
};
#endif
// Class cpp file/implementation file
#include "DiceCup.h"
#include <ctime>
#include <cstdlib>
// Default Construct
DiceCup::DiceCup()
{
diceNumber = 1;
faceNumber = 2;
}
// Construct Overload
DiceCup::DiceCup(int x, int y)
{
setDiceNumber(x);
setFaceNumber(y);
}
// Mutator Function
void DiceCup::setDiceNumber(int x)
{
diceNumber = x;
}
// Mutator Function
void DiceCup::setFaceNumber(int y)
{
faceNumber = y;
}
// Roll Dice Function
int DiceCup::rollDice() // I believe the issue might be here
{
srand(time(0));
int value = 0;
for (int x = 1; x <= diceNumber; x++)
value += rand() % faceNumber + 1;
return value;
}
// Client Program
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "DiceCup.h"
using namespace std;
int main()
{
int dice, faces, rollAmount;
cout << "How many dice are inside the cup: ";
cin >> dice;
cout << "How many faces do each of the dice have ";
cin >> faces;
cout << "How many times do you want to roll the dices ";
cin >> rollAmount;
DiceCup dice1, dice2(2, 6), dice3(dice, faces);
cout << endl << "Dice 1 is " << dice1.rollDice() << endl;
cout << "Dice 2 is " << dice2.rollDice() << endl;
for (int x = 1; x <= rollAmount; x++)
{
cout << "Roll " << x << " is " << dice3.rollDice() << endl;
}
return 0;
}
|
My output in Dice 1 and Dice 2 provide desired results, but as mentioned before Dice 3 provides the same value for each iteration of the loop. Any hints would be greatly appreciated. thanks
How many dice are inside the cup: 3
How many faces do each of the dice have 10
How many times do you want to roll the dices 5
Dice 1 is 2
Dice 2 is 9
Roll 1 is 26
Roll 2 is 26
Roll 3 is 26
Roll 4 is 26
Roll 5 is 26
Press any key to continue . . .
|