Need help with a tic tac toe program
Mar 15, 2013 at 11:11pm UTC
Gotta have a semicolon (;) after the do....while(condition); conditional.
You show
1 2 3
do {
...
} while (condition)
It should be:
1 2 3
do {
......
} while (condition);
Also I see no playAgain variable definition in main
Last edited on Mar 15, 2013 at 11:13pm UTC
Mar 15, 2013 at 11:24pm UTC
I just changed it to while(true) and it worked. Thanks so much for the help guys. It worked like a charm!
Mar 15, 2013 at 11:38pm UTC
no problem. Good luck
Mar 15, 2013 at 11:52pm UTC
Well..I think I spoke a bit prematurely before. I have one tiny problem left. The program restarts whether the users selects 'y' to play again or 'n' to end the program. I'm not really sure how to go about it fixing this, but I imagine it'd be easy for someone who knows more about this.
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
int main ()
{
const int row = 3;
const int col = 3;
int count = 1;
char board[row][col] = {BLANK};
int checkwin = CheckWinner(board);
int againPlay;
getNames();
do
{
InitializeBoard(board);
PrintBoard(board);
while (true )
{
PlayX(board);
PrintBoard(board);
checkwin = CheckWinner(board);
if ((checkwin == X)||(checkwin == BLANK))
break ;
PlayO(board);
PrintBoard(board);
checkwin = CheckWinner(board);
if ((checkwin == O)||(checkwin == BLANK))
break ;
}
if (checkwin == X)
{
cout << name1 << " has won!" << endl;
}
else if (checkwin == O) {
cout << name2 << " has won!" << endl;
}
else if (checkwin == BLANK){
cout << "The game ends in a tie!" << endl;
}
playAgain();
}
while (playAgain);
return 0;
}
I have the do while loop set to while(playAgain), so I'm not sure what the problem is.
Mar 16, 2013 at 2:48am UTC
You aren't using the bool returned by playAgain().
You can do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
bool again = true ;
do
{
// things
again = playAgain();
} while (again);
// Or
do
{
// things
} while (playAgain()); // Calling the function here
// You can not do:
do
{
// things
bool again = playAgain();
} while (again);
Topic archived. No new replies allowed.