I'm having trouble getting my dynamic 2d array to work consistantly in my program.

Here's the code:

#include <iostream>
#include <string>
#include "DisjSet.h"
using namespace std;

struct cell
{
	bool nWall;
	bool sWall;
	bool wWall;
	bool eWall;
};

void CreateMaze(cell** &maze, int x, int y);
void LoadMaze(cell** &maze, int x, int y);

int main()
{
	int x = 0, y = 0, totCells = 0;
	cell **maze = 0;
	
	cout << "Enter the maze's dimensions separated by a comma. " << endl;
	cout << "Format x,y: ";

	cin >> x; 
	cin.get();
	cin	>> y;

	cout << endl;

    totCells = x * y;

	DisjSet<char> djSet(totCells);

    CreateMaze(maze, x, y);
	LoadMaze(maze, x, y);
	
	cout << endl << "cout << maze[][].*Wall; in main()" << endl;
	cout << maze[x][y].nWall << endl;
	system("pause");

	return 0;
}

void CreateMaze(cell** &maze, int x, int y)
{
	maze = new cell*[y];

	for (int i = 0; i < y; i++)
		maze[i] = new cell[x];
}

void LoadMaze(cell** &maze, int x, int y)
{
	cout << "cout << maze[][].*Wall; in LoadMaze()" << endl << endl;
	for (int i = 0; i < y; i++)
	{
		for (int j = 0; j < x; j++)
		{
			cout << i << "," << j << " stores ";
			maze[i][j].nWall = true;
			maze[i][j].sWall = true;
			maze[i][j].wWall = true;
			maze[i][j].eWall = true;
			cout << maze[i][j].nWall << " " << maze[i][j].sWall << " " << maze[i][j].wWall << " " << maze[i][j].eWall << " " << endl;
		}
	}
}

Here's the output:
Enter the maze's dimensions separated by a comma.
Format x,y: 3,5

cout << maze[][].*Wall; in LoadMaze()

0,0 stores 1 1 1 1
0,1 stores 1 1 1 1
0,2 stores 1 1 1 1
1,0 stores 1 1 1 1
1,1 stores 1 1 1 1
1,2 stores 1 1 1 1
2,0 stores 1 1 1 1
2,1 stores 1 1 1 1
2,2 stores 1 1 1 1
3,0 stores 1 1 1 1
3,1 stores 1 1 1 1
3,2 stores 1 1 1 1
4,0 stores 1 1 1 1
4,1 stores 1 1 1 1
4,2 stores 1 1 1 1

cout << maze[][].*Wall; in LoadMaze()
171
Press any key to continue...

Why is it giving me '171'? Thanks!

I test ran your code and got the following output:

Enter the maze's dimensions separated by a comma.
Format x,y: 3
5

cout << maze[][].*Wall; in LoadMaze()

0,0 stores 1 1 1 1
0,1 stores 1 1 1 1
0,2 stores 1 1 1 1
1,0 stores 1 1 1 1
1,1 stores 1 1 1 1
1,2 stores 1 1 1 1
2,0 stores 1 1 1 1
2,1 stores 1 1 1 1
2,2 stores 1 1 1 1
3,0 stores 1 1 1 1
3,1 stores 1 1 1 1
3,2 stores 1 1 1 1
4,0 stores 1 1 1 1
4,1 stores 1 1 1 1
4,2 stores 1 1 1 1

cout << maze[][].*Wall; in main()
1

........................................................
I am not sure of why u r getting that 171 in your output...........

I test ran your code and got the following output:
cout << maze[][].*Wall; in main()
1

........................................................
I am not sure of why u r getting that 171 in your output...........

What compiler did you use? I'm using Microsoft Visual Studio 2008 and Dev C++.
When I run it in VCC, I get 171, and what's even weirder is that when I run it in Dev I get 72?!

Oh, and using the sun compiler on Linux the output is 0.

Here's the solution. Thanks for the reply.

struct cell
{
     cell()
     {
             nWall = true;
             sWall = true;
             wWall = true;
             eWall = true;
     }

     bool nWall;
     bool sWall;
     bool wWall;
     bool eWall;
};

i triend the program in linux.
May be the reason for getting different values (171, etc.) is depending upon the compiler the boll varibales have default value initiated if not initialised explicitly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.