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.
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;
}
elseif (diceRoll2(0) < diceRoll(0))
{
cout << "Player one is the winner!" << endl;
}
elseif(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;
}
@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?
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.
#include <iostream>
#include <ctime>
usingnamespace 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;
}
elseif (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;
}