Could someone help me understand whats wrong

Not sure if it's (rand() % 6 + 1) or my if and else statements. Could someone help me with this.
Also is there any way to attach an image of the output im getting?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
 #include <iostream>
#include<string>
#include<iomanip>
#include<conio.h>

using namespace std;

int Player1Dice1;// declare player 1 first dice
int Player1Dice2;//declare player 1 second dice
int Player1Sum;//declare player 1 sum of both dice
int Player1Wins;// declare Player 1 number of wins
int Player2Dice1;//declare player 2 first dice
int Player2Dice2;//declare player 2 second dice
int Player2Sum;//declare player 2 sum of both dice
int Player2Wins;//declare Player 2 number of wins
int Round = 1;


int main(){
	
	cout << "Welcome to a C++ port of Akeems Dice game simulator" << endl << endl;


	while (Round >= 0 && Round <= 10) //while loop to keep game running for 10 rounds
	{
		cout << "**************** ROUND" << Round << " ****************" << endl;
		cout << "Player 1 press any key to roll dice" << endl << endl;
		system("PAUSE");//Waits for user button input to continue

		
		Player1Dice1 = (rand() % 6 + 1);// first random dice roll
		Player1Dice2 = (rand() % 6 + 1);// second random dice roll


		Player1Sum = Player1Dice1 + Player1Dice2; // sum of player ones dice rolls


		cout << "Player 1 rolled " << Player1Dice1 << " & a " << Player1Dice2 << endl;


		if (Player1Dice1 == Player1Dice2)
		{
			cout << "PLAYER 1 ROLLED DOUBLE. PLAYER 1 WINS!!!" << endl;
			Player1Wins = Player1Wins + 1;
		}
			else if (Player1Dice1 == 1 && Player1Dice2 == 1)// SNAKE EYES IF STATEMENT IF DICE BOTH 1 
			{
				cout << "SNAKE EYES!!! PLAYER 1 HAS LOST ! " << endl;
				Player2Wins = Player2Wins + 1;
			}


			//pLAYER 2 TURN
			else {
				cout << "Player 2 press any key to roll dice" << endl << endl;
				system("PAUSE");//Waits for user button input to continue

				
				Player2Dice1 = (rand() % 6 + 1);// first random dice roll
				Player2Dice2 = (rand() % 6 + 1);// second random dice roll
				Player2Sum = Player2Dice1 + Player2Dice2; // sum of player 2 dice rolls


				cout << "Player 2 rolled " << Player2Dice1 << " & a " << Player2Dice2 << endl;
			}

			if (Player2Dice1 == Player2Dice2)
			{
				cout << "PLAYER 2 ROLLED DOUBLE. PLAYER 2 WINS!!!" << endl;
				Player2Wins = Player2Wins + 1;
			}
				else if (Player2Dice1 == 1 && Player2Dice2 == 1)//SNAKE EYES IF STATEMENT IF DICE BOTH 1 
				{
					cout << "SNAKE EYES!!! PLAYER 1 HAS LOST ! " << endl;
					Player1Wins = Player1Wins + 1;

				}
				Round = Round + 1;

			}// end of while loop


								 //Display results and winner
			cout << "**********************************************" << endl;
			cout << "*                 RESULTS                    *" << endl;
			cout << "**********************************************" << endl;
			cout << "PLAYER 1 WON " << Player1Wins << "OUT OF 10 ROUNDS" << endl;
			cout << "PLAYER 2 WON " << Player2Wins << "OUT OF 10 ROUNDS" << endl;
			if (Player1Wins < Player2Wins)//if player 1 has less wins then player 2 player one wins
			{
				cout << "  . . ." << endl;
				cout << " . o o ." << endl;
				cout << " . \_/ ." << endl;
				cout << "  . . ." << endl;
				cout << "PLAYER 2 WINS!!!!" << endl;
			}
			else if (Player1Wins > Player2Wins)//if player 1 has more wins then player 1 wins
			{
				cout << "  . . ." << endl;
				cout << " . o o ." << endl;
				cout << " . \_/ ." << endl;
				cout << "  . . ." << endl;
				cout << "PLAYER 1 WINS!!!!" << endl;
			}



			cout << "The game has ended" << endl;
			cout << "Press any key to continue..." << endl;
			system("PAUSE");
		}
