nested loops

I am writing this program that will simulate the roll of a dice.
The user inputs the number of rolls (turns) and the value he wants to stay at or above.
each roll or turn the dice continue to roll until he gets at or above his hold value, adding one value to the next.
this happens how ever many times the user inputs when asked how many rolls.

I think my code is adding all the inputs into each other.
for example:
I get:
score
2
5
7
9
when I should get:
2
3
2
2

someone please help


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
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int dieRoll();
int humanTurn(int);
int hold, roll, dice, num;
double total;
int main()

{
	srand(time(0));
	
	cout<<"Enter the amount to hold"<<endl;
	cin>>hold;
	cout<<""<<endl;
	cout<<"enter how many times to roll"<<endl;
	cin>>roll;
	cout<<""<<endl;
	
//	dice=(rand() % 6);
	
	cout<<"Score"<<"\t"<<"Estimated Probability"<<endl;
	
	for (total=0; total<roll; total++) // loop as many time as roll input equals
	{
		do 
		{
			num+=(rand()%6);
			
			if ((rand()%6)==1) //if the dice roll a 1 end turn start over and move to next turn
				{
					num=0;
					break;
				}
			if (num>=hold) //if the num equals or is greater than hold then start over the count move to next turn
				{
					break;
				}
		}
		
		while (num<hold); // if num is less than hold value continue loop
		

	cout<<num<<"\t\t"<<"probability?"<<endl;
	}
	return 0;
}


I am just trying to get this figured out but eventually I need the program to handle probability as well
for example:
let's suppose hold at number is 17

We role the dice until we roll a 17 or larger, but if we roll a 1, then our score is 0.

Let's suppose the number of simulations is 5 and for simulation 1 we roll

2 + 5 + 6 + 4 = 17

and for simulation 2 we roll

5 + 4 + 1, but since we rolled a 1, the score is 0,

for simulation 3 we roll

3 + 4 + 5 + 6 = 18

for simulation 4 we roll

4 + 4 + 6 + 6 = 20

for simulation 5 we roll

3 + 4 + 5 + 5 = 17 so that means we got

0: 1 time 1/5 = .20
17: 2 times 2/5 = .40
18: 1 time 1/5 = .20
19: 0 times 0/5 = .00
20: 1 time 1/5 = .20
21: 0 times 0/5 = .00
22: 0 times 0/5 = .00

In other words the idea is to roll a 17 or larger (or whatever the hold number happens to be), but if you roll a 1, then your score is a 0.
In your do-while loop, set another variable called int roll to handle each roll and and itself to num

This way, you can change if ((rand()%6)==1) to if (roll==1), because as it stands now, I believe it actually "rolls" again and then checks the logic. This is affecting your results.

Also, when you do your roll, it needs to be (rand()%6) + 1)
Last edited on
the results still do not seem correct?

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int dieRoll();
int humanTurn(int);
int hold, roll, dice, num;
double total;
int main()

{
	srand(time(0));
	
	cout<<"Enter the amount to hold"<<endl;
	cin>>hold;
	cout<<""<<endl;
	cout<<"enter how many times to roll"<<endl;
	cin>>roll;
	cout<<""<<endl;
	
//	dice=(rand() % 6);
	
	cout<<"Score"<<"\t"<<"Estimated Probability"<<endl;
	
	for (total=0; total<roll; total++) // loop as many time as roll input equals
	{
		do 
		{
			num+=(rand()%6 + 1);
			int roll;
			
			if (roll==1) //if the dice roll a 1 end turn start over and move to next turn
				{
					num=0;
					break;
				}
			if (num>=hold) //if the num equals or is greater than hold then start over the count move to next turn
				{
					break;
				}
		}
		
		while (num<hold); // if num is less than hold value continue loop
		

	cout<<num<<"\t\t"<<"probability?"<<endl;
	}
	return 0;
}
Topic archived. No new replies allowed.