The problem is that the program would not terminate if either the human or the computer reach 100 or above points. it is not displaying if the human wins that you won or if the computer wins then sorry you lost, try again.

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




const int scoreLimit = 100;

int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);

int main() 

{
		
        bool continuePlay = true;
	
        int humanTotalScore = 0, computerTotalScore = 0;
		srand(time(NULL));
        cout << "This is a game of Pig. It is your turn. Press r to roll." << endl << endl;
        
        if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit))
        {
                continuePlay = true;
        }
        else
        {
                continuePlay = false;
        }
        do
        {
                cout << "Computers Score: " << computerTotalScore << endl;
                
                humanTurn(humanTotalScore);
                computerTurn(computerTotalScore);
                
        }
        while(continuePlay = true);

		while(continuePlay = false)
		{
        
        if(humanTotalScore >= scoreLimit)
        {
                cout << "Good job! You won.";
				break;
        }
        else if (computerTotalScore >= scoreLimit)   
        {
		              
			cout << "Sorry, you lost. Try again!";
			break;
        }
		}
        
        
        
        return 0;
}


int humanTurn(int& humanTotalScore)
{
        int currentScore = 0;
        int lastRoll;
        char rollOrHold;
        cout << "Your total score is: " << humanTotalScore << "." << endl;
        cout << "Press r to roll again, or h to hold." << endl;
        cin >> rollOrHold;
        while (rollOrHold == 'r') 
        {
                lastRoll = diceRoll();
                if (lastRoll == 1)
                {
                        cout << "You rolled a 1, ending your turn." << endl;
                        break;
                }
                else
                {
                        currentScore += lastRoll;
                        cout << "The die shows a " << lastRoll << ". Your score this turn is: " << currentScore << endl;
                        cout << "Press r to roll again, or h to hold." << endl;
                        cin >> rollOrHold;
                        
                }
        }
        while (rollOrHold == 'h')
        {
                humanTotalScore += currentScore;
                break;
                
        }
        
        return humanTotalScore;
}

int computerTurn(int& computerTotalScore)
{
        int currentScore = 0;
        int lastRoll;
        cout << "Computers total score is: " << computerTotalScore << "." << endl;
        while ((currentScore <= 20) && (currentScore != 1))
        {
                lastRoll = diceRoll();
                if (lastRoll == 1)
                {
                        cout << "The computer rolled a 1, ending their turn." << endl;
                        break;
                }
                else
                {
                        currentScore += lastRoll;
                        cout << "The computers die shows a " << lastRoll << ". Their score this turn is: " << currentScore << endl;
                }
        }
        if(currentScore >= 20)
        {
                computerTotalScore += currentScore;
                cout << "After the computers turn, they have gained an additional " << lastRoll << "points." << endl;
                
        }
        
        return computerTotalScore;
}


int diceRoll()
{
        int x;
        
        int const j = 1, k =6;
        x = (j+(rand()%(k-j+1)));
        
        return x;
}

Dani AI

Generated

Two immediate causes explain the hang and the compile errors: the game-loop condition is using assignment instead of a proper boolean check, and the winner test is done outside the active play loop so it never stops the loop when a score crosses the limit. pointed out the misplaced winner check and correctly flagged the assignment-vs-comparison issue; both observations are accurate and should be combined into a single, simple loop that checks for a win after each turn.

A practical, minimal main-loop layout (illustrative only) is:

while (humanTotalScore < scoreLimit && computerTotalScore < scoreLimit) {
    cout << "Computer's score: " << computerTotalScore << endl;
    humanTurn(humanTotalScore);         // update by reference or return value
    if (humanTotalScore >= scoreLimit) break;
    computerTurn(computerTotalScore);
}
if (humanTotalScore >= scoreLimit)
    cout << "Good job! You won.\n";
else
    cout << "Sorry, you lost. Try again!\n";

