0

I have been trying to get this to work correctly; however it seems I can not figure this out. I am trying to get a game board to initialize correctly but it keeps saying that <error reading characters of string>.

using namespace std;

int main()
{

    board show;
    show.init();
    show.printing();

}


class board {

public:
    void init(){

        string Board[8][9] = {
            { "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
            { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
            { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
            { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
            { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
            { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
            { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
            { "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
        };
    }

    void printing(){

        string character = "*";

        int position[2] = { 2, 2 };
        // Draw the grid once
        for (int i = 0; i < 8; i++){
            for (int j = 0; j < 9; j++){
                if (i == position[0] && j == position[1])
                    cout << character;
                else
                    cout <<  Board[8][9];
                cout << " ";
            }
            cout << endl;
        }
    }

private:
    string Board[8][9];
};
1
  • 2
    cout << Board[8][9]; uses two invalid indices. You probably mistyped cout << Board[i][j];. Commented Sep 14, 2016 at 0:19

2 Answers 2

2

it's so easy: you have the half solution: just use for loop to copy values

void A::init()
{
    string Board[8][9] = {
        { "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
        { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
        { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
        { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
        { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
        { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
        { " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
        { "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
    };

    for(int i(0); i < 8; i++)
    {
        for(int j(0); j < 9; j++)
            this->Board[i][j] = Board[i][j];        
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the init method you created a new, local variable. You are not referring to your instance variable, therefore in printing method the variable is not initialized.

Instead of

string Board[8][9] = // ... initialization code

you should write

this->Board = // ... initialization code

or just

Board = // ... initialization code

You can read more about variable types here.

6 Comments

The use of this is not required (it is implied). But if you are going to use it, it is a pointer not a reference.
@Peter true, too much Java lately (;
My condolences.
@Jezer Thank you, now I am getting the error: Expression must be a modifiable lvalue. Any tips to fix that?
You can't assign to arrays.
|

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.