Question:
Tic Tac Toe in C++ (Compiler errors)?
MichaelC1993
2011-02-27 14:55:44 UTC
I was bored today so I decided to write this game in C++, it is not fully completed but I ran into these compiler errors when attempting to compile the program and I am kind of stuck as to why I am getting these errors despite how obvious it may be, so if anybody could help that would be wonderful.

code:

#include
#include

using namespace std;

string grid [3][3];

grid[0][0] = "1";
grid[0][1] = "2";
grid[0][2] = "3";
grid[1][0] = "4";
grid[1][1] = "5";
grid[1][2] = "6";
grid[2][0] = "7";
grid[2][1] = "8";
grid[2][2] = "9";

string player_x (int player_x_move);
string player_o (int player_o_move);
void show_game_grid ();


string player_x (int player_x_move)
{

switch (player_x_move)
{
loop:
case 1: grid[0][0] = "x";
break;
case 2: grid[0][1] = "x";
break;
case 3: grid[0][2] = "x";
break;
case 4: grid[1][0] = "x";
break;
case 5: grid[1][1] = "x";
break;
case 6: grid[1][2] = "x";
break;
case 7: grid[2][0] = "x";
break;
case 8: grid[2][1] = "x";
break;
case 9: grid[2][2] = "x";
break;
default: cout << "Invalid choice, please enter another grid space" << endl;
goto loop;
}

return 0;

}

string player_o (int player_o_move)
{

switch (player_o_move)
{
loop1:
case 1: grid[0][0] = "o";
break;
case 2: grid[0][1] = "o";
break;
case 3: grid[0][2] = "o";
break;
case 4: grid[1][0] = "o";
break;
case 5: grid[1][1] = "o";
break;
case 6: grid[1][2] = "o";
break;
case 7: grid[2][0] = "o";
break;
case 8: grid[2][1] = "o";
break;
case 9: grid[2][2] = "o";
break;
default: cout << "Invalid choice, please enter another grid space" << endl;
goto loop1;
}

return 0;

}

void show_game_grid()
{
cout << grid[0][0];
cout << grid[0][1];
cout << grid[0][2] << endl;
cout << grid[1][0];
cout << grid[1][1];
cout << grid[1][2] << endl;
cout << grid[2][0];
cout << grid[2][1];
cout << grid[2][2] << endl << endl;
}

int main()
{
void show_game_grid();
cout << "Player 1, it is your turn, you are X's" << endl << "Press the number you wish to place your 'X' on the grid" << endl << endl;
string player_x (int player_x_move);
void show_game_grid();
}

compiler errors:

TTT.cpp:8: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:9: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:10: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:11: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:12: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:13: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:14: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:15: error: expected constructor, destructor, or type conversion before ‘=’ token
TTT.cpp:16: error: expected constructor, destructor, or type conversion before ‘=’ token
Compilation failed.


Again, I am sure the error is quite obvious and it is just me being stupid but I would appreciate the help here,

~ Thanks, Michael
Three answers:
Shawn
2011-02-27 16:29:06 UTC
well first of all u need to do a lot more reading on C++, from what i see in ur code u understand the general idea of programing but u really have some problems in ur C++ concepts.



okay lets see ur code now, u can only assign value to variable when it is in a method such as main(), or when u r declaring a variable. so in ur case u must assign value to grid when u r declaring it so as follow:

string grid [3][3] = { {"1","2","3"} , {"4","5","6"} , {"7","8","9"} };



this one is simple, NEVER and i mean NEVER use goto, its very hard to use it right and its 100% useless. this is a command from old days so just forget about it and never use it. i cannot be more persistent about this, DO NOT USE "goto";



when u want to call any method from another method u do not need to put the whole declaration. for example in main for calling void show_game_grid(); u simply have to write show_game_grid(); no need for void and for string player_x (int player_x_move); u have to write player_x (player_x_move);



up to this point should fix all ur compiler errors, but i recommend that u read the rest of my suggestions too.



the two methods that u have for player do not need to return any thing so simply change them to void and remove the return statement. like this:

void player_x (int player_x_move)



its a good idea to add some space btw ur grid when u are printing it out too, it would look better. like this:

cout<


