hi, i have the following code for tic tac toe game Human vs. Computer, it runs without problem but when i prompt for entering X or O, it gives an error, can someone help, appreciate the answer.

here's my code:

#include <iostream>
#include<conio.h>
#include<process.h>
using std::cout;
using std::cin;

char matrix[3][3];

char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int main()
{
char done;
cout<<"\n'TIC-TAT-TOE'\t Human Vs.Computer\n";
done=' ';
init_matrix();
do{
disp_matrix();
get_player_move();
done=check();
if(done!=' ')
break;
get_player_move();
done=check();
}while(done==' ');
if(done=='X')
cout<<"\nYOU WON";
else
cout<<"I WON";
disp_matrix();
return 0;
}


void init_matrix(void)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
matrix[i][j]=' ';
}
void get_player_move(void)
{
int x,y;
cout<<"\nEnter X,Y coordinates for your move: ";
cin>>x>>y;
x--;
y--;
if (matrix[x][y]!='you have choosen the worng letter')
{
cout<<"\nInvalid move,TRY again";
get_player_move();
}
else matrix[x][y]='X';
}
void get_computer_move(void)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
if(matrix[i][j]==' ')
break;
if (matrix[i][j]==' ')
break;
}
if(i*j==9)
{
cout<<"\nDRAW\n";
exit(0);
}
else
matrix[i][j]='o';
}


void disp_matrix(void)
{
int t;
for(t=0;t<3;t++)
{
cout<<matrix[t][0]<<matrix[t][1]<<matrix;
if(t!=2)
cout<<"\n---|---|---\n";
}
cout<<"\n";
}
char check(void)
{
int i;
for(i=0;i<3;i++)
if (matrix[i][0]==matrix[i][1]&&matrix[i][0])
return matrix[i][0];

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

if (matrix[0][0]==matrix[1][1]&&matrix[1][1])
return matrix[0][0];

if (matrix[0][2]==matrix[1][1]&&matrix[1][1])
return matrix[0][2];
return ' ';
}

if (matrix[x][y]!='you have choosen the worng letter') What is that? ' ' surround single characters, not strings. How could your matrix value ever be equal to that?

Why don't you check if it's equal to 'x'?

Also:

if (matrix[i][0]==matrix[i][1]&&matrix[i][0])
is not doing what you think 
for all those if statements you need  (for example)
if(matrix[i][0] == matrix[i][1] && matrix[i][1] == matrix[i][2] && matrix[i][0] == matrix[i][2])
Then you have a win

thanx for answer, i changed it to "x", still the same, the application crashes and it tells me either to break or continue the execution of the program.

get_player_move(); on line 55, you're making a recursive call (calling a function within itself) which is ok in some circumstances but in this case it's going "infinitely" until the stack overflows and it crashes. Try to rework that section, you can probably put a while loop within the other while loop that calls the function until it returns a boolean:

while(get_player_move());   //in main()

bool get_player_move(void)
{
  int x,y;
   cout<<"\nEnter X,Y coordinates for your move: ";
   cin>>x>>y;
   x--;
   y--;
   if (matrix[x][y]=='X') //this should be if it's equal to X
  {
      cout<<"\nInvalid move,TRY again";
      return false;//get_player_move();
   }

   else 
  {
	matrix[x][y]='X';
	return true;
  }
}

This may not be the exact thing we want but this will take care of the infinite recursion. We will need to revisit the do while loop in main since I think that's causing problems too.

Also: line 85 is a typo cout<<matrix[t][0]<<matrix[t][1]<<matrix[t][2];

Two things:
1) Learn to format your code so we can read it easily
2) Where in the universe does "it gives an error" follow from "it runs without problem"? White is not black...

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.