io - C: Logical error in scanning input -
i have following implementation of reading character matrix , printing back. works fine, when give matrix it, waits character , outputs matrix properly. how can fix not need input character?
sample input
3 4 0001 0110 1110
sample output
0001 0110 1110
my code
#include <stdio.h> #include <stdlib.h> int main() { int n, m; /* n, m - dimensions of matrix */ int i, j; /* i, j - iterators */ char **matrix; /* matrix - matrix input */ scanf ("%d %d\n", &n, &m); matrix = (char **) malloc (sizeof (char *) * n); (i = 0; < n; ++i) { matrix[i] = (char *) malloc (sizeof (char) * m); } (i = 0; < n; ++i) { (j = 0; j < m; ++j) { scanf ("%c ", &matrix[i][j]); } } (i = 0; < n; ++i) { (j = 0; j < m; ++j) { printf ("%c", matrix[i][j]); } printf ("\n"); } }
thanks in advance.
put space before %c
. if have whitespace after %c
, scanf() keep reading , ignoring whitespaces. hence forced input non-whitespace character.
change:
scanf ("%c ", &bitmap[i][j]);
to:
scanf (" %c", &bitmap[i][j]);
Comments
Post a Comment