Code will not out put properly!

After it goes goes through and plays it's 1 million games. It proceeds to tell me that I have won all 1 million games(as you can see below) which obviously is not true. Because the outputted code is telling me that I lose quite a bit.


Out of 1 million games you won: 1000000
Out of 1 million games you lose: 0
The probability for the Player to win is: 100%
the probability for House to win is: 0%

Here is my code what do you think is going on?!?!
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
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

int dice1;
int dice2;
int sum_of_dice;
int point;
int number_of_wins;
int number_of_lose;
double probability_of_winning;
double probability_of_losing;
int number_of_times_played;

int main()
{
    
    srand(time(0));
    
    do
    {
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
        
        sum_of_dice = dice1 + dice2;
        
        if (sum_of_dice == 7 || sum_of_dice == 11)
        {
            cout << "Congratulations you have won!" << endl;
        }
        else if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
        {
           cout << "Better luck next time the house has one!" << endl;
        }
        else (sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10);
        {
            do {
                dice1 = rand() % 6 + 1;
                dice2 = rand() % 6 + 1;
                int sum2 = dice1 + dice2;
                    if( sum2 == sum_of_dice )
                    {
                        cout << "Congratulations you have won!" << endl;
                        break;
                    }
                    else if( sum2 == 7 )
                    {
                        cout << "Better luck next time the house has one!" << endl;
                        break;
                    }
                } while(true);
        }
        
    } while (++number_of_times_played != 1000000);
    
    number_of_wins = number_of_times_played - number_of_lose;
    number_of_lose = number_of_times_played - number_of_wins;      // It is now telling me that I have never lost.
    probability_of_winning = number_of_wins / 10000;
    probability_of_losing = number_of_lose / 10000;

    cout << endl << endl << "-----------------------------------------------" << setprecision(7) << endl;
    cout << "Out of 1 million games you won: " << number_of_wins << endl;
    cout << "Out of 1 million games you lose: " << number_of_lose << endl;
    cout << "The probability for the Player to win is: " << probability_of_winning << setw(2) << "%" << endl;
    cout << "the probability for House to win is: " << probability_of_losing << setw(2) << "%" << endl;
    
        return 0;
}//End Code! 
you never update your number_of_lose variable so it always equal 0;

also what do you think this line of code does?
else (sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10);
ok... how would I go about updating that variable I do not see were I went wrong.

and I see what your saying about line 38...... are you saying I do not need it? so I have some cleaning up to do is that what you're saying?
ok... how would I go about updating that variable I do not see were I went wrong.


you need to change the value of the variable anytime the player loses.

so
1
2
3
4
5
        else if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
        {
           cout << "Better luck next time the house has one!" << endl;
           ++number_of_lose;
        }


1
2
3
4
5
6
                   else if( sum2 == 7 )
                    {
                        cout << "Better luck next time the house has one!" << endl;
                        ++number_of_lose;
                        break;
                    }



and I see what your saying about line 38...... are you saying I do not need it? so I have some cleaning up to do is that what you're saying?


what i'm saying is this portion of the code
(sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10);

does absolutely nothing.


you either want an else if statement
else if(sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10)

or just an else
OHHHHHHHH MY THANKS! sweet! That worked and I see what your saying wow I just learned a lot thanks!
How would I get this:

Out of 1 million games you won: 493549
Out of 1 million games you lose: 506451
The probability for the Player to win is: 49%
the probability for House to win is: 50%

to show decimal numbers.

I have tried set precision and setw but it's not working.

Any help would be greatly appreciated

1
2
3
4
cout << "Out of 1 million games you won: " << number_of_wins << endl;
    cout << "Out of 1 million games you lose: " << number_of_lose << endl;
    cout << "The probability for the Player to win is: " << probability_of_winning << setprecision(5) << "%" << endl;
    cout << "the probability for House to win is: " << probability_of_losing << setprecision(5) << "%" << endl;
Try this making this
1
2
 probability_of_winning = number_of_wins / 10000;
probability_of_losing = number_of_lose / 10000;

into
1
2
 probability_of_winning = number_of_wins / 10000.0;
probability_of_losing = number_of_lose / 10000.0;


In C++, when you divide an integer by an integer, it truncates the result to an integer.
Either the numerator or the denominator must be a non-integer type for the result to be a non-integer type, like double.
Last edited on
@wharp2

I added the setprecision() command into the program. Reduced it from 1 million games to
20,000 games. I had better things to do than watch it count up ;) Hope this helps
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision
#include <ctime>
#include <string>
#include <cstdlib>


using std::cout;  // Used these instead of 'using namespace std;'
using std::endl;
using std::setprecision;
using std::fixed;

int dice1;
int dice2;
int sum_of_dice;
int point;
double number_of_wins;  // Changed these 4 into doubles
double number_of_loses;
double probability_of_winning;
double probability_of_losing;
int number_of_times_played;

int main()
{

 srand((unsigned) time(0)); 

 do
 {
	dice1 = rand() % 6 + 1;
	dice2 = rand() % 6 + 1;

	sum_of_dice = dice1 + dice2;
	point = sum_of_dice;
	cout << "Dice total is " << sum_of_dice << endl;
	if (sum_of_dice == 7 || sum_of_dice == 11)
	{
	 cout << "Congratulations, you have won!" << endl;
	 number_of_wins++;
	}
	else if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
	{
	 cout << "Better luck next time. The house wins!" << endl;
	 number_of_loses++;
	}
	else// if (sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10)
	{
	 cout << "Your point is now: " << sum_of_dice << endl;
	 do
	 {
// used this for rolling after finding your point. Not sure if this is how it should be
		dice1 = rand() % 6 + 1;
		dice2 = rand() % 6 + 1;

		sum_of_dice = dice1 + dice2;
    cout << "This total is " << sum_of_dice << endl;
		if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
		{
		 cout << "The house won!" << endl;
		 number_of_loses++;
		}
		if(sum_of_dice == point)
		{
		 cout << "Yeah, you win!" << endl;
		 number_of_wins++;
		}
	 }while ((sum_of_dice != 2 && sum_of_dice != 3 && sum_of_dice != 12) || sum_of_dice == point);
	}
} while (++number_of_times_played != 20000);
number_of_wins = number_of_times_played - number_of_loses;
    number_of_loses = number_of_times_played - number_of_wins;

 probability_of_winning = (number_of_wins/number_of_times_played)*100;
 probability_of_losing = (number_of_loses/number_of_times_played)*100;

 cout << "Out of 20,000 games, you won : " << number_of_wins << endl;
 cout << "Out of 20,000 games, the house won : " << number_of_loses << endl;
 cout << "The probability for the Player to win is: "  << fixed << setprecision(2) << probability_of_winning << "%" << endl;
 cout << "The probability for House to win is: "  << fixed << setprecision(2) << probability_of_losing << "%" << endl;

 return 0;
}
Topic archived. No new replies allowed.