Loop

This is the loop in my main - it does not recognize when a player wins - it just keeps playing... Could someone please tell me what I am doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  	do
	{
		if (player2.score < 100)
		{
			cout << "Player one's turn" << endl;
			playerone();
		}
		else
		{
			cout << "Player Two wins!" << endl;
		}

		if (player1.score < 100)
		{
			cout << "Player two's turn" << endl;
			playertwo();
		}
		else
		{
			cout << "Player one wins!" << endl;
		}
	} while (player1.score < 100 && player2.score < 100);
Last edited on
closed account (2LzbRXSz)
Can you post the full code please?
sure

this is source:
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
#include <iostream>
#include <string>
#include <ctime>
#include "PigDice.h"
using namespace std;
PigDice player1;
PigDice player2;
int score = player1.score1(player1.score);
int total1 = 0, total2 = 0;
void playerone()
{
	int again;
	do {
			int turn1 = 0;
			cout << "Roll the dice. " << endl;
			player1.Roll();
			int score = player1.score1(player1.score);
			if (score == -1)
			{
				total1 = total1 - total1;
				cout << "You lost all your points. " << endl;
				system("pause");
				again = 2;
			}
			else if (score == 0)
			{
				total1 = total1 - turn1;
				cout << "You rolled doubles, points for turn were lost. " << endl;
				system("pause");
				again = 2;
			}
			else
			{
				total1 = total1 + score;
				turn1 = turn1 + score;
				cout << "Your new score is " << total1 << endl
					;
				cout << "Do you want to roll again? " << endl << "1. Yes" << endl << "2. No" << endl;
				cin >> again;
			}
		

		} while (again == 1);
}

void playertwo()
{
	int again;
	do {
		int turn2 = 0;
		cout << "Roll the dice. " << endl;
		player1.Roll();
		int score = player1.score1(player1.score);
		if (score == -1)
		{
			total2 = total2 - total2;
			cout << "You lost all your points. " << endl;
			system("pause");
			again = 2;
		}
		else if (score == 0)
		{
			total2 = total2 - turn2;
			cout << "You rolled doubles, points for turn were lost. " << endl;
			system("pause");
			again = 2;
		}
		else
		{
			total2 = total2 + score;
			turn2 = turn2 + score;
			cout << "Your new score is " << total2 << endl;
			cout << "Do you want to roll again? " << endl << "1. Yes" << endl << "2. No" << endl;
			cin >> again;
		}


	} while (again == 1);
}

int main()
{
	srand(time(0));

	do
	{
		if (player2.score < 100)
		{
			cout << "Player one's turn" << endl;
			playerone();
		}
		else
		{
			cout << "Player Two wins!" << endl;
		}

		if (player1.score < 100)
		{
			cout << "Player two's turn" << endl;
			playertwo();
		}
		else
		{
			cout << "Player one wins!" << endl;
		}
	} while (player1.score < 100 && player2.score < 100);
}


and this is my class

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
#include <iostream>
#include <string>
#include <ctime>

using namespace std;
class PigDice{

private:
	//The face values of the dice
	int die1 = 1;
	int die2 = 1;

public:
	int score;

	// Roll function:
void Roll()
	{
	die1 = rand() % 6 + 1;
	die2 = rand() % 6 + 1;
	cout << "Your dice have the numbers " << die1 << " and " << die2 << endl << endl;
	}

// Score function:    
int score1(int score)
	{
	if (die1 == 1 && die2 == 1) score = -1;
	else if (die1 == die2) score = 0;
	else score = die1 + die2;
	return score;
	}

  // Display function:
void display()
	{
	cout << "Die 1 = " << die1 << " and die 2 = " << die2 << endl;
	}
};

closed account (2LzbRXSz)
player.score isn't really the "score" is what I'm noticing. The values you would need to check lie in total1 and total2

All you need to do is change your do while loop in main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
do
    {
        if (total2 < 100)
        {
            cout << "Player one's turn" << endl;
            playerone();
        }
        else
        {
            cout << "Player Two wins!" << endl;
        }
        
        if (total1 < 100)
        {
            cout << "Player two's turn" << endl;
            playertwo();
        }
        else
        {
            cout << "Player one wins!" << endl;
        }
    } while (total1 < 100 && total2 < 100);

:)
thanks a bunch! (:
closed account (2LzbRXSz)
No prob bob :)
Topic archived. No new replies allowed.