user1985351
user1985351

Reputation: 4689

2D Array in C++

I'm trying to make a 2D array that looks like:

----
----
----
----

For the life of my I can't figure it, I'm new to C++. Here are my attempts so far:

    char board[boardSize][boardSize];
    for(int i=0;i<boardSize;i++){
        for(int j=0;j<boardSize;j++){
            board[i][j]='-';
        }
    }
    for(int i=0;i<boardSize;i++){
        for(int j=0;j<boardSize;j++){
            cout<<board[i][j];
        }
    }

This gives:

----------------

Can anyone please direct me in the right direction? Thank you!

Upvotes: 0

Views: 90

Answers (1)

Borgleader
Borgleader

Reputation: 15916

for(int i=0;i<boardSize;i++){
    for(int j=0;j<boardSize;j++){
        cout<<board[i][j];
    }
}

should be

for(int i=0;i<boardSize;i++){
    for(int j=0;j<boardSize;j++){
        cout<<board[i][j];
    }
    cout<<endl;
}

you're printing all the "lines" on one line.

Upvotes: 1

Related Questions