I think i'm pretty close, a few issues i see running it is 12 doesn't always end game with loss on first roll and probability does not calculate for some reason. Any direction would be appreciated. Leaving for phish fest tonight and need to get this figured out!
#include <iostream>
#include <ctime> // for time()
#include <cstdlib>
usingnamespace std;
int main ()
{
int dieOne=0;
int dieTwo=0;
int roll=0;
int point=0;
int win=0;
int loss=0;
double odds=0;
srand(time(0)); //makes time random seed
for(int game=1; game<4; game++)
{
dieOne=(rand()%6)+1;//creates dice
dieTwo=(rand()%6)+1;
roll=dieOne+dieTwo;
cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl;
if(roll==7 || roll==11)//win on first roll
win++;
elseif(roll==2 || roll==3 || roll==12)//lose on first roll
loss++;
elseif(roll==4 || roll==5 || roll==6 || roll==8 || roll==9 || roll==10)
{
point=roll;//point set
do
{
dieOne=(rand()%6)+1;//rolls dice again
dieTwo=(rand()%6)+1;
roll=dieOne+dieTwo;
cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl;
if(roll==point)//wins if player rolls point
win++;
elseif(roll==7)//lose if player rolls 7
loss++;
elseif(roll!=7 && roll!=point) //nothing otherwise
odds=0;
}while(roll!=point && roll!=7);//keep going until point or 7 is rolled
cout<<"game over- game #: "<<game<<endl;
}
}
odds=(win/(win+loss)); //calculate probability
cout<<win<<" wins and "<<loss<<" loses"<<endl;
cout<<"Probability to win is: "<<odds<<endl;
}
Calculating odds; you have an integer divided by another integer. That will give you an integer output. You don't want that. You must turn them into floating point values before you do the calculation.
anything on the logic error part of game? i've been trying to rework it with a combo of switch statements, but this seems much closer. also thinking of breaking it down into a few functions with switch statements that would do checks instead of if-else.
edit: i tried to set point to 0 at the end of loop once point had been checked and it really messes up program. i wouldn't think it would need to be reinitialized to 0 anywhere because point changes before it gets checked again anyway i think...
I'll preface this response in saying that I am a new student to C++ and very much a beginner in programming as a whole, but I like to review threads like this to try and problem solve for my own benefit as well.
When I ran the program myself, I noted that rolling an automatic win or automatic loss worked in all respects except notifying you that the game had ended. The next game just picked up without a visual cue that the prior game ended.
I tested this theory by altering the code so that for the first roll, both dies were equal to 6 rather than a random number. The output was:
6 plus 6 = 12
6 plus 6 = 12
6 plus 6 = 12
0 wins and 3 loses
Probability to win is: 0
Note that the same would happen if you rolled a winning roll. Everything would work correctly except for the output indicating that the game ended. This issue is caused because that line of output is nested within the "else if" statement for when you roll a point on the first roll.