Hi guys, how are you?
I need your help in a tic tac game, I have created a program that asks a user to specify first the index whether [0][0],[2][1],[2][2],etc..and to enter the char letter either 'x' or 'o' but I don't know where is the wrong? can any any one help me

#include<stdio.h>
int main()
{
char array[3][3];
/* I want know somthinh here, in the each cell of this array does it take a charecter or a string*/
int i=0,l=0;
int x,y;

 for(i=0;i<3;i++)
    {
       for(l=0;l<3;l++)
       {
         printf("Enter your index:");
         scanf("%d %d",&x,&y);
         printf("Enter your value inside:");
/*I want to know here why the program stops running if I used %c in scanf as below and continue looping if I used in the scanf like this ("%s",array[i][l]);*/       
scanf("%c",&array[x][y]);
        }
     }
//here to print the vlaues of the array
for(i=0;i<3;i++)
{
      for(l=0;l<3;l++)
          {
              printf("%c",&array[i][l]);
           }
   printf("\n");
}

return 0;
}

Recommended Answers

All 4 Replies

I'm fine, but now my eyes are bleeding because you've yet again failed to grasp the concept of code tags, despite the watermark at the back of the edit window, and "how to" threads in bold writing at the top of every forum.

>>but I don't know where is the wrong?
What does it not do correctly? I see a couple problems with line 17:

1. What happens if you enter a value for either x or y that are outside the range of the array? For instance, enter a value of 50000 and see what happens to your pgoram. You need to validate the coordinates before using them in line 17.

2. Can I enter the same coordinates more than once ? If not, what are you doing to prevent that ?

After every scanf() you need to flush the keyboard of the '\n' key that it leaves there as well as any other extraneous keys you might type. Unfortunately there is no really good standard C way to do it. My solution is to call fgets() with a char buffer.

char buf[255];
fgets(buf,sizeof(buf), stdin);

That too is not perfect solution but probably good enough for most programs.

my freinds, what I am trying to say is that what is the suitable way to let the compiler let you to enter a charecter in a two dimensional array what I have done of looping like this is wrong

for(i=0;i<3;i++)
{       for(l=0;l<3;l++)
{
printf("Enter your index:");
scanf("%d %d",&x,&y);
printf("Enter your value inside:");
scanf("%c",&array[x][y]);
}

either you use scanf("%c",&array[x][y]) or you use ("%s",&array[x][y]) is wrong.
guys I realy want to know how you let a compiler to allow you to enter a charecter in two dimensional array then print it

thanks

>>either you use scanf("%c",&array[x][y]) or you use ("%s",&array[x][y]) is wrong.
The second form is certainly because %s is for strings not single characters. I don't understand why you think the first form is wrong.

I already told you how to resolve the problem. Here is the program that works All I did was add lines 7, 14 and 18 to flush the keyboard buffer of the '\n' character.

int main()
{
char array[3][3];
/* I want know somthinh here, in the each cell of this array does it take a charecter or a string*/
int i=0,l=0;
int x,y;
char iobuf[255];
 for(i=0;i<3;i++)
    {
       for(l=0;l<3;l++)
       {
         printf("Enter your index:");
         scanf("%d %d",&x,&y);
         fgets(iobuf,sizeof(iobuf),stdin);
         printf("\nEnter your value inside:");
/*I want to know here why the program stops running if I used %c in scanf as below and continue looping if I used in the scanf like this ("%s",array[i][l]);*/       
scanf("%c",&array[x][y]);
         fgets(iobuf,sizeof(iobuf),stdin);
        }
     }
//here to print the vlaues of the array
for(i=0;i<3;i++)
{
      for(l=0;l<3;l++)
          {
              printf("%c",&array[i][l]);
           }
   printf("\n");
}

return 0;
}
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.