Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my for loop not taking 9 inputs?

Tags:

c

loops

for-loop

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?

like image 646
user2556926 Avatar asked Dec 26 '22 21:12

user2556926


1 Answers

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.

like image 141
Dayal rai Avatar answered Dec 28 '22 12:12

Dayal rai