c++ problem

I think my looping is wrong can someone over look it for ,e its a pig dice game

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<ctime>

using namespace std;

// Declare Variables
int pigDice = 0;
int gamerDice = 0;
int gamerTurn = 0;
int computerTurn = 0;
int gamerInput = 0;
int totalScore;
const int SCORE_LIMIT = 100;
char choice = ' ';
int gamerScore = 0;
int turnScore = 0;
int pigScore = 0;
int main()
{
cout << "Welcome to ARCADEMANIA" << endl;
cout << "Enter 1 to play Pig " << endl;
cout << "Enter 2 to play Blackjack dice" << endl;
cin >> gamerInput;




srand(static_cast<int>(time(0)));
pigDice = (rand() % 6) + 1;
gamerDice = (rand() % 6) + 1;
gamerScore = gamerScore + gamerDice;

if
(gamerInput == 1)
{

char gamerName[12];
cout << "WELCOME TO PIG GAME" << endl;
cout << "Hi welcome to Pig you will be trying to beat Wilbert to 100 pts. Rember you cannot roll a 1 or you will lose a turn and will not get any points. Wilbert is excited to play goodluck and roll on" << endl;
cout << "Please enter your gamer name" << endl;
cin >> gamerName;
cout << " Welcome to Pig " << gamerName << " Lets see that you got!!!!!" << endl;

cout << "Press r to roll.\n" << endl;
cin >> choice;


do
{

cout << "Your roll is a " << gamerDice << endl;
cout << "Your total score is " << gamerScore << endl;
cin >> choice;
gamerDice = (rand() % 6) + 1;
gamerScore = gamerScore + gamerDice;



} while (choice == 'r' && gamerScore <= 100);


while (gamerDice <= 1)
{

do
{
cout << gamerScore << endl;
pigDice = (rand() % 6) + 1;
pigScore = pigScore + pigDice;
cout << "Your roll is a " << pigDice << endl;
cout << "Your total score is " << pigScore << endl;
} while (pigScore < 20 || gamerDice == 1);


if (pigDice == 1) cout << "You lose your turn Pig!!! " << gamerName << " press 'r' to roll die. \n" << endl;


while (gamerScore >= 100)
{
cout << " Congragulation" << gamerName << "Your the winner" << endl;
}



}







system("pause");
return 0;

}
}
Program works fine for me? Do you get any errors? What is it doing wrong? You need to give more information
When gamerDice rolls a 1 it should go to the pigDice but when pigDice roll a 1 it should go back to gamerDice until 1 reaches 100 but it stops after pigDice roll 1 and close program I think its a looping error
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
		do
		{

			cout << "Your roll is a " << gamerDice << endl;
			cout << "Your total score is " << gamerScore << endl;
			cin >> choice;
			gamerDice = (rand() % 6) + 1;
			gamerScore = gamerScore + gamerDice;




		} while (choice == 'r' && gamerScore <= 100);


You're not telling this loop to stop running when the user rolls 1. So even if the user rolls one it will continue to run until the gamescore is over 100.
so do I put a if statement in between the do while statement
You can just change the while parenthesis to while (gamerDice !=1)

That will exit the loop as soon as the player rolls a 1.
this is what I did


do
{

cout << "Your roll is a " << gamerDice << endl;
cout << "Your total score is " << gamerScore << endl;
cin >> choice;
gamerDice = (rand() % 6) + 1;
gamerScore = gamerScore + gamerDice;



} while (choice == 'r' && gamerScore <= 100);


while (gamerDice != 1)
I told you to change it, as in replace them -

1
2
3
4
5
6
7
8
9
10
11
12
do
{

cout << "Your roll is a " << gamerDice << endl;
cout << "Your total score is " << gamerScore << endl;
cin >> choice;
gamerDice = (rand() % 6) + 1;
gamerScore = gamerScore + gamerDice;



} while (gamerDice != 1);
ok so when the pig roll a 1
do this look right

} while (gamerDice != 1);
{

do
{
cout << gamerScore << endl;
pigDice = (rand() % 6) + 1;
pigScore = pigScore + pigDice;
cout << "Your roll is a " << pigDice << endl;
cout << "Your total score is " << pigScore << endl;
} while (pigDice != 1);


if (pigDice == 1) cout << "You lose your turn Pig!!! " << gamerName << " press 'r' to roll die. \n" << endl;


while (gamerScore >= 100)
{
cout << " Congragulation" << gamerName << "Your the winner" << endl;
}



}
No idea what your code above is. Can you post all of it? And please put it between code tags, they are under "format" and look like this <>. http://www.cplusplus.com/articles/jEywvCM9/

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<ctime>

using namespace std;

