Tic Tac Toe Day 2024 is on Friday, December 13, 2024: Tic Tac Toe in C++ (Compiler errors)?
Friday, December 13, 2024 is Tic Tac Toe Day 2024. The Craft Barn: Valentine's Day Tic Tac Toe Game Valentine's Day Tic Tac Toe
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 which day Bill Gates desinged his first programme Tic-Tac-Toe? In 1968, when he was 13. See Tic-Tac-Toe: X's or O's? X all day everyday