4

My code only takes 5 values as input.? What am i doing wrong?

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char arr[3][3];
    int i,j,n;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%c",&arr[i][j]);
        }
    }
    return 0;
}

How should i correct it?

yulian
  • 1,601
  • 3
  • 21
  • 49
user2556926
  • 89
  • 1
  • 5

5 Answers5

8

Change

scanf("%c",&arr[i][j]);

to

scanf(" %c",&arr[i][j]);.

Notice the space given before specifier to consume \n left in stdin buffer when you pressed enter.

Each \n is working as input taking your space from input space.

someone
  • 1,638
  • 3
  • 21
  • 36
Dayal rai
  • 6,548
  • 22
  • 29
3

It should work, but note that %c will read only a single character. Whitespace is not suppressed as it is for other (more "high-level") format specifiers, so if you're separating your characters by blanks of any kind those will be read instead of the actual characters.

Also note that you should check the return value of scanf(), it can fail if no suitable input is present.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

The first input is stored in arr[0][0] then when you press enter(return key) it is stored in arr[0][1] and while you think you are inputting the second character actually you are giving the third input. Try the code below to see if you didnt get it.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char arr[3][3];
    int i,j,n;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
        printf("Input array [%d][%d]",i,j);
            scanf("%c",&arr[i][j]);
        }
    }
}

And as for the correction you need a scanf(" %c",&arr[i][j]); a space infront of %c to consume the \n

Hope it answers your question

Aatish Sai
  • 1,647
  • 1
  • 26
  • 41
0

Instead you can use gets(arr[i][j])

instead of scanf("%c",&arr[i][j]);

This will work perfectly. It is good idea to use gets() and puts() function instead of printf() and scanf() function for string and character in C

Sudeep Acharya
  • 226
  • 2
  • 15
  • No. First, `gets` need a parameter of type `char *`. Second, `gets` is dangerous, one should always avoid it and use `fgets` or `gets_s` instead. Third, `printf` is for formatted output, `puts` is hardly a replacement in many situations. – Yu Hao Sep 02 '13 at 13:40
  • o actually it is `getc` instead of `gets` and it always works fine for the above program and related. – Sudeep Acharya Sep 03 '13 at 11:15
-2

you can use fflush(stdin)

char arr[3][3];
int i,j,n;
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        scanf("%c",&arr[i][j]);
        fflush(stdin);
    }
}
Thi Nguyen
  • 101
  • 5
  • 1
    `fflush(stdin)` is undefined behavior. It may work on a particular implementation but is not portable. – interjay Aug 28 '13 at 08:45
  • 1
    This is undefined behavior, since fflush is meant to be called on an output stream. have a look [here](http://stackoverflow.com/questions/2979209/using-fflushstdin). – Dayal rai Aug 28 '13 at 08:46