### determining the winner?

Everything seems to be working in my dice pig console game except for determining the winner between the user and the computer. It is supposed to say you win when you reach 100 points. Anyone help?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133`` ``````#include "stdafx.h" #include #include #include #include #include using namespace std; //variables const int scoreLimit = 100; int diceRoll(); int humanTurn(int& humanTotalScore); int computerTurn(int& computerTotalScore); int continuePlay; int diceRoll() { int x; int const j = 1, k =6; x = (j+(rand()%(k-j+1))); return x; } int _tmain(int argc, _TCHAR* argv[]) { srand(time(0)); bool continuePlay = 1; int humanTotalScore = 0, computerTotalScore = 0; cout << setw(25)<< right << "GAME OF PIG" << endl; cout << "=============================================" << endl; //Human's turn and scoring int humanTurn(int& humanTotalScore) { int currentScore = 0; int lastRoll; char rollOrHold; cout << "Your total score is: " << humanTotalScore << "." << endl; cout << "Press r to roll 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 << "You Rolled a " << lastRoll << ". Your Current Score is: " << currentScore << " ?" << endl; cin >> rollOrHold; } } while (rollOrHold == 'h') { humanTotalScore += currentScore; break; } return humanTotalScore; } //Computers turn and scoring 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 << "Computer Rolled a 1, ending their turn." << endl; break; } else { currentScore += lastRoll; cout << "Computer Rolled a " << lastRoll << ". Their Current Score is: " << currentScore << endl; } } if(currentScore >= 20) { computerTotalScore += currentScore; cout << "After the computers turn, they have gained an additional " << currentScore << " points." << endl; } return computerTotalScore; } // Determines winner if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit)) { continuePlay = 1; } else { continuePlay = 0; } do { cout << "Computers Score: " << computerTotalScore << endl; humanTurn(humanTotalScore); computerTurn(computerTotalScore); } while(continuePlay == 1); if(continuePlay = 0, humanTotalScore > computerTotalScore) { cout << "Good job! You won."; } else { cout << "You lost!!"; } getchar(); getchar(); return 0; }``````
In your last do-while loop, when do you determine to stop?

You're checking `while(continuePlay == 1);`, when is that changed?
What do you mean?
You're stuck here:

 ``1234567`` ``````do { cout << "Computers Score: " << computerTotalScore << endl; humanTurn(humanTotalScore); computerTurn(computerTotalScore); }while(continuePlay == 1);``````

I mean, when you're in that loop, there's no way to change "continuePlay" to anything else since you're not checking inside the loop if someone went over the score.

You should put the if-else statement in there. :)
Yelnatz is right - if continuePlay is equal to 1 at the start of the loop, then the loop will run forever, because continuePlay will never change from being equal to 1.

Also, why are you defining your functions inside the body of your main function? That's not the usual way of doing things.
Topic archived. No new replies allowed.