Generate and print 2d char array, problem.

Hello all! At first, thank you for this extremely useful webpage, I've learned here alot since I have began my adventure with c++, but it's the first time I have to ask my own question, quite desperate, unable to find the mistake which I surely "commited" here. I've been shuffling through youtube tutorials, google'ing but yet, failed to find the answer to my question, here it is:

I am trying to make a simple game with randomly generated map for my university basic C++ course, and though I have succeded in making a code for plain generation, I have a problem with map "refreshing" for which I have used the loop no.1 (in the code) which reading through each separate char works very slowly on larger maps.. the rest of description in the code...

My guess is that because loop 3. is working and 2. does not, my all mistake lays somewhere in the mapcreation() function, but I really don't know where :(

Forgive me if a similar question was already made somwhere here, but I haven't found such.

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
#include <iostream>
#include <windows.h>
#include <conio.h>

#define xMAX 10
#define yMAX 10

using namespace std;

void mapcreation(char map[][xMAX])
{
    for(int y=0; y<yMAX; y++)
    {
            for(int x=0; x<xMAX; x++)
            {
                    if(x==(xMAX-1)) map[x][y] = '\0';
                    else           map[x][y] = 'X';
            }
    }
}

char map2[10][20] = { "###################",
                      "#@                #",                    
                      "#                 #",
                      "#                 #",
                      "#                 #",
                      "#                 #",
                      "#                 #",
                      "###################" };

                     
int main()
{
    char map[yMAX][xMAX];
    mapcreation(map);
    
    // LOOP NO.1
    
    for(int y = 0; y < yMAX; y++) /* this one works, prints the basic 10x10 array from mapcreation(), 
    BUT is very slow when working on a larger map */
         {
               for(int x = 0; x < xMAX; x++)
               {
                 cout << map[x][y];
               } cout << endl;
         } 
    
    
    // LOOP NO.2
    
    for(int y = 0; y < yMAX; y++) /* theoretically much faster map-printing loop using the same 10x10 array from mapcreation(),
    supposed to print whole verse each loop, BUT does not work   */
    {
            cout << map[y] << endl;
    }
    
    // LOOP NO.3
        
    for(int y = 0; y < 10; y++) // THE SAME LOOP as in 2., WORKING, BUT using, not generated, premade char array
    {
            cout << map2[y] << endl;
    }
    
    getch();  
    return 0;  
}


Last edited on
It seems that in these statements of the function

if(x==(xMAX-1)) map[x][y] = '\0';
else map[x][y] = 'X';

you should exchange indexes, that is the code will look like

if(x==(xMAX-1)) map[y][x] = '\0';
else map[y][x] = 'X';
worked like a charm, thank you very much! that was really obvious... such mistakes are sometimes most perplexing :/
Topic archived. No new replies allowed.