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.
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
usingnamespace 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;
}
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.