Using loops and rand function

Hello
I am trying to create a program that will ask the user for how many rolls of a dice he would like to roll and then ask at what value he wants to hold at. (like the game of pig)
I started to write the code below but I have an endless loop and it does not stop when the total value reaches the set value input by the user.

Also I am not sure how to keep track of turns as well so the it would stop if the turns input from user was reached.

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

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

int dieRoll();
int humanTurn(int);
int hold, roll;

int main()
{
	int humanTotalScore = 0, computerTotalScore = 0;
	
	srand(time(0)); //set a different seed for rand() every time to get different game outputs
	cout<<"how many rolls?"<<endl;
	cin>>roll;
	cout<<"where to hold"<<endl;
	cin>>hold;
	//loop to keep playing until someone scores 100+ or else
	do
	{
		humanTotalScore = humanTotalScore + humanTurn(humanTotalScore); //add the score from a new turn to the running total
	
		if(humanTotalScore >= 100)
		{
			cout << "You have reached 100!";
			break;
		}
	
	}
	while(humanTotalScore < 100);
	return 0;
}

//simulate rolling of die
int dieRoll()
{
	return (rand() % 6) + 1; //call to rand() returns 0-5, + 1 to give range 1-6, best way to avoid impossible die roll of 0
}

int humanTurn(int humanTotalScore)
{
	int thisTurnScore = 0, score = 0;
	
	
	//loop to keep going as long the player value has not been met or a 1 was rolled
	do
	{
		score = dieRoll(); //roll the die
		
		if(score == 1)
		{
			cout << "You rolled a 1.  End of turn." << endl;
			return 0;
		}
		
		thisTurnScore = thisTurnScore + score; 
				
		cout << thisTurnScore << endl;
                
		
	}
	while(hold<thisTurnScore);
	
	
	
Last edited on
Did you post the complete code? It doesn't look like it, since your humanTurn function is missing a bracket at the end. Anyways, you need to actually set the return value of that function. I'm not entirely sure how the game works, but if I'm understanding it correctly you want return thisTurnScore after the while statement in your function.
your right this was working now I get the error : error: expected unqualified-id before _{_ token

I cannot find where my brackets are incorrect



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

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

int dieRoll();
int humanTurn(int);
int hold, roll;

int main()
{
	int humanTotalScore = 0, computerTotalScore = 0;
	
	srand(time(0)); //set a different seed for rand() every time to get different game outputs
	cout<<"how many rolls?"<<endl;
	cin>>roll;
	cout<<"where to hold"<<endl;
	cin>>hold;
	//loop to keep playing until someone scores 100+ or else
	do
	{
		humanTotalScore = humanTotalScore + humanTurn(humanTotalScore); //add the score from a new turn to the running total
	
		if(humanTotalScore >= 100)
		{
			cout << "You have reached 100!";
			break;
		}
	
	}
	while(humanTotalScore < 100);
	return 0;
}

//simulate rolling of die
int dieRoll()
{
	return (rand() % 6) + 1; //call to rand() returns 0-5, + 1 to give range 1-6, best way to avoid impossible die roll of 0
}

int humanTurn(int humanTotalScore);
{
	int thisTurnScore = 0, score = 0;
	
	
	//loop to keep going as long the player value has not been met or a 1 was rolled
	do
	{
		score = dieRoll(); //roll the die
		
		if(score == 1)
		{
			cout << "You rolled a 1.  End of turn." << endl;
			return 0;
		}
		
		thisTurnScore = thisTurnScore + score; 
				
		cout << thisTurnScore << endl;
                
		
	}
	while(hold<thisTurnScore)
	return thisTurnScore;
}
	
	
Get rid of the semi-colon on line 44.
perfect!
I actually saw it right before you posted, thank you!
Basically I need this program to only roll the dice as many times as the user sets and/or stop when the total score reaches the hold value the user sets.
I just do not know how to get there....

Any suggestions?

am I even close?
I think the program stops automatically when a score of >100 is achieved. (It should, at least). To make the other condition an additional stopping point, you could just add to the while loop:

while(humanTotalScore < holdValue || rollNumber < maxRolls ) //ask the user to input holdValue and maxRolls... the code will stop when either event occurs
how I keep track of the rollnumber
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
#include <iostream>
#include <cstdlib>
#include <ctime>

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

int dieRoll();
int humanTurn(int);
int hold, roll;

int main()
{
	int humanTotalScore = 0, computerTotalScore = 0;
	
	srand(time(0)); //set a different seed for rand() every time to get different game outputs
	cout<<"how many rolls?"<<endl;
	cin>>roll;
	cout<<"where to hold"<<endl;
	cin>>hold;
	//loop to keep playing until someone scores 100+ or else
	do
	{
		humanTotalScore = humanTotalScore + humanTurn(humanTotalScore); //add the score from a new turn to the running total
	
	
		if(humanTotalScore >= 100)
		{
			cout << "You have reached 100!";
			break;
		}
	
	}
	while(humanTotalScore < 100);
	while((humanTotalScore < hold) || (humanTurn(humanTotalScore) < roll ))
	return 0;
}

//simulate rolling of die
int dieRoll()
{
	return (rand() % 6); //call to rand() returns 0-5, 
}

int humanTurn(int humanTotalScore)
{
	int thisTurnScore = 0, score = 0;
	
	
	//loop to keep going as long the player value has not been met or a 1 was rolled
	do
	{
		score = dieRoll(); //roll the die
		
		if(score == 1)
		{
			cout << "You rolled a 1.  End of turn." << endl;
			return 0;
		}
		
		thisTurnScore = thisTurnScore + score; 
				
		cout << thisTurnScore << endl;
                
		
	}
	while(hold<thisTurnScore);
}
Topic archived. No new replies allowed.