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?

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <time.h>
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 << " <R or H>?" << 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:

1
2
3
4
5
6
7
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.