Notes and quick fixes that address remaining bugs seen in the thread:

  • A break statement outside any loop or switch causes a compile error; ensure break is only used inside loops. That likely caused the errors reported after the attempted fixes.
  • Make function semantics consistent: either have humanTurn/computerTurn modify totals by reference and return void, or have them return the points gained and assign that in main. Mixing both is confusing.
  • In the computer turn, base the stop condition on the last die roll (e.g., if (lastRoll == 1)) or on currentScore >= holdThreshold — checking currentScore != 1 is wrong because currentScore is a sum, not a single die value. Also print currentScore when reporting gained points (not lastRoll).
  • For easier testing, temporarily lower scoreLimit (for example to 20) and add short debug prints after every update to confirm scores change and the loop terminates as expected.

These changes combine the corrections already suggested by , and into a clear control-flow and should resolve both the infinite loop and the compile-time break errors.

Recommended Answers

All 14 Replies

your check if either player has more than the points needed to win is not inside your do while loop. it is only checked before you enter the loop and then you go on forever.

I think lines 40 and 42 are incorrect. You are using an assignment operator (=) instead of a comparison operator (==), unless you're trying to get it to do something I'm not seeing.

Also, I think lines 24 to 31 should be inside the do...while(continuePlay == true) loop. That would make the program check to see if there's a winner each time both players have taken a turn. You might want to modify it to check the scores after each player takes a turn, too.

Edit: I think the while(continuePlay == false) loop is not necessary. The game will stay in the previous loop till the game is over right? After the program ends that loop, the only thing you'll need is the if/else statements :)

thanks for your suggestions but it all seems so confusing can you guys please illustrate what you mean in the code

this is what i did to correct it but it is still giving me errors.

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




const int scoreLimit = 100;

int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);

int main() 

{
		
        bool continuePlay = true;
	
        int humanTotalScore = 0, computerTotalScore = 0;
		srand(time(NULL));
        cout << "This is a game of Pig. It is your turn. Press r to roll." << endl << endl;
        
		

        if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit))
        {
                continuePlay = true;
        }
        else
        {
                continuePlay = false;
        }
		do
		{
      
                cout << "Computers Score: " << computerTotalScore << endl;
                
                humanTurn(humanTotalScore);
                computerTurn(computerTotalScore);
		}
			 
        
        if(humanTotalScore >= scoreLimit)
        {
                cout << "Good job! You won.";
				break;
        }
        else if (computerTotalScore >= scoreLimit)   
        {
		              
			cout << "Sorry, you lost. Try again!";
			break;
        } 
       
         while(continuePlay = true);
        
        
        return 0;
}


int humanTurn(int& humanTotalScore)
{
        int currentScore = 0;
        int lastRoll;
        char rollOrHold;
        cout << "Your total score is: " << humanTotalScore << "." << endl;
        cout << "Press r to roll again, or h to hold." << endl;
        cin >> rollOrHold;
        while (rollOrHold == 'r') 
        {
                lastRoll = diceRoll();
                if (lastRoll == 1)
                {
                        cout << "You rolled a 1, ending your turn." << endl;
                        break;
                }
                else
                {
                        currentScore += lastRoll;
                        cout << "The die shows a " << lastRoll << ". Your score this turn is: " << currentScore << endl;
                        cout << "Press r to roll again, or h to hold." << endl;
                        cin >> rollOrHold;
                        
                }
        }
        while (rollOrHold == 'h')
        {
                humanTotalScore += currentScore;
                break;
                
        }
        
        return humanTotalScore;
}

int computerTurn(int& computerTotalScore)
{
        int currentScore = 0;
        int lastRoll;
        cout << "Computers total score is: " << computerTotalScore << "." << endl;
        while ((currentScore <= 20) && (currentScore != 1))
        {
                lastRoll = diceRoll();
                if (lastRoll == 1)
                {
                        cout << "The computer rolled a 1, ending their turn." << endl;
                        break;
                }
                else
                {
                        currentScore += lastRoll;
                        cout << "The computers die shows a " << lastRoll << ". Their score this turn is: " << currentScore << endl;
                }
        }
        if(currentScore >= 20)
        {
                computerTotalScore += currentScore;
                cout << "After the computers turn, they have gained an additional " << lastRoll << "points." << endl;
                
        }
        
        return computerTotalScore;
}


int diceRoll()
{
        int x;
        
        int const j = 1, k =6;
        x = (j+(rand()%(k-j+1)));
        
        return x;
}

