dice game prototype function problem

All rollDice is supposed to do is roll the dice and return nothing. But then in main I have to cout dice1 and dice2's values and add then into TotalDice so that if TotalDice equals to 7 then the user wins.

I used x and y thinking that they would take the value of dice1 and dice2 but that's not working since x and y are not initialized.

help?

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
#include <iostream>		//for cout
#include <cstdlib>		//for rand()
#include <ctime>		//for time()

using namespace std;

void rollDice(int dice1, int dice2);

int main()
{

int TotalDice;
int x, y;

rollDice(x,y);

TotalDice = (x+y);

if (TotalDice == 7)

cout << "You Win";

else

cout << "You lose";

}

void rollDice(int dice1, int dice2)
{
	
	dice1 = rand() % 6 + 1; //random number from 1 - 6
	dice2 = rand() % 6 + 1; //random number from 1 - 6

}
Take a look at Arguments passed by value and by reference:
http://www.cplusplus.com/doc/tutorial/functions/

Basically you are assigning the value of x to dice1 and the value of y to dice2 then reassigning the values dice1 and dice2 to random numbers. At no point do x or y get assigned any value other than the garbage they originally got when declared. There are a couple different ways to fix it, but you are missing a few other things before you need to worry about that.
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
#include <iostream>		//for cout
#include <cstdlib>		//for rand()
#include <ctime>		//for time()

using namespace std;

int rollDice();

int main()
{

int TotalDice;


TotalDice = rollDice();

if (TotalDice == 7)

cout << "You Win";

else

cout << "You lose";

}

int rollDice()
{
	int dice1,dice2;
	dice1 = rand() % 6 + 1; //random number from 1 - 6
	dice2 = rand() % 6 + 1; //random number from 1 - 6
        return dice1+dice2;
}
Last edited on
thank you Himanshu123 but my professor doesn't want rollDice to return anything, unless I've missread her "The method will take in two int parameters one for each die, but it will not return anything to where it was called."

I haven't had any luck figuring out a way out of this
You can pass the x and y by reference (see the link admkrk posted for you). If you pass by reference, the function can update the original variable values and not have to return anything.
Have you covered pass by reference yet?

You had the right idea in your first post, except that dice1 and dice2 were passed by value, not by reference.

1
2
3
4
void rollDice(int & dice1, int & dice2)  // Note the & for pass by reference
{   dice1 = rand() % 6 + 1; //  caller's variable will be updated
    dice2 = rand() % 6 + 1; //  caller's variable will be updated
}


Be sure to change your function protoype accordingly.
Last edited on
thank you so much guys. I got it working. That link was also very helpful in clearing some things up about functions.
Topic archived. No new replies allowed.