Jun 3, 2010 at 2:06am UTC
I don't know what's wrong with it, but whenever I try to compiler it, it won't run.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int display(char a[3][3]);
void main()
{int r,z,g,k,i,j;
char a[3][3],l='0';
while(l=='0')
{r=0;
z=0;
a[0][0]='1';
a[0][1]='2';
a[0][2]='3';
a[1][0]='4';
a[1][1]='5';
a[1][2]='6';
a[2][0]='7';
a[2][1]='8';
a[2][2]='9';
r=display(a);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=' ';
while(r==0)
{
k=1;
while(k!=0)
{cout<<"enter the position as specified"<<endl;
cin>>g;
if(g<1||g>9)
{cout<<"sorry absurd"<<endl;
k=1;
}
else
{if((a[(g-1)/3][(g-1)%3]=='x')||(a[(g-1)/3][(g-1)%3]=='o'))
{cout<<"sorry it is absurd"<<endl;
k=1;
}
else
k=0;
}
}
if(z%2==0)
a[(g-1)/3][(g-1)%3]='x';
else
a[(g-1)/3][(g-1)%3]='o';
r=display(a);
z++;
}
if(r==1)
cout<<"player 1 wins"<<endl;
if(r==2)
cout<<"player 2 wins"<<endl;
if(r==3)
cout<<"game is drawn"<<endl;
cout<<"wanna play another game \n press 0 to cont"<<endl;
l=getchar();
}
getch();
}
int display(char a[3][3])
{int i,j,r=0,s;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{cout<<a[i][j]<<"|";
}cout<<"\n";
cout<<"-|-|-"<<endl;
}
if((a[0][0]=='x')&&(a[1][1]=='x')&&(a[2][2]=='x'))
r=1;
if((a[0][0]=='x')&&(a[0][1]=='x')&&(a[0][2]=='x'))
r=1;
if((a[0][0]=='x')&&(a[1][0]=='x')&&(a[2][0]=='x'))
r=1;
if((a[2][0]=='x')&&(a[2][1]=='x')&&(a[2][2]=='x'))
r=1;
if((a[1][0]=='x')&&(a[1][1]=='x')&&(a[1][2]=='x'))
r=1;
if((a[0][1]=='x')&&(a[1][1]=='x')&&(a[2][1]=='x'))
r=1;
if((a[0][2]=='x')&&(a[1][2]=='x')&&(a[2][2]=='x'))
r=1;
if((a[0][2]=='x')&&(a[1][1]=='x')&&(a[2][0]=='x'))
r=1;
if((a[0][0]=='o')&&(a[1][1]=='o')&&(a[2][2]=='o'))
r=2;
if((a[0][0]=='o')&&(a[0][1]=='o')&&(a[0][2]=='o'))
r=2;
if((a[0][0]=='o')&&(a[1][0]=='o')&&(a[2][0]=='o'))
r=2;
if((a[2][0]=='o')&&(a[2][1]=='o')&&(a[2][2]=='o'))
r=2;
if((a[1][0]=='o')&&(a[1][1]=='o')&&(a[1][2]=='o'))
r=2;
if((a[0][1]=='o')&&(a[1][1]=='o')&&(a[2][1]=='o'))
r=2;
if((a[0][2]=='o')&&(a[1][2]=='o')&&(a[2][2]=='o'))
r=2;
if((a[0][2]=='o')&&(a[1][1]=='o')&&(a[2][0]=='o'))
r=2;
s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{if((a[i][j]=='x')||(a[i][j]=='o'))
s++;
}
if(r!=1&&r!=2&&s==9)
return (3);
if(r==1|r==2)
return r;
if(r!=1&&r!=2)
if(s!=9)
return (0);
}
Jun 3, 2010 at 2:09am UTC
void main() does not comply with ANSI C++03 standards.
And you should #include <iostream> instead of <iostream.h>
And please use [code] tags.
And why not use strings instead of char arrays?
And there are probably other errors.
-Albatross
Last edited on Jun 3, 2010 at 2:16am UTC
Jun 3, 2010 at 2:22am UTC
A few things.
As Albatross says, use #include <iostream> instead of <iostream.h>
You also don't need <stdio.h> as its an old C library and you don't need <conio.h> as its non standard.
1 2 3 4 5 6 7 8
#include <iostream> // don't use the .h
//#include <stdio.h> // don't need (old)
//#include <conio.h> // don't need (non standard)
int display(char a[3][3]);
int main() // must be int main()
{
// ....
Also you need to prefix the items you are using from <iostream> with std:: like this:
1 2 3 4 5 6 7 8 9 10 11
while (k != 0)
{
// need to put std:: in front of included std library items.
std::cout << "enter the position as specified" << std::endl;
std::cin >> g;
if (g < 1 || g > 9)
{
std::cout << "sorry absurd" << std::endl;
k = 1;
}
You can avoid cetchar();
1 2 3 4 5 6 7
// l = getchar();
std::cin >> l; // this is more C++ like
}
// getchar();
std::cin.ignore();
std::cin.get();
}
Last edited on Jun 3, 2010 at 2:23am UTC
Jun 4, 2010 at 1:44am UTC
Last edited on Jun 4, 2010 at 1:45am UTC