I finished this last night and it almost works perfectly aside from one minor thing that I can't quite figure out.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int playerscore, computerscore;
	int turnscore;
	int dice;
	char input = 0;
	bool turnover, gameover;
	
	srand((unsigned)time(0));
	playerscore = 0;
	computerscore = 0;
	cout << "Welcome to the game of Pig! You get to roll first\n\n";

	while(!gameover){  //while compiling, it says this bool value "gameover" is unused
			turnscore = 0;
			turnover = false;
			do //Human Turn
			{
			
				dice = (rand()%6)+1;
				  if(dice==1){
					  cout << "You rolled a 1! ";
					  cout << "you lose your turn! Your total is " << playerscore << " \n";
					  turnscore = 0;
					  turnover = true;
				  }
				  else{
					  turnscore += dice;
					  cout << "\n";
					  cout << "You rolled a " << dice << " ";
					  cout << "Your turn score is " << turnscore << " ";
					  cout << "and your total score is " << playerscore << endl;
					  cout << "If you hold, you will have " << playerscore + turnscore << " points.\n";
					  cout << "Enter 'r' to roll again, or 'h' to hold: ";
					  cin >> input;
					  if(input == 'r'){
						  turnover = false;
					  }
					  else if(input == 'h'){
						  break;
					  }
					  else {
						  return 0;
					  }
				  }
			}
				  while(!turnover);
			playerscore += turnscore;
			cout << "Your score is " << playerscore << "\n\n";
			if(playerscore >= 100){
				cout << "YOU WIN!!";
					break;
					gameover = true; //right here it should finish, but it goes all the way down to the end from here.
			}

			turnscore = 0;
			do  //Computer turn
			{
				
				dice = (rand()%6)+1;
				if(dice==1){
					cout << "Computer rolled a 1! ";
					cout << "Computer loses its turn! Computer total is " << computerscore << " \n";
					turnscore = 0;
					turnover = true;
				}
				else{
					turnscore += dice;
					cout << "\n";
					cout << "Computer rolled a " << dice << " ";
					cout << "Computer's turn score is " << turnscore << " ";
					cout << "and its total score is " << computerscore << endl;
					if(turnscore >= 20){
						turnover = true;
					}
					else {
						turnover = false;
					}
				}
			}
				while(!turnover);
			computerscore += turnscore;
			cout << "Computer score is " << computerscore << endl;
			if(computerscore >= 100){
				cout << "COMPUTER WINS!!";
					gameover = true;
			}
	}

	return 0;
}

My problem is that after the Human wins and the message, "YOU WIN!!" is displayed, instead of terminating right there, it goes through the computer turn one more time before finishing.
I'm thinking perhaps I used my bool value "gameover" improperly, but I don't know why it would be wrong, any ideas?

Recommended Answers

All 4 Replies

Don't continue after line 58 if you're done. Perhaps add another if() statement to see if you ought do the do...while loop.

Don't continue after line 58 if you're done. Perhaps add another if() statement to see if you ought do the do...while loop.

I added a if(gameover==false) around the computers turn, which fixed the overall result, but I'm still getting a
"c47000 error: uninitialized local variable 'gameover' used"
on line 19. Ive tried changing it to an if (which made the program mess up) statement to see if it made any difference but to no avail. how can I get rid of this error?

To fix the uninitialized variable, initialize it.

Frankly, I would also redo all of your user input (using strings and conversions where necessary). And I would also put various chunks into functions.

This is really my first program to write that actually does something, I haven't dealt with anything more advanced like making functions yet, but thanks for the help Dave!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.