this is what i did to correct it but it is still giving me errors.

//SNIP'd the beginning stuff
        
		
// MOVE THIS IF STATEMENT AND ELSE STATEMENT INSIDE OF YOUR while(continuePlay = true); LOOP.
        if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit))
        {
                continuePlay = true;
        }
        else
        {
                continuePlay = false;
        }
		do
		{
      
                cout << "Computers Score: " << computerTotalScore << endl;
                
                humanTurn(humanTotalScore);
                computerTurn(computerTotalScore);
		} // WHAT IS THIS DOING? IT IS NOT HELPING. IT NEEDS TO BE MOVED, NOT DELETED.
			 
        
        if(humanTotalScore >= scoreLimit)
        {
                cout << "Good job! You won.";
				break;
        }
        else if (computerTotalScore >= scoreLimit)   
        {
		              
			cout << "Sorry, you lost. Try again!";
			break;
        } 
       
         while(continuePlay = true);
        
        
        return 0;
}


//SNIP'd the turn functions.


int diceRoll()
{
        int x;
        
        int const j = 1, k =6;
        x = (j+(rand()%(k-j+1)));

// THIS IS OVERLY COMPLEX.
// It is possible simplify it.
// A few redundant additions and subtractions.
        
        return x;
}

This should help some more. I'd feel bad if I just did it for you.

Check the while loops. You should be using == not =.

u mean something like this. btw heal brains it would make me feel very happy if you did this for me anyway it is late two weeks and the max i can get on this assignment is 8/10.

do
		{
		 

      
                cout << "Computers Score: " << computerTotalScore << endl;
                
                humanTurn(humanTotalScore);
                computerTurn(computerTotalScore);

			if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit))
        {
                continuePlay = true;
        }
        else
        {
                continuePlay = false;
        }
		}
		while(continuePlay = true); 
			 
        do
		{
        if(humanTotalScore >= scoreLimit)
        {
                cout << "Good job! You won.";
				
        }
        else if (computerTotalScore >= scoreLimit)   
        {
		              
			cout << "Sorry, you lost. Try again!";
		
        }
		 }
       while(continuePlay = false);
       
        
	    
        return 0;

btw heal brains it would make me feel very happy if you did this for me anyway it is late two weeks and the max i can get on this assignment is 8/10.

That's not the way the forum works. You do it, we help. Heal Brains would be breaking the rules if he gave you the whole program.

Read posts 3 and 7 again. You still have = and == mixed up.

That's not the way the forum works. You do it, we help. Heal Brains would be breaking the rules if he gave you the whole program.

Read posts 3 and 7 again. You still have = and == mixed up.

ok calm down forum police. no one is giving out the answer

commented: Bad attitude. -2
commented: We know. But you asked for it to be done for you. Don;t get uppity just because you are in the wrong. -2

ok calm down forum police. no one is giving out the answer

Forum police, huh? It's pretty clear you're not willing to put effort into this. A lot of people would have phrased it it much more bluntly than I did. Hopefully no one will help you.

commented: let him have it +1

Forum police, huh? It's pretty clear you're not willing to put effort into this. A lot of people would have phrased it it much more bluntly than I did. Hopefully no one will help you.

not willing to put the effort? did you right the program for me?

not willing to put the effort? did you right the program for me?

No, I didn't write the program for you. Like I said, that's not how the forum works. I pointed that out to you. You could take my pointing that out to you in either of two ways. You could either post something that showed that you understood and respected that or you could post some smart-ass comment that proved that you are like a lot of people who post here and just expect people to hand you the answer on a silver platter. Clearly you are in the latter category. I imagine that the "forum police" would agree with me.

I imagine that the "forum police" would agree with me.

We do. This thread almost got closed.

Clean up your attitude, faaz. You get more help by being thankful than being belligerent .

We do. This thread almost got closed.

Clean up your attitude, faaz. You get more help by being thankful than being belligerent .

okay people really you guys need to calm down because i don't believe i have done anything wrong. I never said that i am looking for the answer. that one reply was a joke. obviously if you looked at the other posts you would know

forget it.

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.