Last edited on
You can copy the output from the console and put it in [output][/output] tags.

I don't see any call to srand(), which you will need to do in order to seed the random number generator used by rand().
http://www.cplusplus.com/reference/cstdlib/srand/
Line 74: Your cout is incorrect. It should be player 2 has lost.

Lines 93,101: You have an invalid escape sequence. If you want to use a \ in a quoted string, you need to escape the \ as \\.

Lines 67-77: You probably want these lines within the else for player 2 turn (54-65). You're executing 67-77 even if player 2 did not roll.

You're still not handling a tie.

As Zhuge mentioned, you're not calling srand() to initialize the random number generator. Without it, you're going to get the same sequence of rolls every time. srand() should be called only once at the beginning of main.

Lines 31-35 and 59-61: These lines are prefect candidates for a function.
1
2
3
4
5
6
int RollDice ()
{  int die1, die2;
    die1 = (rand() % 6) + 1;
    die2 = (rand() % 6) + 1;
    return die1 + die2;
}


Also, your smiley at lines 91-94 and 99-102 is another candidate for a function.

Other than that, nothing else is jumping out at me. please be more specific about what it is doing or not doing.
Last edited on
This site is great. Thanks guys!. I'll have to wait till tomorrow but ill make the corrections and see where i get. Thanks again. Also i will be more descriptive next time.

So I realized I had to right most of the code over to add Tie handling. Also making the task harder without functions. (Dont ask). I took all suggestions and redid the code but I am still having problems.
The program should simulate and display Player 1 and two Dice rolls. Store them and them go through all the possible outcomes.
Tie , Snake eyes , P1 win, or P2 wins. It will do this for 10 rounds then tally it all and display the winner and stats on screen.

A couple of problems I noticed is I am not getting RANDOM numbers and my else if are all messed up I think. The sums aren't adding up and snake eyes from a 6 & 6 ....

Here's the code
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include<string>
#include<iomanip>
#include<conio.h>

using namespace std;

int Player1Dice1;// declare player 1 first dice
int Player1Dice2;//declare player 1 second dice
int Player1Sum;//declare player 1 sum of both dice
int Player1Wins;// declare Player 1 number of wins
int Player2Dice1;//declare player 2 first dice
int Player2Dice2;//declare player 2 second dice
int Player2Sum;//declare player 2 sum of both dice
int Player2Wins;//declare Player 2 number of wins
int Round = 1;


int main()

