0

I am trying to assign a specific part of an array a new value, but it doesn't seem to be inserting the new value into the array.

char matrix[20][8] = {/*160 * '#'*/};

void Draw() {
    system("CLS");
    cout << "Welcome to Primitive Pong v1.0!" << endl;
    for (int i = 0; i < 8; i++) {
        cout << endl;
        for (int j = 0; j < 20; j++) {
            cout << matrix[i][j] << " ";
        }
    }
}

while (gameOver == false) {
    matrix[10][4] = 'O';
    Draw();
    this_thread::sleep_for(chrono::milliseconds(1000));
}

I expect this to output a grid of 160 "#" with a "O" near the middle, but instead it just prints 160 "#". I am trying to make a game of console pong. I have tried using 'matrix[10][4] = {'O'};, but that does nothing different.

2
  • 2
    Your matrix has 20 rows 8 columns but your Draw function accesses 20 columns. Commented Jul 6, 2019 at 5:38
  • I stared at this code for like 20 minutes and didn't notice that lmao Commented Jul 6, 2019 at 5:48

1 Answer 1

4

The problem is that you declare matrix[20][8] but then you access it as if its dimensions are [8][20] instead.

The total is the same but the access doesn't work correctly and, unfortunately, C++ will not check about this kind of mistake. Changing the code to

        cout << matrix[j][i] << " ";

should make things work a you expect.

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.