Why cant I pass this array to the function?

I am making a small console game and I made the map, and an working on putting things into functions and classes buit this wont work for some reason, i get this error:

C:\Users\thund_000\Desktop\String stuff\main.cpp|20|error: cannot convert 'char (*)[10]' to 'char (*)[20]' for argument '4' to 'void GameMap(int, int, char, char (*)[20])'|

But i have no idea what it means.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;

void GameMap(const int rows, const int columns, const char mapChar, char g_map[][20]);

int main()
{
    const int rows = 10;
    const int columns = 20;
    int plrPosX = 0, plrPosY = 0;
    const char mapChar = '.';
    string direction = "";

    char g_map[columns][rows];

    bool loopEnd = false;

    GameMap(rows, columns, mapChar, g_map);

    while(!loopEnd)
    {
        cout << "Which direction would you like to go?" << endl;

    }

    return 0;
}

void GameMap(const int rows, const int columns, const char mapChar, char g_map[][20])
{
    for(int r = 0; r < rows; r++)
    {
        for(int c = 0; c < columns; c++)
        {
            g_map[r][c] = mapChar;
            cout << g_map[r][c];
        }
        cout << endl;
    }
}
@Ch1156

You have the g_map declared backwards. It should be char g_map[rows][columns];
ah, i switched them around to test something and forgot. thanks.
ok, now i am trying to change my arrays to vectors for more flexibility and dynamic map sizing, but my program doesnt display the text in the loop, it just displays the map when its first called, and wont do anything else. When i try to exit the program it hangs for a second. what is wrong?

EDIT: Ok i just figured out that it was because the map needs to be 20x20 not 10x20, but what if i want it 10x20? what do i do? It works just fone when i make the rows and columns 20x20.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void DrawMap(int rows, int columns, char mapChar, vector<vector<char> > gameMap, char player, int plrPosX, int plrPosY);
void InputMovement(int& plrPosX, int& plrPosY, int& direction);
void GameDetails(string& gameName);

int main()
{
    const int rows = 10;
    const int columns = 20;
    int plrPosX = 3, plrPosY = 3;
    char mapChar = '.';
    char player = '#';
    int direction = 0;

    //char g_map[rows][columns];

    vector<vector<char> > gameMap(rows, vector<char>(rows));

    bool loopEnd = false;

    DrawMap(rows, columns, mapChar, gameMap, player, plrPosX, plrPosY);

    while(!loopEnd)
    {
        cout << "\nWhich direction would you like to go?" << endl;
        cout << "1) Up" << endl;
        cout << "2) Down" << endl;
        cout << "3) Left" << endl;
        cout << "4) Right" << endl;
        cout << ">";
        cin >> direction;

        InputMovement(plrPosX, plrPosY, direction);

        DrawMap(rows, columns, mapChar, gameMap, player, plrPosX, plrPosY);
    }

    return 0;
}

void DrawMap(int rows, int columns, char mapChar, vector<vector<char> > gameMap, char player, int plrPosX, int plrPosY)
{
    for(int r = 0; r < rows; r++)
    {
        for(int c = 0; c < columns; c++)
        {
            gameMap[r][c] = mapChar;
            gameMap[plrPosX][plrPosY] = player;
            cout << gameMap[r][c];
        }
        cout << endl;
    }
}

void InputMovement(int& plrPosX, int& plrPosY, int& direction)
{
    if(direction == 1)
    {
        plrPosX -= 1;
    }
    if(direction == 2)
    {
        plrPosX += 1;
    }
    if(direction == 3)
    {
        plrPosY -= 1;
    }
    if(direction == 4)
    {
        plrPosY += 1;
    }
}

//Dev use only. Used to create maps in the game world.

void MapCreator()
{

}

void GameDetails(string& gameName)
{
    cout << "Enter a name for your game(Can be changed later)" << endl;
    getline(cin, gameName);
}

void MapDetails()
{

}
Last edited on
bump still cant figure it out.
You don't even need any array to print that. This does everything that your DrawMap does:
1
2
3
4
5
6
7
8
9
10
11
12
void DrawMap( int rows, int columns, char mapChar, char player, int plrPosX, int plrPosY )
{
    for( int r = 0; r < rows; ++r )
    {
        for( int c = 0; c < columns; ++c )
        {
            if ( r == plrPosX && c == plrPosY ) std::cout << player;
            else std::cout << mapChar;
        }
        std::cout << '\n';
    }
}


When you do have a std::vector, you do have std::vector::size() member function. Why pass some unrelated "rows" or "columns" values, if the intention is to iterate the entire vector?
1
2
3
4
5
6
7
8
9
10
11
void foo( const std::vector<std::vector<char>> & bar )
{
    for ( const auto & row : bar )
    {
        for ( auto dot : row )
        {
            std::cout << dot;
        }
        std::cout << '\n';
    }
}

but what if the size is different for x and y? like i stated before? what if i want a vector with 30 rows and 80 columns? i tried it and the thing crashed, what do i do then?

EDIT: Actually i think i figured it out, maybe, i had:

vector<vector<char> > gameMap(rows, vector<char>(rows));

and changed it to:

vector<vector<char> > gameMap(rows, vector<char>(columns));

It doent seem to crash anymore.
Last edited on
BTW

1
2
3
4
5
6
7
8
9
10
11
12
void DrawMap( int rows, int columns, char mapChar, char player, int plrPosX, int plrPosY )
{
    for( int r = 0; r < rows; ++r )
    {
        for( int c = 0; c < columns; ++c )
        {
            if ( r == plrPosX && c == plrPosY ) std::cout << player;
            else std::cout << mapChar;
        }
        std::cout << '\n';
    }
}


How does this output the map? Im confused by this.
How does this output the map?

It does produce exactly the same output as your DrawMap() does. The question is thus: what is your version actually doing?
Topic archived. No new replies allowed.