I'm a beginner at c++ and I have to do a game of tic tac toe and I'm having problems with asking the player to play. Please help.

*****************************************************

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

///function prototype
void inttableArray(int [][3], int );
void displayArray (int [][3], int ); 
void inputArrayFunction(int [][3], int ); 
void getMove(int [][3], int );
void getComputerMove(int [][3], int );
char Check(int [][3], int );


int main()
{
	char end;
	
	
	end= ' ';

	int tableOne[3][3]; //array of 3 row and 5 columns


	cout<<"This is the game of Tic Tac Toe.\n";
	cout<<"You will be playing against the computer.\n";


	
	inttableArray(tableOne, 3);

	displayArray(tableOne, 3);

	getMove(tableOne, 3);

	end = Check(tableOne, 3);

	getComputerMove(tableOne, 3);

	inputArrayFunction(tableOne, 3);

	if(end== ' ');
	
	
  if(end=='X') cout<<"You won!";
  else cout<<"I won!!!!";
  displayArray(tableOne, 3); 


	
	return 1;

}

//**************************************************
void inttableArray(int TempArray[][3], int size )
{
	int row, column;

	for ( row = 0; row <3; row ++)
		for (column = 0; column < size; column ++)
			TempArray[row][column] = ' ';
}

//**************************************************
void getMove(int TTempArray[][3], int Tsize)
{
	int x =0;
	int	y=0;
	int n=0;


  cout<<" Please make your move: ";
  cin>>n;

  if(n<1||n>9)
	  cout<<"Invalid move"<<endl;
  else 
	  if(TTempArray[x][y]!= ' ')
	{
		cout<<"Invalid move, try again.\n";
		getMove(TTempArray, 3);
	}
	else TTempArray[x][y] = 'X';

	


}

//**************************************************

void getComputerMove(int STempArray[][3], int Ssize)
{
	int x;
	int	y;
	int n;


  cout<<"Player 2"<<endl;
  cout<<" Please make your move: ";
  cin>>n;

  if(n<1||n>9)
	  cout<<"Invalid move"<<endl;
  else 
	  if(n=' ';n<=9);
 	if(STempArray[y][x]!= ' ')
	{
		cout<<"Invalid move, try again.\n";
		getMove(STempArray, 3);
	}
	else STempArray[y][x] = 'Y';
}

//**************************************************
void displayArray (int DTempArray[][3], int Dsize)
{
	int x=0;
	int y=0;
	
		
	for ( x = 0; x < 3; x ++)
	{
		printf(" %c | %c | %c ",DTempArray[x][0],
            DTempArray[x][1], DTempArray [x][2]);
    if(x!=2) printf("\n---|---|---\n");
  }
  printf("\n");

	
}


//**************************************************
void inputArrayFunction(int inputArray[][3], int inputsize)
{
	int a, b;
	int x = 1;

	printf("\n---|---|---\n");

	for ( a = 0 ;a < 3; a++)
		for (b = 0; b < inputsize; b++)
			if ( b == 2 )
				inputArray[a][b] = 10;
			else 
				inputArray[a][b] = b;
}
//**************************************************

char Check(int TinputArray[][3], int Tinputsize )
{
  int i;
  int row;
  int tableOne[3][3];
  
  for ( row = 0; row <3; row ++)
    if(tableOne[row][0]==tableOne[row][1] &&
       tableOne[row][0]==tableOne[row][2]) return tableOne[row][0];

  for(i=0; i<3; i++) 
    if(tableOne[0][row]==tableOne[1][row] &&
       tableOne[0][row]==tableOne[2][row]) return tableOne[0][row];

  
  if(tableOne[0][0]==tableOne[1][1] &&
     tableOne[1][1]==tableOne[2][2])
       return tableOne[0][0];

  if(tableOne[0][2]==tableOne[1][1] &&
     tableOne[1][1]==tableOne[2][0])
       return tableOne[0][2];

  return ' ';
}

Dani AI

Generated

A concise diagnosis and practical fixes for 's Tic‑Tac‑Toe code.

The posted program shows a few recurring problems: the board is declared as int but treated like characters, input numbers (1–9) are never converted to row/column, the main flow runs only one human move and one computer move instead of looping, Check() inspects an uninitialized local array rather than the passed board, and there are several control-flow/syntax mistakes (stray semicolons, = vs ==, recursive input calls that can blow the stack). was correct to suggest a game loop; ’s header advice is helpful; and ’s suggestion to produce a minimal reproducer will make debugging faster.

Concrete repairs to apply (in order):

  • Use a char board[3][3] (or std::array) and initialize every cell to ' '. Mixing int with '\0'/'X'/'O' leads to confusing bugs.
  • Map a single move number n (1..9) to indices: idx = n-1; row = idx / 3; col = idx % 3. Check board[row][col] == ' ' before writing.
  • Replace single-shot calls with a main loop: repeat (player move → check → computer move → check) until a winner or board full.
  • Rewrite Check(const char b[3][3]) to inspect the passed board (rows, columns, diagonals) and return 'X', 'O', or ' '.
  • Avoid recursive input for validation; use a small input loop that re-prompts on invalid input.
  • Do not overwrite the game board with helper routines (the current inputArrayFunction clobbers state).

Example skeleton (safe, new code):

char board[3][3];
std::fill(&board[0][0], &board[0][0]+9, ' ');

int idx = move - 1;          // move in 1..9
int r = idx / 3, c = idx % 3;
if (board[r][c] == ' ') board[r][c] = 'X';

char winner(const char b[3][3]) {
  for (int i=0;i<3;++i)
    if (b[i][0]!=' ' && b[i][0]==b[i][1] && b[i][1]==b[i][2]) return b[i][0];
  for (int j=0;j<3;++j)
    if (b[0][j]!=' ' && b[0][j]==b[1][j] && b[1][j]==b[2][j]) return b[0][j];
  if (b[0][0]!=' ' && b[0][0]==b[1][1] && b[1][1]==b[2][2]) return b[0][0];
  if (b[0][2]!=' ' && b[0][2]==b[1][1] && b[1][1]==b[2][0]) return b[0][2];
  return ' ';
}

Quick debugging tips: compile with warnings enabled (-Wall -Wextra -std=c++11), print the board after each move, run short test sequences (corner, center, edge) and check index math. A small, focused example that shows only the failing behavior (input → mapping → board update) will make follow‑up help much faster, as suggested.

Recommended Answers

All 4 Replies

I didn't really look at anything accept for the main function. Wouldn't it make sense to have a loop? It looks like your only calling the human to move once, then the computer the move once. Then the game ends... Shouldn't you loop until someone wins or something? And also please be more specific as to what your error is.

It doesn't sound like the problem has anything to do with tic-tac-toe. Can you please make a much, much smaller demonstration of your problem along with input, expected output, and current (incorrect) output.

And as a side note, you shouldn't be using stdio.h or stdlib.h as those aren't standard C++ headers. Use cstdio and cstdlib which are the standard C++ headers.

And as u8sand said, you're only executing things through once. Loop until the game is over, then ask to if the player wants to play again, and if so, re-loop it all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.