For a homework assignment i have to build a Tic Tac Toe game. The game runs fine until some one has won the game and i try to break out of the while loop. I've checked all the returned values from each function and they all seem to be working properly so the problem must lie somewhere with the syntax of my loop. However, it seems to be logical.
InitBoard(boardAr, AR_ROW, AR_COL);
do {
// Game start
turn++;
// Displays Game Board
DisplayBoard(boardAr);
// Gets and validates input
GetAndCheckInp(boardAr, token, AR_ROW, AR_COL, playerX,
playerO);
// Checks for a win; if X wins 'X' will be returned, if O wins 'O' will be returned, 'T' for tie or
// ' ' if no win or tie has been made
whoWon=CheckWin(boardAr, turn);
// Switches the token from 'X' to 'O' or 'O' to 'X'
token=SwitchToken(token);
// If anything but ' ' is saved to whoWon, gameOver will be assigned the value 'true'
if(whoWon!=' ')
{
gameOver=true;
}
// Crashing here
}while(!gameOver);
// Output game results
OutputWinner(whoWon, playerX, playerO);
Any help would be appreciated, im pulling my hair out over here!
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
What do you mean by "crashing"? Does it freeze? Does it show the "program has stopped responding" box? Does it make your computer bluescreen? Does it just vanish? You need to be specific.
I used a series of statements such as "cout << "test 1\n\n"; cout << "test 2\n\n"; through out the loop and my test statement that was on line 28 never printed after someone had won the game. Also my OutputWinner() function has never printed either. so im quite confident that somewhere between line 23 and line 30 is the problem.
i've tried to use my IDE's debugger, im using eclipse, but i havent been able to make much sense of it and as far as i could tell it didnt give me any more information than when i would run it normally.
I don't believe you. There is nothing between line 23 and 30 that could cause your program to crash - it sounds like you are experiencing a corrupted program state due to overwriting memory or similar.
Run your program in the debugger until it crashes, then examine where it crashed and see the value of the various variable at the time of the crash.
I don't have much programming experience and I haven't learned how to properly use a debugger. I agree with you, there is nothing between those lines that would cause it to crash. However I do know that nothing at or after those lines will be executed after someone wins.
When I ran it till it crashed in my debugger the only thing I saw was something along the lines of "signal stopped - segmentation error" followed by a long line of things that I can't remember at the moment. But like I said my understanding of the debugger is tenuous at best.
When I ran it till it crashed in my debugger the only thing I saw was something along the lines of "signal stopped - segmentation error"
This is it - you have memory corruption. It makes unbelievable things happen, which is why I didn't believe you. When you get the chance you should check over all your code and make sure you are not accessing memory incorrectly through pointers, etc.