Simple Craps program

So I was working in this small program to run over a million times this game of Craps. However, when my rolls in my arrays wins[rolls] and losses[rolls] are even, the results are way off the intended result. Can someone see what can I do to fix this issue. Here are the instructions but I'm trying to stay within these subdirectories BTW.

Write an application that runs 1,000,000 games of craps and answers the following questions:
1) How many games are won on the first roll, second roll, …, twentieth roll, and after the
twentieth roll?
2) How many games are lost on the first roll, second roll, …, twentieth roll, and after the
twentieth roll?
3) What are the chances of winning at craps? [Note: You should discover that craps is one of
the fairest casino games. What do you suppose this means?]
4) What is the average length of a game of craps?
Rules of Craps (Seven-Eleven)
◦ A player rolls two dice. Each die has six faces.
◦ Faces contain 1, 2, 3, 4, 5 and 6 spots.
◦ After the dice have come to rest, the sum of the spots on the two upward faces is
calculated.
◦ If the sum is 7 or 11 on the first roll, the player wins.
◦ If the sum is 2, 3 or 12 on the first roll (called “craps”), the player loses (i.e., the
“house” wins).
◦ If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, then that sum becomes the player’s
“point.”
◦ To win, continue rolling until you “make your point.”
◦ You lose by rolling a 7 before making the point.
Below is a complete listing of Craps Simulation in C++ and sample execution results

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

unsigned int rollDice();

int main(){

enum class Status {CONTINUE, WON, LOST};

srand(static_cast<unsigned int>(time(0)));

unsigned int sumOfDice{rollDice()};
unsigned int myPoint{0};
Status gameStatus;

unsigned int wins[22];
unsigned int losses[22];


int winSums = 0;
int totalGames = 0;
int roll;

for(int i = 1; i<=1000000; ++i)
{
sumOfDice = rollDice();
roll = 1;

switch(sumOfDice)
{
case 7:
case 11:
gameStatus = Status::WON;
break;
case 2:
case 3:
case 12:
gameStatus = Status::LOST;
break;
default:
gameStatus = Status::CONTINUE;
myPoint = sumOfDice;
break;
}

while(gameStatus == Status::CONTINUE)
{
sumOfDice = rollDice();
++roll;
if(sumOfDice == myPoint){
gameStatus = Status::WON;
}
else {
if(sumOfDice == 7){
gameStatus = Status::LOST;
}
}
}

if(roll>21){
roll = 21;
}

if(gameStatus == Status::WON)
{
++wins[roll];
++winSums;
++totalGames;
}
else{
if(gameStatus == Status::LOST)
++losses[roll];
++totalGames;
}
}

int length = 0;

for(int i = 1; i <= 21; ++i)
{
if(i == 21){
cout<<wins[i]<<" games won and "
<<losses[i]<<" games lost after the 20th roll"<<endl;
}
else{
cout<<wins[i]<<" games won and "
<<losses[i]<<" games lost on roll #"<<i<<endl;
}
length = length + (wins[i]*i + losses[i]*i);
}

cout<<"The chances of winning are "<<100.0*(winSums/totalGames)<<endl;

cout<<"The average game length is "<<((double)length/totalGames)<<" rolls.";

}


unsigned int rollDice(){
int die1 = (1 + rand()%6);
int die2 = (1 + rand()%6);
int sum = (die1 + die2);

return sum;
}
Please edit your post to put [code][/code] tags around your code (the <> format icon).
Line 18-19: wins and losses are uninitialized (garbage).

1
2
    unsigned int wins[22] = {};
    unsigned int losses[22] = {};


Line 94: You're doing integer division always resulting in 0.
 
    cout << "The chances of winning are " << 100.0*((double)winSums / totalGames) << endl;


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.