Output 3 random sets of 5
I'm writting a Yahtzee program and so far it's going okay. Write now I'm trying to display 3 sets of random numbers. Each set is to have 5 numbers. Well, I have 3 sets displaying, but each set looks the same. Example:
13146
13146
13146
Should be different, like:
13146
53614
64135
Cane someone tell me where I'm faltering in my code? Here's what I have:
Code:
#include "Player.h"
int main()
{
Player aPlayer("John Sparks");
aPlayer.displayPlayer();
int i;
for(i=0; i<3; i++)
{
aPlayer.rollThem();
}
return 0;
}
#include <stdio.h>
#include <iostream.h>
#include<conio.h>
#include<string.h>
#include "Die.h"
const int MAX_NAME = 15;
class Player
{
private:
char playerName[MAX_NAME];
Die rollDie;
Die MAX_DIE[5];
public:
Player(char *name);
void setPlayer(char *name);
void displayPlayer();
char *getName();
void rollThem();
};
void Player::rollThem()
{
Die myDie;
int i;
for(i = 0; i < 5; i++)
{
myDie.rollDie();
myDie.displayValue();
}
cout<<endl;
}
char *Player::getName()
{
return playerName;
}
Player::Player(char *name)
{
strcpy(playerName, name);
}
void Player::displayPlayer()
{
cout<<playerName<<' '<<endl;
}
void Player::setPlayer(char *name)
{
cout<<"Enter name ";
cin>>playerName;
cout<<cout<<"Player name is "<<playerName<<'.'<<' '<<endl;
}
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
const int MAX_DIE = 5;
class Die
{
private:
int dieFaceVal;
bool dieRoll;
public:
Die();
int getFaceVal();
void setFaceVal(int newVal);
void rollDie();
void dieTrue();
void dieFalse();
void displayValue();
};
Die::Die()
{
srand((unsigned)time(NULL));
dieRoll = true;
}
int Die::getFaceVal()
{
return dieFaceVal;
}
void Die::rollDie()
{
if (dieRoll == true)
{
dieFaceVal = ((rand() % 6) + 1);
}
}
void Die::dieTrue()
{
dieRoll = true;
}
void Die::dieFalse()
{
dieRoll = false;
}
void Die::setFaceVal(int newVal)
{
dieFaceVal = newVal;
}
void Die::displayValue()
{
cout<<dieFaceVal<<' ';
}
srand((unsigned)time(NULL));
I can't believe it was as simple as that! Thank you both!
I had srand((unsigned)time(NULL));, but it was in the Die class. Moving it to main() did the trick.