I am filling a 10 x 10 grid of characters from an input file. I need to check if the grid is a square (i.e, has N x N characters, where N <= 10)
The input file is such:
pitk
olpe
pkey
tope
When I print the grid in gdb, I get the following result:
$1 = {"pitk\000\000\000\000\366h",
"olpe\000\000\001\000\000",
"pkey\000\000\000\000\000",
"tope\000\000\000\000\000",
"\000\344\241\367\377\177\000\000", <incomplete sequence \336>,
"\000\377\377\177\000\000\037\355\336", <incomplete sequence \367>,
"\000\177\000\000\000\000\000\000\000",
"\000\000\000\000\000\000\000\000\000",
"\000\000\000\000\000\000\000\000\r\020",
"\000\000\000\000\000\000\000\000\000"}
The part of my main function that checks if the grid is valid, is:
bool check = (checknxn(grid));
if(check == false) {
fprintf(stderr, "Invalid Input!\n");
exit(0);
}
The checknxn function:
bool checknxn(char grid[10][10]) {
int columns = 0;
for(int i=0;i<10;i++) {
if(grid[0][i]!=NULL)
columns++;
else {
break;
}
}
for(int i=1;i<10;i++) {
for(int j=columns;j<10;j++) {
if(grid[i][j]!=NULL)
return false;
}
}
int rows = 0;
for(int i=0;i<10;i++) {
if(grid[i][0]!=NULL)
rows++;
else {
break;
}
}
if (!(rows == columns))
return false;
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++) {
if(grid[i][j]==NULL) {
return false;
}
}
}
return true;
}
This returns false, and my program exits, even though the input grid in this case is valid.
I can't figure out why the checknxn function is returning false in this case.
UPDATE: This is how I initialize my grid:
FILE *file1 = fopen(argv[1], "r"); // "r" for read
char grid[10][10];
char c;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
fscanf(file1,"%c", &c);
if (c == '\n') {
grid[i][j] = '\0';
break;
}
if (c == ' ') {
grid[i][j] = '\0';
continue;
}
if (c == '\0') {
grid[i][j] = '\0';
continue;
}
else {
grid[i][j] = c;
}
}
}