Basically, what your program does is: For every character in the file (while
), set ALL (for
, for
) puzzle entries to that character. As a result, all entries will contain the file's last character.
You want to put the for
loops on the outside and read one character per puzzle entry instead:
for (int i = 0; i < PUZZLE_SIZE; ++i)
for (int j = 0; j < PUZZLE_SIZE; ++j) {
c = fgetc(fp);
if (c == '\n')
c = fgetc(fp); // get a new char if we hit a newline
puzzle[i][j] = c - `0`;
}
This is assuming that there are enough characters in the file to fill the puzzle.