Read integers from stdin and store in 2d array (C) -
i'm trying read text file containing integers via stdin , store values in 9x9 array (please note file must read via stdin , not arg)
this have:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main() { int puzzle[9][9]; int i,j,count=0; char value[81]; for( = 0; < 9; i++ ) { for( j = 0; j < 9; j++ ) { scanf("%c", &value[count]); puzzle[i][j] = value[count] - '0'; count++; } } } but doesn't seem convert ascii characters scanf int, thought value[count] - '0' supposed do, end getting values this:
-16-16-160-16-160-16-161 basically i'm trying whats described in thread, in c instead of c++:
how convert 2d char array 2d int array?
edit -
the input file looks (contains both white space , new lines):
0 0 1 9 0 0 0 0 8 6 0 0 0 8 5 0 3 0 0 0 7 0 6 0 1 0 0 0 3 4 0 9 0 0 0 0 0 0 0 5 0 4 0 0 0 0 0 0 0 1 0 4 2 0 0 0 5 0 7 0 9 0 0 0 1 0 8 6 0 0 0 7 7 0 0 0 0 9 2 0 0
the problem not conversion line puzzle[i][j] = value[count] - '0';. problem lies following scanf() statement, scanf("%c", &value[count]);. scanf reading first white space. use scanf(" %c", &value[count]); read input.
Comments
Post a Comment