infinite loop issue that is elusive as ever

I am creating a game of pig where the user plays against the computer in a race to reach 100 points. they take turns rolling the die and adding up the points until they hit 100. The turn ends and no points are awarded if the player rolls a 1. The computer ends the turn if it hits 20 points and the player ends the turn if they press any button besides 'r' since r makes them roll. My issue is that my program infinitely loops at the mother while loop that checks to see if anyone has won i.e. reached 100 points. I have been searching for so long and i can not seem to find whats causing the loop. If you could help asap, i would appreciate it.

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
#include "pch.h"
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>


int main()
{
	srand(time(0));
	std::cout << "You will be player 2. Press R to roll. Any other button will cause you to hold." << std::endl;
	int p2Score = 0;
	int p2Total = 0;
	char p2Input = 'r';
	bool p2Roll1 = false;
	bool cpuKeepGoing = true;
	bool p2KeepGoing = true;
	bool p2Pass = false;
	int cpuScore = 0;
	int cpuTotal = 0;
	bool cpuPass = false;
	bool cpuRoll1 = false;
	int cpuTurnMax = 0;
	int p2Roll;
	int cpuRoll;
	bool p2Win = false;
	bool cpuWin = false;
	while ((p2Win == false) & (cpuWin == false)) {
		std::cout << "it is Player 1's turn." << std::endl;
		while ((cpuTurnMax < 20) & (cpuRoll1 == false) & (cpuPass == false) & (cpuTotal <= 100)) {
			cpuRoll = rand() % (6 - 1 + 1) + 1;
			std::cout << "Roll: " << cpuRoll << std::endl;
			cpuKeepGoing = false;
			if ((cpuRoll == 1) & (cpuKeepGoing == false)) {
				std::cout << "Player 1 rolled a one. No points earned for this turn." << std::endl;
				cpuScore = 0;
				std::cout << "Turn total: " << "0" << std::endl;
				std::cout << "New Score: " << cpuTotal << std::endl;
				std::cout << "Player 1 Score: " << cpuTotal << std::endl;
				std::cout << "Player 2 Score: " << p2Total << std::endl;
				cpuRoll1 = true;
			} else {
				cpuScore = cpuRoll + cpuScore;
				cpuTurnMax = cpuTurnMax + cpuRoll;

				if (cpuTurnMax >= 20) {
					cpuPass = true;
					cpuTotal = cpuTotal + cpuScore;
					cpuTurnMax = 0;
					if (cpuTotal >= 100) {
						cpuWin = true;
					} else {
						cpuWin = false;
					}
					std::cout << "Turn total: " << cpuScore << std::endl;
					std::cout << "New Score: " << cpuTotal << std::endl;
					std::cout << "Player 1 Score: " << cpuTotal << std::endl;
					std::cout << "Player 2 Score: " << p2Total << std::endl;
				} else {
					cpuKeepGoing = true;
					break;
				}
			}
			std::cout << "it is Player 2's turn." << std::endl;
			std::cout << "Press R to roll. Any other key will end your turn. " << std::endl;
			while ((p2Roll1 == false) & (p2Total <= 100) & (p2KeepGoing == true)) {
				std::cin >> p2Input;
				if ((p2Input == 'r') & (p2KeepGoing == true)) {
					p2Roll = rand() % (6 - 1 + 1) + 1;
					std::cout << "Roll: " << p2Roll << std::endl;
					p2KeepGoing = false;
					if ((p2Roll == 1) & (p2KeepGoing = true)) {
						std::cout << "Player 2 rolled a one. No points earned for this turn." << std::endl;
						p2Score == 0;
						std::cout << "Turn total: " << "0" << std::endl;
						std::cout << "New Score: " << p2Total << std::endl;
						std::cout << "Player 1 Score: " << cpuTotal << std::endl;
						std::cout << "Player 2 score: " << p2Total << std::endl;
						p2Roll1 = true;
						p2KeepGoing = false;
					} else {
						p2Score = p2Roll + p2Score;
						p2KeepGoing = true;

					}
				} else {
					std::cout << "You have chosen to end the turn. " << std::endl;
					p2Total = p2Score + p2Total;
					std::cout << "Score: " << p2Score << std::endl;
					std::cout << "Player 1 Score: " << cpuTotal << std::endl;
					std::cout << "Player 2 Score: " << p2Total << std::endl;
					if (p2Total >= 100) {
						p2Win = true;
					} else {
						p2Win = false;
					}
					p2KeepGoing = false;
					cpuPass = false;
					cpuRoll1 = false;

				}



			}
			
			if (p2Win == true) {
				std::cout << "CONGRATULATION! YOU WIN!" << std::endl;
			} else if (cpuWin == true) {
				std::cout << "YOU HAVE BEEN DEFEATED!" << std::endl;

			} else {
				cpuKeepGoing = true;
				p2KeepGoing = true;
				break;
			}

			return 0;

		}

	}

}
line 29 :while ((p2Win == false) & (cpuWin == false)) {
& is not a logical operator, if you meant to use the operator AND you should use &&

see https://en.cppreference.com/w/cpp/language/operator_logical

same problem for the lines
31: while ((cpuTurnMax < 20) & (cpuRoll1 == false) & (cpuPass == false) & (cpuTotal <= 100))

35: if ((cpuRoll == 1) & (cpuKeepGoing == false))

67: while ((p2Roll1 == false) & (p2Total <= 100) & (p2KeepGoing == true))

69: if ((p2Input == 'r') & (p2KeepGoing == true))

73: if ((p2Roll == 1) & (p2KeepGoing = true))

That 'might' cause your program to behave in different way that you intend to!



Last edited on
Using & is a bitwise operation. What you want is a logical && operation. See:

http://www.cplusplus.com/doc/tutorial/operators/
thanks for the help but the problem still persists.
> thanks for the help but the problem still persists.
You need to post your latest code.
Simply acknowledging the issues is a far cry from showing that you've comprehended and made all the necessary changes.


It might help if your program wasn't a ball of mud.
https://en.wikipedia.org/wiki/Big_ball_of_mud

Your main would be a lot clearer if it were just
1
2
3
4
5
6
7
8
9
10
int main ( ) {
   do {
        winner = playComputer();
        if ( !winner ) {
            playUser();
        }
   } while ( someCondition );

   // who won?
}


Yes, it will be hard work to figure out how to move code around, and figure out what parameters to pass and return (hint: a struct could come in handy).

Painful to do perhaps, but it's better than trying to debug a main which is 200 lines long, then 300 lines long.

It might help if your program wasn't a ball of mud.
I was going to report salem c for being mean to a beginner. Then I figured I'd just chastise him But after reading the code more closely, I have to agree with this assessment. The code is real mess.

Here are some examples:
1
2
3
            while ((p2Roll1 == false) & (p2Total <= 100) & (p2KeepGoing == true)) {
                std::cin >> p2Input;
                if ((p2Input == 'r') & (p2KeepGoing == true)) {
At line 3, p2KeepGoing will always be true. If it was false then the while loop wouldn't have executed.

1
2
                    p2KeepGoing = false;
                    if ((p2Roll == 1) & (p2KeepGoing = true)) {
The if will always be false because you set p2KeepGoing on the line right before it.

I think a big reason for the mess is that you're using lots of boolean variables when you should just be checking for the conditions directly as needed. Also, don't be afraid to break out of a loop when you discover that you should.
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>

int
main()
{
    srand(time(0));
    std::cout <<
	"You will be player 2. Press R to roll. Any other button will cause you to hold."
	      << std::endl;
    int p2Total = 0;
    char p2Input = 'r';
    int cpuTotal = 0;

    while (cpuTotal < 100 && p2Total < 100) { // while no winner
	std::cout << "it is Player 1's turn." << std::endl;
	int scoreThisTurn = 0;
	while ((scoreThisTurn < 20) && (cpuTotal+scoreThisTurn < 100)) {
	    int roll = rand() % (6 - 1 + 1) + 1;
	    std::cout << "Roll: " << roll << std::endl;
	    if (roll == 1) {
		std:: cout << "Player 1 rolled a one. No points earned for this turn."
			   << std::endl;
		scoreThisTurn = 0;
		break;
	    } else {
		scoreThisTurn += roll;
	    }
	}
	cpuTotal += scoreThisTurn;
	std::cout << "Turn total: " << scoreThisTurn << std::endl;
	std::cout << "New Score: " << cpuTotal << std::endl;
	std::cout << "Player 1 Score: " << cpuTotal << std::endl;
	std::cout << "Player 2 Score: " << p2Total << std::endl;
	
	if (cpuTotal >= 100) break; // CPU won

	// 
	///////////////  Player 2's turn
	// 

	scoreThisTurn = 0;
	std::cout << "it is Player 2's turn." << std::endl;
	std::cout << "Press R to roll. Any other key will end your turn. " << std::endl;
	while (p2Total+scoreThisTurn < 100) {
	    std::cin >> p2Input;
	    if ((p2Input == 'r')) {
		int roll = rand() % (6 - 1 + 1) + 1;
		std::cout << "Roll: " << roll << std::endl;
		if ((roll == 1)) {
		    std::cout << "Player 2 rolled a one. No points earned for this turn."
			      << std::endl;
		    scoreThisTurn = 0;
		    break;
		} else {
		    scoreThisTurn += roll;
		}
	    } else {
		std::cout << "You have chosen to end the turn. " << std::endl;
		break;
	    }
	}
	p2Total += scoreThisTurn;
	std::cout << "Turn total: " << scoreThisTurn << std::endl;
	std::cout << "New Score: " << p2Total << std::endl;
	std::cout << "Player 1 Score: " << cpuTotal << std::endl;
	std::cout << "Player 2 score: " << p2Total << std::endl;
    }

    // Somebody won
    if (p2Total >= 100) {
	std::cout << "CONGRATULATION! YOU WIN!" << std::endl;
    } else {
	std::cout << "YOU HAVE BEEN DEFEATED!" << std::endl;
    }
}

Topic archived. No new replies allowed.