{
	srand();
	cout << "Welcome to a C++ port of Akeems Dice game simulator" << endl << endl;


	while (Round >= 0 && Round <= 10) //while loop to keep game running for 10 rounds
	{
		cout << "**************** ROUND " << Round << " ****************" << endl;

		//Player ones turn to roll
		cout << "Player 1 press any key to roll dice" << endl << endl;
		system("PAUSE");//Waits for user button input to continue
		cout << "Player 1 rolled " << Player1Dice1 << " & a " << Player1Dice2 << endl;

		Player1Dice1 = (rand() % 6 + 1);// first random dice roll
		Player1Dice2 = (rand() % 6 + 1);// second random dice roll
		Player1Sum = Player1Dice1 + Player1Dice2; // sum of player ones dice rolls

		//player twos turn to roll
		cout << "Player 2 press any key to roll dice" << endl << endl;
		system("PAUSE");//Waits for user button input to continue 
		cout << "Player 2 rolled " << Player2Dice1 << " & a " << Player2Dice2 << endl;


		Player2Dice1 = (rand() % 6 + 1);// first random dice roll
		Player2Dice2 = (rand() % 6 + 1);// second random dice roll
		Player2Sum = Player2Dice1 + Player2Dice2; // sum of player 2 dice rolls

		//****************************** HANDLE TIE START **********************************************

		//If both player 1 and 2 roll snake eyes
		if ((Player1Dice1 == 1) && (Player1Dice2 == 1) && (Player2Dice2 == 1) && (Player2Dice2 == 1))
		{
			cout << "Both player 1 & 2 rolled SNAKE EYES!" << endl;
			cout << "Its a tie!  Restart Round " << Round << endl;
		}

		//If both player 1 and 2 roll doubles
		else if ((Player1Dice1 == Player1Dice2) && (Player2Dice1 == Player2Dice2))
		{
			cout << "Both players 1 & 2 rolled doubles" << endl;
			cout << "Its a tie! Restart Round " << Round << endl;
		}

		//If no doubles or snake eyes but sum of both player 1 and 2 dice = the same
		else if (Player1Sum == Player2Sum)
		{
			cout << "Player 1 rolled the sum of  " << Player1Sum << "& " << "Player 2 rolled the sum of = " << Player2Sum << endl;
			cout << "Its a tie! Restart Round " << Round << endl;
		}

		//****************************** HANDLE TIE END *****************************************************


	   //**************************** HANDLE SNAKE EYES START *****************************************************


			//Handle snake eyes loss PLAYER 1
		else if ((Player1Dice1 == 1) && (Player1Dice2 == 1))
		{
			cout << "Player 1 Rolled SNAKE EYES!!!!" << endl;
			cout << "Player 2 WINS!!" << endl;
			cout << "PLayer 1 Loses" << endl;
			Player2Wins = Player2Wins = 1; //Player 2 wins
			Round = Round + 1; //Next round
		}

		//Handle snake eyes loss PLAYER 2
		else if ((Player2Dice1 == 1) && (Player2Dice2 == 1))
		{
			cout << "Player 2 Rolled SNAKE EYES!!!" << endl;
			cout << "Player 1 WINS!!" << endl;
			cout << "Player 2 Loses" << endl;
			Player1Wins = Player1Wins + 1; //Player 1 wins add 1
			Round = Round + 1; //Next round add 1
		}

		//**************************** HANDLE SNAKE EYES END *****************************************************


		//**************************** HANDLE DOUBLE START *****************************************************
			 //If player 1 rolls a Double
		else if (Player1Dice1 = Player1Dice2)
		{
			cout << "Player 1 Rolled a Double!" << endl;
			cout << "Player 1 WINS" << endl;
			Player1Wins = Player1Wins + 1; //Player 1 Wins add 1
			Round = Round + 1; //Next Round add 1
		}

		//If player 2 rolls double
		else if (Player2Dice1 = Player2Dice2)
		{
			cout << "Player 2 Rolled a Double!" << endl;
			cout << "Player 2 WINS" << endl;
			Player2Wins = Player2Wins + 1; //Player 2 wins add 1
			Round = Round + 1; // Next Round add 1
		}

		//**************************** HANDLE DOUBLE END *****************************************************


		//**************************** HANDLE SUM START *****************************************************

			//If player 1 sum is greater then player 2
		else if (Player1Sum > Player2Sum)
		{
			cout << "Player 1 rolled the sum of  " << Player1Sum << "& " << "Player 2 rolled the sum of = " << Player2Sum << endl;
			cout << "Player 1 WINS!" << endl;
			Player1Wins = Player1Wins + 1; //Player 1 Wins add 1
			Round = Round + 1; //Next Round add 1
		}

		//If player 1 sum is less then player 2
		else if (Player1Sum < Player2Sum)
		{
			cout << "Player 1 rolled the sum of  " << Player1Sum << "& " << "Player 2 rolled the sum of = " << Player2Sum << endl;
			cout << "Player 2 WINS!" << endl;
			Player2Wins = Player2Wins + 1; //Player 2 Wins add 1
			Round = Round + 1; //Next Round add 1
		}

		//**************************** HANDLE SUM END *****************************************************

	} //End of while loop . insert bracket }
		


								 //Display results and winner
			cout << "**********************************************" << endl;
			cout << "*                 RESULTS                    *" << endl;
			cout << "**********************************************" << endl;
			cout << "PLAYER 1 WON " << Player1Wins << "OUT OF 10 ROUNDS" << endl;
			cout << "PLAYER 2 WON " << Player2Wins << "OUT OF 10 ROUNDS" << endl;
			if (Player1Wins < Player2Wins)//if player 1 has less wins then player 2 player one wins
			{
				cout << "  . . ." << endl;
				cout << " . o o ." << endl;
				cout << " . \\_/ ." << endl;
				cout << "  . . ." << endl;
				cout << "PLAYER 2 WINS!!!!" << endl;
			}
			else if (Player1Wins > Player2Wins)//if player 1 has more wins then player 1 wins
			{
				cout << "  . . ." << endl;
				cout << " . o o ." << endl;
				cout << " . \\_/ ." << endl;
				cout << "  . . ." << endl;
				cout << "PLAYER 1 WINS!!!!" << endl;
			}



			cout << "The game has ended" << endl;
			cout << "Press any key to continue..." << endl;
			system("PAUSE");
		}


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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
And here's The output .

