global char value assigning too much

Here is my issue, I'm trying to simply fill a global variable:
 
  char maze[13][13];

with chars 'O' and '#' O for cells and # for walls.
However here is a small tidbit of my code output:

12##############
0#############
1############
2###########
3##########
4#########
5########
6#######
7######
8#####
9####
10###
11##
12#

BTW it goes on 144 total times.

Here is my code:
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
#include <iostream> //for output
#include <stdlib.h> //for the rand() func
#include <time.h> //to add a new seed to srand()

using namespace std;
/*
Important Info:
O = Open Cell
V = Visited Cell
U = Used Cell
D = Destroyed Cell
-maze[x][y]
-cell[x][y]
*/


//Global Vars
char maze[13][13];

void createSMaze() { //create the starter maze so the program can generate a random one
	for (int x = 0; x < 13; ++x) {
		for (int y = 0; y < 13; ++y) {
			if ((x + 1) % 2 == 0 && (y + 1) % 2 == 0) { //if x and y are even insert open cell!
				maze[x][y] = 'O';
			}
			else {
				maze[x][y] = '#';
			}
		}
	}
}

void outputMaze() {
	for (int x = 0; x < 13; ++x) {
		for (int y = 0; y < 13; ++y) {
			cout << y; //debug purposes
			cout << &maze[x][y];
			cout << endl; //debug purposes
		}
		//cout << endl; //this should be the original end line
	}
}


int main()
{
	createSMaze();
	outputMaze();
	return 0;
}
Last edited on
cout << &maze[x][y]; <-- why are you doing cout of the address? :S
To clarify, this is also a good reason to quit using arrays and pass to vectors xD

Basically you think is a 2d array[13][13], in reality that is an abstraction because the compiler see that thing as a single array[169] (which is 13*13), and the square brackets are simply handling the logic to get the right thing.

But when you do cout << &maze[0][0] , you're basically telling to cout to print an array[169] that starts at at the address of maze[0][0].

And then when you have &maze[0][1] you simply interpretas an array[169] the memory starting 1 char further compared to the previous array, and this is the reason for the inverted triangle you're printing, probably the memory after your array is filled with '\0' which is the string terminating character for C strings therefore you got:
cout << "#####\0" << endl;
cout << "####\0\0" << endl;
cout << "###\0\0\0" << endl;
cout << "##\0\0\0\0" << endl;
cout << "#\0\0\0\0\0" << endl;

Do this:
1
2
3
4
5
6
7
8
void outputMaze() {
	for (int x = 0; x < 13; ++x) {
		for (int y = 0; y < 13; ++y) {
			cout << maze[x][y] << " ";
		}
		cout << endl;
	}
}


Better yet, use vector xD

1
2
3
4
5
6
for (auto& vec : maze){
	for (auto symbol : vec)	{
		cout << symbol << " ";
	}
	cout << endl;
}
Last edited on
Well I was having issues with assigning things.

Here is what i'm trying:
1
2
3
 vector<array<char,13>> maze; 
//to another part of the code
maze[x][y] = 'O';


However I get an error:
"vector subscript out of range"
you have told it the type, a vector containing array, still the actual object rapresented by 'maze' is empty.

You have to fill it with array<char,13> before you can index into them.

 
maze.push_back(array<char,13>());

now call maze.size() and it will say 1, meaning you can index into maze[0][from 0 to 12]

anyway mixing them seems kind of weird, why not just vector<vector<char>> ? you can still call maze[y][x] on it.
(yes, maze[y][x] , the first index is your row which is the "up/down" movement, so better to use y in it, second index is moving trough a given colum elements "left/right")

1
2
3
4
5
6
vector<vector<char>> maze(13, vector<char>(13, '#')); // call constructor, maze contain 13 vector<char>(with 13 '#') each
for(auto& row : maze)
    for(char& ch : row)
    {
        ch = // this loop will visit every char in the grid, place the logic to choose the symbol
    }
Last edited on
1
2
3
4
5
for(auto& row : maze)
    for(char& ch : row)
    {
        ch = // this loop will visit every char in the grid, place the logic to choose the symbol
    }

What does the 'auto' mean, and why do you have the pointer access after it, same for the char?
that is called "for range" loop, you can google it.

Auto means "deduce the type of the variable for me", so
for(auto& row : maze)
is the same as
for(vector<char>& row : maze)
The compiler knows what maze contains, so you don't need to tell it to him again, "auto" just do the right thing.

when you see that loop, you read the ':' as "in", so the loop above is read as
for (every vector<char>& in maze)

the & allows you to take a reference thus modify the original thing, otherwise "row" would have been a copy of the vector in maze.

Last edited on
Thank you very much.
Here is what I finally succeeded in outputing:

# # # # # # # # # # # # #
# O # O # O # O # O # O #
# # # # # # # # # # # # #
# O # O # O # O # O # O #
# # # # # # # # # # # # #
# O # O # O # O # O # O #
# # # # # # # # # # # # #
# O # O # O # O # O # O #
# # # # # # # # # # # # #
# O # O # O # O # O # O #
# # # # # # # # # # # # #
# O # O # O # O # O # O #
# # # # # # # # # # # # #



And here's my code if you want:
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
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

using namespace std;
/*
Important Info:
O = Open Cell
V = Visited Cell
U = Used Cell
D = Destroyed Cell
-maze[y][x]
-cell[y][x]
*/


//Global Vars
vector<vector<char>> maze(13, vector<char>(13, '#'));


void createSMaze() {
	for (int x = 0; x < 13; ++x) {
		for (int y = 0; y < 13; ++y) {
			if ((x + 1) % 2 == 0 && (y + 1) % 2 == 0) {
				maze[y][x] = 'O';
			}
			else {
				maze[y][x] = '#';
			}
		}
	}
}

void cleanMaze() {
	for (int x = 0; x < 13; x++) {
		for (int y = 0; y < 13; y++) {
			if (maze[x][y] == 'O') {
				cout << "There seems to be an open cell there may be an issue!" << endl;
				maze[x][y] = ' ';
			}
			else if (maze[x][y] == 'U') {
				maze[x][y] = ' ';
			}
			else if (maze[x][y] == 'D') {
				maze[x][y] = ' ';
			}
			else if (maze[x][y] == 'V') {
				maze[x][y] = ' ';
			}
		}
	}
}

void outputMaze() {
	for (int x = 0; x < 13; ++x) {
		for (int y = 0; y < 13; ++y) {
			cout << maze[y][x] << " ";
		}
		cout << endl;
	}
}


int main()
{
	createSMaze();
	outputMaze();
	return 0;
}

Last edited on
Topic archived. No new replies allowed.