Help with program.

Rules of the game are basically to see whose die had the higher roll.
The code works fine with the exception of when the die roll happens the program SOMETIMES does not output that player 1 or 2 won, just says if you would like to run again.
It also does not output when both the dies are the same as a tied game.
The program also takes any other character letter as a way to replay the game instead of just y/Y.
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
 int main()
{
	
	
	char playGame;

	do
	{
		cout << "Player one die roll is: " << diceRoll(0) << endl;
		cout << "Player two die roll is: " << diceRoll2(0) << endl;

		if (diceRoll(0) < diceRoll2(0))
		{
			cout << "Player two is the winner!" << endl;
		}
		else if (diceRoll2(0) < diceRoll(0))
		{
			cout << "Player one is the winner!" << endl;
		}

		else if(diceRoll == diceRoll2)

		{
			cout << "Tie game!"<< endl; 
		}
		
		
		cout << "Enter Y/y to play again." << endl;
		cin >> playGame;

	} while (playGame=='y'||'Y');
	
	



}

//Function for rolling a die
int diceRoll(int)
{
	srand(time(0));
	int die1;
	die1 = rand() % 6 + 1;
	return die1;


}
int diceRoll2(int)
{

	int die2;	
	die2 = rand() % 6 + 1;
	return die2;

}
How are you inputting the char playgame? there is no input?
@chambi - Did you miss line 29?

@OP
Lines 9,10,12.16 - Why are you passing an argument to diceroll/diceroll2 that isn't used?

Line 31: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Lines 9,10: You don't save the result of your calls to diceroll/diceroll2. At line 12, you're going to get different results.

Line 42: Do not call srand() multiple times. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Line 40,49: Why do you have two diceroll functions are do the same thing?



@AbstractionAnon Yeah I did, didn't see that.
@AbstractionAnon

Lines 9,10,12.16 - Why are you passing an argument to diceroll/diceroll2 that isn't used?


I'm not sure what argument to pass without creating another variable.

Line 31: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.

I'm not sure what you mean by this? Should I use the && operator instead?

Lines 9,10: You don't save the result of your calls to diceroll/diceroll2. At line 12, you're going to get different results.

How do I save the results?

I put the srand(time) at the top of the main.
Also, how can I only use one function if I need to return two values?
--Sorry if these are dumb questions, just kinda lost.

Last edited on
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
#include <iostream>
#include <ctime>
using namespace std;

int diceRoll();  //  Function prototype required

int main()
{	int roll1, roll2;
	char playGame;

	srand(time(0));
	do
	{
		roll1 = diceRoll();		//	Roll first die
		roll2 = diceRoll();		//	Roll second die
		cout << "Player one die roll is: " << roll1 << endl;
		cout << "Player two die roll is: " << roll2 << endl;
		if (roll1 < roll2)
		{	cout << "Player two is the winner!" << endl;
		}
		else if (roll1 > roll2)
		{	cout << "Player one is the winner!" << endl;
		}
		else 
		{	//	Must be a tie.  No need to check for ==
			cout << "Tie game!" << endl;
		}
		cout << "Enter Y/y to play again." << endl;
		cin >> playGame;

	} while (playGame == 'y' || playGame =='Y');  // Fully express both contitionals
}

//Function for rolling a die
int diceRoll ()
{	int die;
	die = rand() % 6 + 1;
	return die;
}

Ended up doing it slightly different, but thank you for the help!
Topic archived. No new replies allowed.