**************** ROUND 1 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 0 & a 0
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 0 & a 0
Both players 1 & 2 rolled doubles
Its a tie! Restart Round 1
**************** ROUND 1 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 6 & a 6
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 5 & a 5
Player 2 Rolled SNAKE EYES!!!      //?????????? HUH
Player 1 WINS!!
Player 2 Loses
**************** ROUND 2 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 6 & a 5
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 1 & a 1
Player 1 Rolled a Double!       //???? HUH
Player 1 WINS
**************** ROUND 3 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 3 & a 3
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 6 & a 6
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 4 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 4 & a 4
Player 2 press any key to roll dice    //4AND 4 IS 8


Press any key to continue . . .
Player 2 rolled 2 & a 6
Player 1 rolled the sum of  5& Player 2 rolled the sum of = 5
Its a tie! Restart Round 4
**************** ROUND 4 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 2 & a 3
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 4 & a 1
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 5 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 1 & a 1
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 3 & a 4
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 6 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 5 & a 5
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 4 & a 3
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 7 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 6 & a 6
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 6 & a 1
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 8 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 1 & a 1
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 4 & a 5
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 9 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 2 & a 2
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 2 & a 1
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 10 ****************
Player 1 press any key to roll dice

Press any key to continue . . .
Player 1 rolled 4 & a 4
Player 2 press any key to roll dice

Press any key to continue . . .
Player 2 rolled 3 & a 4
Player 1 Rolled a Double!
Player 1 WINS
**********************************************
*                 RESULTS                    *
**********************************************
PLAYER 1 WON 10OUT OF 10 ROUNDS       
PLAYER 2 WON 0OUT OF 10 ROUNDS
  . . .
 . o o .
 . \_/ .
  . . .
PLAYER 1 WINS!!!!
The game has ended
Press any key to continue...
Press any key to continue . . . 
Line 103,112: You're using the assignment operator (=), not the comparison operator (==)

Still no use of functions for repeated code.

Still not handling a tie (number of wins equal).

Lines 8-16: The variables don't need to be global by the looks of it (although if they do please ignore this).

Line 22: You need to seed your random, or it will produce the same values each time.

Line 26: Since you're setting round to one yourself, and it only increases, you don't need to check the lower bound.

Lines 33 & 42: Here are your big problems. In these lines, you are printing out the roles for player 1 and player two respectively. However, it is not until 35 & 45 respectively that you roll the dice, so the values you're outputting for the current round are actually from the previous round, but the message you display will be for the current round.

Line 66: You're checking whether the got the same sum before checking whether either of them got doubles. This may be intended, but if it's not, you will be saying it is a tie in the case of Player 1 (2, 4) Player 2 (3, 3). That else if should probably be moved to line 123 to remedy the situation.

Other than that, a good thing to know to shorten your code a bit is that, instead of writing
round = round + 1 (or anything of the format x = x + 1), you could write round++ (or ++round) which has the same functionality.
Thanks guys. Its working perfectly now. :)
Consider a function:

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
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;

int cast (int);              // Proto type your function

int main()
{
srand(time(0));  // Make random seed with off computer time

// Any time you need a die cast, you can do this...
cout << "Die cast is " << cast(6) << endl;
cout << "Die cast is " << cast(20) << endl;
cout << "Die cast is " << cast(12) << endl;
cout << "Die cast is " << cast(100) << endl;
cout << "Die cast is " << cast(75) << "\n\n\n" <<endl;

// If you need it returned to a variable, you can do this...
int rolled = cast(6);
cout << "You rolled a " << rolled << endl;

return 0;
}

int cast(int num){                                    // Your integer function
int number = rand()% num +1;                // You only need this once
return number;                                       // send your number back
}
Topic archived. No new replies allowed.