0

I am trying to display this for loop 2d array but I am getting a strange output and I'm not sure what is wrong in my code. I am using an if statement to convert the outer column and row into "x" and the rest should be blank spaces.

#include <iostream>
using namespace std;

int main() {


const int H = 25;
const int W = 82;


char Map[H][W]; // test map display


for(int i = 0; i < H; i++ ){ // display the map
    for(int j = 0; j < W; j++){
        if(i == 0 || i == 24 || j == 0 || j == 81) Map[i][j] = 'x';
        else Map[i][j] = ' ';
        cout << Map[i][j];
    }
}






    return 0;
}

The output I am aiming for should look like this

xxxxxxxxxxxxxxxxxxx
x                 x
x                 x
x                 x
x                 x
xxxxxxxxxxxxxxxxxxx
4

1 に答える 1

3

各行を埋めた後に新しい行を印刷したいと思います:

for(int i = 0; i < H; i++ ){ // display the map
    for(int j = 0; j < W; j++){
        if(i == 0 || i == 24 || j == 0 || j == 81) Map[i][j] = 'x';
        else Map[i][j] = ' ';
        cout << Map[i][j];
    }
    cout << '\n';  //<-------- new line
}

コンピューターは、指示された場合にのみ新しい行を開始します。
これらを に保存するかどうかを検討する必要がある場合がありますMap

于 2013-11-10T17:26:09.367 に答える