Arrays with random numbers and looping

I'm having a problem with this code I wrote. Basically it is supposed to roll two dice 36 times and if you roll snake eyes, two ones, at least 3 of the 36 times you win 10 times your bet. It is really improbable to get snake eyes 3 times, but it seems to happen every time once it has looped a couple of times. I'm not sure what I'm doing wrong.

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
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
using namespace std;

int die(void);

int main ()
{
	int rolls[36] = {0}, wager = 0, die1 = 0, die2 = 0, total = 0, sum = 0;
	srand (unsigned(time(0)));
	cout << "Hello, this is a game. If you win you get 10 times your bet.\n";
	
	char ab = 'a';
do{
	cout << "\nMake a bet: $";
	cin >> wager;
	
	for(int i = 0; i < 36; i++)
	{
		die1 = die();
		die2 = die();
		total = die1 + die2;
		rolls[i] = total;
	}

	for (int i = 0; i < 36; i++)
	{
		if (rolls[i] == 2)
			sum++;			
	}

	cout << "\nYou have rolled 'Snake Eyes' " << sum << " times.\n";

	if (sum >= 3)
	{
		cout << "Congratulations you are a Winner!!!!! You have won 10 times your wager!\nYou have won: $" << wager*10 << endl;
	}
	else
	{
		cout << "\nYou loose.\n";
	}		

	char runagain;
	cout << "\nWould you like to bet again? (y or n)\n";
	cin >> runagain;

	if (runagain == 'n' || runagain == 'N')
	{
			return 0;
	}
	
}while(ab == 'a');

		return 0;
}

int die(void)
{
	return rand() % 6 + 1;
}
1
2
3
4
5
	for (int i = 0; i < 36; i++)
	{
		if (rolls[i] == 2)
			sum++;			
	}


You never reset the value of sum to 0 each time you iterate through the do-while loop, if you roll two snake eyes the first 36 times and roll one more the next 36 times, the player hasn't won the game, but because you failed to set sum = 0 at the beginning of the do-while loop, the program adds the number of snake eyes rolled in the first time with those in the second.
Last edited on
How exactly do you reset it back to zero, I put sum = 0, at the end of the do while loop and it seemed to fix it. Is there a better way to do this?
1
2
3
4
5
6
7
8
9
10
11
}while(ab == 'a');
sum = 0;
		return 0;
}

int die(void)
{
	return rand() % 6 + 1;
}
Edit & Run
Are you asking for a better way to write sum = 0 ?
Never mind, I misread your comment, thank you.
I meant where to put sum=0; but as Keene said in the first comment it should go in the beginning of the loop.
Topic archived. No new replies allowed.