Help With Tic Tac Toe

So I'm making a Tic Tac Toe Game and I've gotten into it an then encountered a problem with this code:
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
#include <iostream>
using namespace std;
string squareOne = "1";
string squareTwo = "2";
string squareThree = "3";
string squareFour = "4";
string squareFive = "5";
string squareSix = "6";
string squareSeven = "7";
string squareEight = "8";
string squareNine = "9";
string charToBeEntered;
bool GameOver = (true);
int currentPlayer = 1;
int move;
int main (){
	do{
	do{
    cout << "  " << squareOne << " | " << squareTwo << " | " << squareThree <<"\n-------------\n  " << squareFour << " | " << squareFive << " | " << squareSix << "\n-------------\n  " << squareSeven << " | " <<squareEight << " | " << squareNine << endl;
	cout << "Player " << currentPlayer << " Enter your move: \n";
	cin >> move;
	}while(move != 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9);
	}while(!GameOver);
    return 0;
}

instead of moving on when you enter a number it repeats all that is in the do while loop and when you enter any thing but a number get a never ending loop that constantly repeats.
closed account (o1vk4iN6)
That doesn't do what you think.

1
2
3
4
while(move != 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9);

while( ( move != 1 ) | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9); // infinite loop



You could do this:

 
while( ( move < 1 ) || ( move > 9 ) );
Last edited on
Still got one problem Xerzi, Sure when you enter a number it moves on but when you enter anything else it goes into an infinite loop.
Yep, people know about that issue. std::cin (cin in this code with no std::) you can use as a bool, which is false if you enter the wrong data type (I'm a bit fuzzy on this, it's a bit more complicated, but using it as a bool which is true if everything went to plan and false otherwise should do the job.)
after every cin request, put the following 2 lines of code

1
2
std::cin.clear();
std::cin.ignore();


or maybe its vice-versa, not on my Pc so I can't double check it with my code, but with those 2 lines in the right order, whatever the right order is, entering the wrong data type forces you to reenter it
This is also not the most efficient but a bit more efficient than yours.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
string squareOne = "123456789";
string charToBeEntered;
bool GameOver = (true);
int currentPlayer = 1;
int move;
int main (){
	do{
	do{
    cout << "  " << squareOne[0] << " | " << squareOne[1] << " | " << squareOne[2] <<"\n-------------\n  " << squareOne[3] << " | " << squareOne[4] << " | " << squareOne[5] << "\n-------------\n  " << squareOne[6] << " | " <<squareOne[7] << " | " << squareOne[8] << endl;
	cout << "Player " << currentPlayer << " Enter your move: \n";
	cin >> move;
}while( ( move < 1 ) || ( move > 9 ) );
	}while(!GameOver);
    return 0;
}
Topic archived. No new replies allowed.