Char is not outputting what it is supposed to.
This program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int x, y;
char grid[16][16];
for(x=0;x<16;x++)
grid[x][0] = '.';
for(y=0;y<16;y++)
grid[0][y] = '.';
for(x=0;x<26;x++)
grid[x][15] = '.';
for(y=0;y<16;y++)
grid[15][y] = '.';
for(x=1,y=1;x<15,y<15;x++,y++)
grid[x][y] = ' ';
for(x=0;x<16;x++)
{
for(y=0;y<16;y++)
cout << grid[x][y];
cout << endl;
}
| |
should create a box of periods surrounding space, but I get this instead:
................
. .
. \200
\340.
.\357\340 \313o\340\217K&\340.
.\367\377\277 \350\366\377\277r<\340.
. \367\377.
.\367\377\277l\367 \277.
. .
. .
. .
. .
.\340\217<\367\377 l\367\377.
. .
. \300\340\217 \342.
.\274 .
................
|
Line 8 in your code, x<26
?
I see a typo
1 2
|
for(x=0;x<26;x++)
grid[x][15] = '.';
| |
you array has only
16 rows not
26.
To escape such a typo it is better to set dimensions of an array with constants.
For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
const size_t N = 16;
char grid[N][N];
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
if ( i == 0 || j == 0 || i == N - 1 || j == N - 1 ) grid[i][j] = '.';
else grid[i][j] = ' ';
}
}
for( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
cout << grid[i][j];
}
cout << endl;
}
| |
I have not tested this code but hope it is correct.
Last edited on
The typo was not the problem, I am still getting:
................
. .
. \200
\340.
.\357\340 \313o\340\217K&\340.
.\367\377\277 \350\366\377\277r<\340.
. \367\377.
.\367\377\277l\367 \277 .
. .
. .
. .
. .
. \340\217<\367\377 l\367\377.
. .
. \300\340\217 \342.
. \274 .
................
|
change the loop
1 2
|
for(x=1,y=1;x<15,y<15;x++,y++)
grid[x][y] = ' ';
| |
to
1 2 3 4 5 6 7
|
for ( x = 1; x < 15; x ++ )
{
for ( y = 1; y < 15; y++ )
{
grid[x][y] = ' ';
}
}
| |
Topic archived. No new replies allowed.