Default case causing infinite loops in game

I'm making a tic-tac-toe game. everything works fine(this is a small portion of the program), but if a letter is entered then the program goes into an infinite loop. Something is wrong with the default case. I set x equal to false, so it should loop this portion of the code, but not infinitely. I've also tried replacing the "x = false" and "break;" with "return 0;". This terminates the program if a letter is the first thing you enter, but if you enter a letter after your first move then it goes into an infinite loop again. Could someone please explain why this is happening.

FYI: I have the do-while loop and if-else statements in there so that if the player tries to move in a spot where there is already an 'X' or 'O', then the program will loop this portion and ask them to move somewhere else.

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
 //Player move
				do
				{
				cout << "Where would you like to move? ";
				cin >> PlayerMove;
				switch (PlayerMove)
				{
				case 1:
					if(box1 == 'X' || box1 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else if(box1 == '1')
					{
						box1 = 'X';
						x = true;
					}
					break;
				case 2:
					if(box2 == 'X' || box2 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box2 = 'X';
						x = true;
					}
					break;
				case 3:
					if(box3 == 'X' || box3 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box3 = 'X';
						x = true;
					}
					break;
				case 4:
					if(box4 == 'X' || box4 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box4 = 'X';
						x = true;
					}
					break;
				case 5:
					if(box5 == 'X' || box5 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box5 = 'X';
						x = true;
					}
					break;
				case 6:
					if(box6 == 'X' || box6 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box6 = 'X';
						x = true;
					}
					break;
				case 7:
					if(box7 == 'X' || box7 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box7 = 'X';
						x = true;
					}
					break;
				case 8:
					if(box8 == 'X' || box8 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box8 = 'X';
						x = true;
					}
					break;
				case 9:
					if(box9 == 'X' || box9 == 'O')
					{
						cout << "You can't do that. ";
						x = false;
					}
					else
					{
						box9 = 'X';
						x = true;
					}
					break;
				default:
						cout << "\nYou can't put letters in this program.\n\n";
						x = false;
						break;
				}
				}while(x == false);
Last edited on
Topic archived. No new replies allowed.