1

I was searching for a solution, but I couldn't use any without an error. Im trying to draw a map in console, using multidismentional array. I want to have more maps, but I can`t do it without spamming useless code. This is the map:

char map2[11][15] = {
"###^######^###",
"#L           #",
"^S           #",
"#S           #",
"#    K       #",
"###########  #",
"#G           #",
"#       #### #",
"#       #M   #",
"# @     #    #",
"## ########^##"};

and this is moving script:

void Game::showing_different_maps()
{
    differentmap= true;
    while (differentmap)
    {
        system("cls");
        for(int i = 0; i < 81; i++)  // i < map2[y][]
        {
            cout << somemap[i] << endl; // drawing a map !THIS CAUSES CRASH!
        }
    system("pause>nul"); // this line prevent lagging somehow

        if(GetAsyncKeyState(VK_UP))  // arrows to move on axis (y, x)
        {
            mapka.move(-1, 0);                    
        }
        if(GetAsyncKeyState(VK_DOWN))
        {
            mapka.move(1, 0);
        }
        if(GetAsyncKeyState(VK_RIGHT))
        {
            mapka.move(0, 1);
        }
        if(GetAsyncKeyState(VK_LEFT))
        {
            mapka.move(0, -1);
        }

    }

}

and here is what I could find in the internet.

  Wut wut(3, 13);
  Maps *wsk;
  pointer = &wut;

  char (*somemap)[81] = new char[81][81];
  somemap= &map2[81];

  pointer = &wut;
  pointer -> get_in_area();

thing I want to do is to: in some part of the code, I want the pointer to be map2. I managed to make pointer "pointer" to show void get_in_area() in class Maps, but Console crashes, when I want to draw the map with somemap pointer in second code sample. It works, when instead of somemap[i] I put map2[i]. I`m sorry if I missed anything, I'm beginner and English is not my first language. I made this script with this tutorial: https://www.youtube.com/watch?v=7gpH7bOS350

4
  • Beacause you are assuming the array bounds == 81? it will crash as soon as undefined behaviour desides it should crash Commented Jun 30, 2016 at 10:24
  • also in c++ try to avoid raw arrays as possible and wrap them inside a class or use one of the existing classes to let this fail in a nicer way with a clean exception Commented Jun 30, 2016 at 10:27
  • I tried everywhere to edit 81, but It never helped. maps will be different in size, and max size is 81x81, so I just wrote 81 everywhere. thanks for the advice, but code sample is in class, I just didn`t pasted whole code Commented Jun 30, 2016 at 10:27
  • wrap the map in a class where it is defined what the maps bounds are? Commented Jun 30, 2016 at 10:29

2 Answers 2

1

This is how you would create a 2-dimensional array:

typedef int T;
void f()
{

    T ** map = new T*[10];

    for (int i = 0; i < 10; i++)
    {
        map[i] = new T[10];
    }

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            map[i][j] = i*j;
        }
    }

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            std::cout << map[i][j] << "\t";
        }
        std::cout << std::endl;
    }
}

This will output a generic multiplication table.

If you want more dimensions you would simply add one more step in creation:

T*** map = new T**[10];

for (int i = 0; i < 10; i++)
{
    map[i] = new T*[10];
}
...
Sign up to request clarification or add additional context in comments.

Comments

0

You should learn how to pass parameters instead of using global (map2) or static (81) variables. If you declare:

void Game::showing_different_maps(int n, int m, char map[n][m])
{
 String   differentmap= true;
    while (differentmap)
    {
        system("cls");
        for(int i = 0; i < n; i++)  // i < map2[y][]
        {
            cout << somemap[i] << endl; // drawing a map !THIS CAUSES CRASH!
        }

you should no longer crash...

But anyway, for this usage, I would use a pointer to pointer instead of a 2d array:

char *map2[]= {
  "###^######^###",
  "#L           #",
  "^S           #",
  "#S           #",
  "#    K       #",
  "###########  #",
  "#G           #",
  "#       #### #",
  "#       #M   #",
  "# @     #    #",
  "## ########^##"};

And then

void Game::showing_different_maps(int n, char **map)

3 Comments

Thank you very much, I had some problems with understanding, so I could not understand previous help, but after quite some time I've managed to use pointer to pointer advice, but I've encountered another problem. Applications runs, but when the map is painted and I press a key, it crashes. Debugger told me only this: "Program received signal SIGSEGV, Segmentation fault." From the internet I understood only "you never allocate the space needed to store the variables" but I don't know what to do with it. I have added more code to my question. Thank you again for what you`ve done to this moment.
This should not be an edit to this question but a new question, because it is now a different problem and current answers are now unrelated to this new part.
oh, okay, I`ll make new question, nonetheless thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.