Help on printing a 8x8 grid in C
I need help printing a 8x8 grid in C.
A typical 1x1 grid is supposed to look like this:
+----+
| |
| |
| |
+----+
But I'm not getting that even though I checked everything that would be causing the problem.
Could you guys look over the code and tell me what the problem is?
int main() {
row_count = 2;
col_count = 2;
strcpy(row0NoX, "-----+");
strcpy(row1NoX, " |");
strcpy(row2NoX, " |");
strcpy(row3NoX, " |");
strcpy(开发者_如何学编程row4NoX, "-----+");
printf("Welcome to the Checkerboard program!\n");
for (row = 0; row < row_count; row++) {
for (tiny_row = 1; tiny_row < N_ROWS; tiny_row++) {
printf("%c", row0NoX[strlen(row0NoX)-1]);
printf("%c", row1NoX[strlen(row1NoX)-1]);
printf("%c", row2NoX[strlen(row2NoX)-1]);
printf("%c", row3NoX[strlen(row3NoX)-1]);
printf("%c", row4NoX[strlen(row4NoX)-1]);
for (col = 0; col < col_count; col++) {
switch (checkerboard[row][col]) {
case 0:
switch (tiny_row) {
case 1:
printf("%s", row0NoX);
break;
case 2:
printf("%s", row1NoX);
break;
case 3:
printf("%s", row2NoX);
break;
case 4:
printf("%s", row3NoX);
break;
case 5:
printf("%s", row4NoX);
break;
default:
printf("Error");
break;
}
break;
}
}
printf("\n");
}
}
//printf("Enter a command: ");
return (0);
}
The output is close but somehow I'm missing the |s and the +----+ at the end.
Output for 2x2:
+|||+-----+-----+
+|||+ | |
+|||+ | |
+|||+ | |
+|||+-----+-----+
+|||+-----+-----+
+|||+ | |
+|||+ | |
+|||+ | |
+|||+-----+-----+
Something like this?
#define BLOCK_SIZE_X 5
#define BLOCK_SIZE_Y 4
void draw_box(int w, int h) {
int x, y;
for (y = 0; y <= h * BLOCK_SIZE_Y; y++) {
int on_horizontal = !(y % BLOCK_SIZE_Y);
for(x = 0; x <= w * BLOCK_SIZE_X; x++) {
int on_vertical = !(x % BLOCK_SIZE_X);
if (on_horizontal && on_vertical) {
printf("+");
} else if (on_horizontal) {
printf("-");
} else if (on_vertical) {
printf("|");
} else printf(" ");
}
printf("\n");
}
}
You forgot to print newlines between rows:
for (tiny_row = 1; tiny_row < N_ROWS; tiny_row++) {
printf("%c", row0NoX[strlen(row0NoX)-1]);
for (col = 0; col < 8; col++) {
switch (checkerboard[row][col]) {
case 0:
switch (tiny_row) {
case 1:
printf("%s", row0NoX);
break;
case 2:
printf("%s", row1NoX);
break;
case 3:
printf("%s", row2NoX);
break;
case 4:
printf("%s", row3NoX);
break;
case 5:
printf("%s", row4NoX);
break;
default:
printf("Error");
break;
}
break;
}
// printf("\n"); <-- Not here
}
printf("\n") // <-- Here
}
Also, you forgot to print the line of -'s at the bottom.
I think your new problem is in this line:
for (tiny_row = 1; tiny_row < N_ROWS; tiny_row++) {
I'm guessing N_ROWS
is initialized to 5
.
row4NoX
is the line initialized to all ----
, but since it is never printed, I'm guessing that tiny_row
is never equal to 5
, and that for
loop sure looks funny.
(I'll never get used to 1-initialized loops in C, but when I see one used with a <
test, I assume it is a bug. It might not be the most accurate heuristic in the world, but it's worked well for me so far. :)
The small bit of preachy advice: don't mix variable names and indexes with different numbering schemes. Stick with either 0-indexed or 1-indexed (if you're moving from FORTRAN code :) names, when you try to mix them you often wind up making more mental hurdles than already exist in trying to write reliable software.
精彩评论