Reputation: 755
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
Upvotes: 0
Views: 83
Reputation: 19232
I suspect you want to print a new line after filling each row:
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
}
The computer will only start a new line if you tell it to.
You may need to consider if you wish to store these in the Map
or not.
Upvotes: 3