and in ur main if u use a while loop u wont need to use goto anymore. u simply call ur methods in ur loop and they will be called continuously till u exit ur application.



also u don't need to provide methods declaration for ur methods when u are declaring them before main, u only need to do that when they are after ur main method.

so this is how ur code should look like:

#include

#include



using namespace std;



string grid [3][3] = { {"1","2","3"} , {"4","5","6"} , {"7","8","9"} };



void player_x (int player_x_move)

{

switch (player_x_move)

{

case 1: grid[0][0] = "X";break;

case 2: grid[0][1] = "X";break;

case 3: grid[0][2] = "X";break;

case 4: grid[1][0] = "X";break;

case 5: grid[1][1] = "X";break;

case 6: grid[1][2] = "X";break;

case 7: grid[2][0] = "X";break;

case 8: grid[2][1] = "X";break;

case 9: grid[2][2] = "X";break;

default: cout << "Invalid choice, please enter another grid space" << endl;

}

}



void player_o (int player_x_move)

{

switch (player_x_move)

{

case 1: grid[0][0] = "O";break;

case 2: grid[0][1] = "O";break;

case 3: grid[0][2] = "O";break;

case 4: grid[1][0] = "O";break;

case 5: grid[1][1] = "O";break;

case 6: grid[1][2] = "O";break;

case 7: grid[2][0] = "O";break;

case 8: grid[2][1] = "O";break;

case 9: grid[2][2] = "O";break;

default: cout << "Invalid choice, please enter another grid space" << endl;

}

}



void show_game_grid()

{

cout<
cout<
cout<
}



int main()

{

int input = 0;

while(true){

show_game_grid();

cout << "Player 1, it is your turn, you are X" << endl;

cout<< "Press the number you wish to place your 'X' on" << endl << endl;

cin >> input;

player_x(input);

show_game_grid();

cout << "Player 2, it is your turn, you are O" << endl;

cout<< "Press the number you wish to place your 'O' on" << endl << endl;

cin >> input;

player_o(input);

}

}



and just for extra this is how i would write the code:

#include

#include



using namespace std;



string grid [3][3] = { {"1","2","3"} , {"4","5","6"} , {"7","8","9"} };



void player(int move,string sign)

{

switch (move)

{

case 1: grid[0][0] = sign;break;

case 2: grid[0][1] = sign;break;

case 3: grid[0][2] = sign;break;

case 4: grid[1][0] = sign;break;

case 5: grid[1][1] = sign;break;

case 6: grid[1][2] = sign;break;

case 7: grid[2][0] = sign;break;

case 8: grid[2][1] = sign;break;

case 9: grid[2][2] = sign;break;

default: cout << "Invalid choice, please enter another grid space" << endl;

}

}



void printGrid()

{

cout<
for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

cout<
}

cout<
}

cout<
}



void doTurn(string sign){

int input = 0;

printGrid();

cout << "Player "<
cout << ">> ";

cin >> input;

player(input,sign);

}



int main()

{

while(true){

doTurn("X");

doTurn("O");

}

}

if u compare it u will see there is much less redundancy. and code can be easily be read and changed when it is needed. i did not add anything else to the code such as choosing winner and keeping score. when u add those part to it if u want post it to this question so i can see what u have done.

good luck and keep coding
Ratchetr
2011-02-27 16:21:47 UTC
The errors are coming from these lines:

grid[0][0] = "1";



You can't have code like that outside of any function in your code. You could move those lines of code into main (or some other function), and they will work. Or, you can initialize the array at compile time using this syntax:



string grid[3][3] = {

  { "1","2","3" },

  { "4","5","6" },

  { "7","8","9" },

};



That will let it run, but it doesn't do very much. 3 of your 4 lines in main are like this:

void show_game_grid();



That does NOT call show_game_grid. It's a function prototype (because it starts with a return type). To call the function, drop the void:

show_game_grid();



You also don't have any input functions yet. You'll need that next so you can actually make a move.
?
2016-10-17 07:20:37 UTC
i do no longer think of that that's an argument of who prefer the X to the O, i think of that X (for some reason) consistently is going first. I somewhat have in no way performed a sport of tic tac toe and O going first. bizarre, yet I in no way theory-approximately it before. :o)~


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...