I am trying to take the input of the following type:
5 4
a a a a
a b a p
c d e a
d b c a
d h i k
Using the following code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n;
scanf("%d %d", &m, &n);
char mat[m][n], str[2 * n];
for (int i = 0; i < m; i++)
{
fgets(str, 2 * n, stdin);
//char d;
//getc(d); // added to absorb if any char remains in stream after pressing enter
int j = 0;
while (j < n)
{
mat[i][j] = str[2 * j];
j++;
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("%c ", mat[i][j]);
printf("\n");
}
return 0;
}
The program doesn't take the second line of input as when I press enter after it just prints the output and exists. When I run using GDB it shows Invalid Parameter passed to C runtime function
. I have tried searching it and tried by adding getc after fgets but no success. I am unable to debugg the program.
Please help me. It would be nice if we can make a generic program for the following type of input.
5 4
a ar a alp
atg bk a p
cf dg er alphs
a g hdb gdb
klat cmat s fun
Thanks in advance.