scanf 2d array size C -
i've tried make custom array size by:
#include <stdio.h> #include <stdlib.h> int main() { int x; int y; printf("array size:"); scanf("%d %d", &x, &y); int kancelar[x][y]; int i; int q; (q = 0; q < x; q++) (i = 0; < y; i++) kancelar[q][i] = getchar(); (q = 0; q < x; q++) (i = 0; < y; i++) putchar(kancelar[q][i]); return 0; }
but no luck, values wrong. i'm using c,
you getting messed newline
s present in input buffer press of enter key after each input. need consume newline each time before going getchar()
.
a simple solution may like
printf("array size:"); scanf("%d %d%*c",&x,&y); //eat newline int kancelar[x][y]; int i; int q; for(q = 0;q < x;q++) { for(i = 0;i < y;i++ ) { kancelar[q][i] = getchar(); getchar(); //eat newline } }
Comments
Post a Comment