// Declare Variables
int pigDice = 0;
int gamerDice = 0;
int gamerTurn = 0;
int computerTurn = 0;
int gamerInput = 0;
int totalScore;
const int SCORE_LIMIT = 100;
char choice = ' ';
int gamerScore = 0;
int turnScore = 0;
int pigScore = 0;
int main()
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
{
	cout << "Welcome to ARCADEMANIA" << endl;
	cout << "Enter 1 to play Pig " << endl;
	cout << "Enter 2 to play Blackjack dice" << endl;
	cin >> gamerInput;




	srand(static_cast<int>(time(0)));
	pigDice = (rand() % 6) + 1;
	gamerDice = (rand() % 6) + 1;
	gamerScore = gamerScore + gamerDice;

	if
		(gamerInput == 1)
	{

		char gamerName[12];
		cout << "WELCOME TO PIG GAME" << endl;
		cout << "Hi welcome to Pig you will be trying to beat Wilbert to 100 pts. Rember you cannot roll a 1 or you will lose a turn and will not get any points. Wilbert is excited to play goodluck and roll on" << endl;
		cout << "Please enter your gamer name" << endl;
		cin >> gamerName;
		cout << " Welcome to Pig " << gamerName << " Lets see that you got!!!!!" << endl;

		cout << "Press r to roll.\n" << endl;
		cin >> choice;


		do
		{

			cout << "Your roll is a " << gamerDice << endl;
			cout << "Your total score is " << gamerScore << endl;
			cin >> choice;
			gamerDice = (rand() % 6) + 1;
			gamerScore = gamerScore + gamerDice;



		} while (gamerDice != 1);
		{

			do
			{
				cout << gamerScore << endl;
				pigDice = (rand() % 6) + 1;
				pigScore = pigScore + pigDice;
				cout << "Your roll is a " << pigDice << endl;
				cout << "Your total score is " << pigScore << endl;
			} while (pigDice != 1);


			if (pigDice == 1)	cout << "You lose your turn Pig!!! " << gamerName << " press 'r' to roll die. \n" << endl;


			while (gamerScore >= 100)
			{
				cout << " Congragulation" << gamerName << "Your the winner" << endl;
			}



		}






system("pause");
return 0;

}
}
Sorry you probably want the whole thing in code format


#include<iostream>
#include<ctime>
#include<cstdlib>
#include<ctime>

using namespace std;

// Declare Variables
int pigDice = 0;
int gamerDice = 0;
int gamerTurn = 0;
int computerTurn = 0;
int gamerInput = 0;
int totalScore;
const int SCORE_LIMIT = 100;
char choice = ' ';
int gamerScore = 0;
int turnScore = 0;
int pigScore = 0;
int main()
{
cout << "Welcome to ARCADEMANIA" << endl;
cout << "Enter 1 to play Pig " << endl;
cout << "Enter 2 to play Blackjack dice" << endl;
cin >> gamerInput;




srand(static_cast<int>(time(0)));
pigDice = (rand() % 6) + 1;
gamerDice = (rand() % 6) + 1;
gamerScore = gamerScore + gamerDice;

if
(gamerInput == 1)
{

char gamerName[12];
cout << "WELCOME TO PIG GAME" << endl;
cout << "Hi welcome to Pig you will be trying to beat Wilbert to 100 pts. Rember you cannot roll a 1 or you will lose a turn and will not get any points. Wilbert is excited to play goodluck and roll on" << endl;
cout << "Please enter your gamer name" << endl;
cin >> gamerName;
cout << " Welcome to Pig " << gamerName << " Lets see that you got!!!!!" << endl;

cout << "Press r to roll.\n" << endl;
cin >> choice;


do
{

cout << "Your roll is a " << gamerDice << endl;
cout << "Your total score is " << gamerScore << endl;
cin >> choice;
gamerDice = (rand() % 6) + 1;
gamerScore = gamerScore + gamerDice;



} while (gamerDice != 1);
{

do
{
cout << gamerScore << endl;
pigDice = (rand() % 6) + 1;
pigScore = pigScore + pigDice;
cout << "Your roll is a " << pigDice << endl;
cout << "Your total score is " << pigScore << endl;
} while (pigDice != 1);


if (pigDice == 1) cout << "You lose your turn Pig!!! " << gamerName << " press 'r' to roll die. \n" << endl;


while (gamerScore >= 100)
{
cout << " Congragulation" << gamerName << "Your the winner" << endl;
}



}







system("pause");
return 0;

}
}
Please declare the variables in the main. Dont use global ones, its bad practice and in this case not needed at all - Here is the complete program, it works perfectly now, make sure to study it so you understand every bit of it! - http://pastebin.com/WYpQ2VHg
Thank you very much I would use this as a reference
Topic archived. No new replies allowed.