0

The code I have for the rolling of the dice is in a function:

void Dice(int &dice1, int &dice2)
{
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1; 
}

but when it is in main

int main()
{
    srand(time(NULL));
    int dice1;
    int dice2;

    Dice(dice1, dice2);
    cout << "Player 1's Roll: " << dice1 << " and " << dice2 << endl;
    cout << "Player 2's Roll: " << dice1 << " and " << dice2 << endl;
}

and when I run the program, the output is that player 2's rolls are always the same as player 1's.

Example:

Player 1's Roll: 1 and 4
Player 2's Roll: 1 and 4

How do I fix this (without having to have another function) so that Player 2 may have difference dice rolls?

  • 5
    what do you expect? You rolled dices only once! (called `Dice` only once) – pergy May 16 '17 at 10:44
  • 4
    Please don't downvote on the grounds of obviousness as that is subjective. This question is clear, with compilable code. – Bathsheba May 16 '17 at 10:46

2 Answers2

3

Your problem was simple. You call Dice() once, not two times. If you want that second player roll dices too – you need to call Dice() again. If you don't do this – you'll get the same result. There's no reason to get different if there's no changes done between that two cout.

I've just added second call of Dice(), check this.

int main()
{
    srand(time(NULL));
    int dice1;
    int dice2;

    Dice(dice1, dice2); //you're rolling first time
    cout << "Player 1's Roll: " << dice1 << " and " << dice2 << endl; //printing result
    Dice(dice1, dice2); //Added by Sylogista: you're rolling second time
    cout << "Player 2's Roll: " << dice1 << " and " << dice2 << endl; //printing result
}
Sylogista
  • 565
  • 3
  • 10
1

One way would be to write something on the lines of

struct Player
{
    int roll1;
    int roll2;
    void roll()
    {
        Dice(roll1, roll2);
    }    
};

And at the call site:

int main()
{
    srand(time(NULL));
    Player one, two;
    one.roll();
    two.roll();
    cout << "Player 1's Roll: " << one.roll1 << " and " << one.roll2 << endl;
    cout << "Player 2's Roll: " << two.roll1 << " and " << two.roll2 << endl;
}

Then you can enhance the player class at your leisure, such as building a proper constructor, encapsulating the member variables, adding other members such as the name of the player &c.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Od dear... ok, idea with struct is good, but for me – it looks like OP's learning. He could don't know what are you talking about ;) OP: if I'm right – check this [tutorial about structures](http://www.cplusplus.com/doc/tutorial/structures/), it's really clear and would help you while understanding Bathsheba's solution. – Sylogista May 16 '17 at 10:58
  • Why not just drop the code from the `Dice` function into `Player::roll()`? –  May 16 '17 at 10:59
  • Eventually I hope that would happen. I wanted to preserve some of the original code. Eventually they will drop the call to `rand()` too. – Bathsheba May 16 '17 at 10:59
  • Thank you for helping as well! – Confused.Student May 16 '17 at 11:18
  • @Bathsheba when I try this, an error comes up on the "Dice(roll1, roll2);" line and says "'Dice' was not declared in this scope" – Confused.Student May 16 '17 at 11:26
  • Make sure the compiler "sees" at least a *forward declaration* of `Dice` before it tries to compile the `struct`. That will get you started. Alternatively, you could move the code inside `Dice` to `roll`, and ditch the `Dice` function entirely. – Bathsheba May 16 '17 at 11:31
  • @Bathsheba I'm sorry but I am not very skilled with programming. What do you mean by a forward declaration of Dice? – Confused.Student May 16 '17 at 12:45