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
  • 2
    What "strange output" do you get? Commented Nov 10, 2013 at 17:21
  • you can see the output through this compiler ideone.com/cBIXOv Commented Nov 10, 2013 at 17:22
  • 1
    Looks fine to me. Did you want it to print a new line for each value of i? Commented Nov 10, 2013 at 17:23
  • Sorry, check the edited question for the output i'm looking for, thanks! Commented Nov 10, 2013 at 17:24

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.