Help with craps programs

Ok so my assignment is to simulate a game of craps and I was just starting it and trying to simulate lose and win statements, if they get 7 or 11 its a win, if its 2,3, or 12 its a loss but i am having trouble getting unique random numbers and getting the code to display the output statements when the sum equals their respective numbers. Instead it ALWAYS displays the win output

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()


{

srand(time(0));

int d1, d2, sum;

d1 = rand() % 6 + 1; 
d2 = rand() % 6 + 1;
sum = d1 + d2;

	cout << "Dice Roll 1: " << d1 << endl
		 << "Dice Roll 2: " << d2 << endl; 

	if (sum = 7)
	{cout << "Congrats, you ween!";}
	else if (sum = 11)
	{cout << "Congrats, you ween!";}
	else if (sum = 2)
	{cout << "Congrats, you lose!";}
	else if (sum = 3)
	{cout << "Congrats, you lose!";}
	else if (sum = 12)
	{cout << "Congrats, you lose!";}
	else 
	{cout << "Error..";}
	
return 0;
}
Last edited on
Looks like the assignment = statement was used instead of the equality comparison ==.

 
if (sum == 7)   // line 24 

The reason it keeps reporting that "you ween" is because line 24 sets sum to equal 7, then evaluates to true as the operation was successful. The initial if statement was true, so the remaining if/else statements are skipped.

Update the = to == for the if/else block and you should be good to go.
Last edited on
Topic archived. No new replies allowed.