Reading ints from a Text File with a 2D Array

reading in a maze into a 2D array. The first two reads will give me the dimension of the maze(ex. m x n maze). So in order to create the 2D array i need the first two reads. Then after it is created it will read the rest of the data which are 1s and 0s. I have to create a program that will solve the maze but i cant test my movement code if i cant read in the data first. the entire program compiles but to test if i read the file i have a function to print it. But it says "There are: 0 rows and 0 columns " so it didn't read anything since rCount and cCount are initialized to 0. and basically the maze has nothing in it.

[code]
#include <iostream>
#include <fstream>
#include <stack>
#include <cstdlib>

#include "position.h"
#include "outputhelp.h"

using namespace std;
using namespace p3_maze;

ifstream mf;//--------------------------// The input file
int rCount = 0; // Reads the amount of Rows
int cCount = 0;//-----------------------// Reads the amount of columns
int **maze;//---------------------// the 2D array that will contain the maze

int main()
{
mf.open(argv[1]);// Second maze: the Smallest
mf >> rCount;//will read the number of rows from the file
mf >> cCount;//will read the number of columns from the file
//The maze will b created
maze = new int *[rCount];
for(int i = 0; i < rCount; i++)
{
maze[i] = new int[cCount];
}
//Filling in the maze in proper position from left to right
// row by row
for(int x = 0; x < rCount; x++)
{
for(int y = 0; y < cCount; y++)
{
mf >> number;
maze[x][y] = number;
}
}

mf.close();
}
Last edited on
@jaymez

I was able to get your program working, kind of. Don't have the last two header files, and I hard coded in the text file name. Added in some code to zero out the new array, printed out what was created etc. Didn't use DELETE command before ending program, etc. But, this works.

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
#include <iostream>
#include <fstream>
#include <stack>
#include <cstdlib>

//#include "position.h"
//#include "outputhelp.h"

using namespace std;
//using namespace p3_maze;

ifstream mf;//--------------------------// The input file
int rCount = 0; // Reads the amount of Rows
int cCount = 0;//-----------------------// Reads the amount of columns
//int **maze;//---------------------// the 2D array that will contain the maze

int main()
{
 int number;
 mf.open("MyMaze.txt");// Second maze: the Smallest
 mf >> rCount;//will read the number of rows from the file
 mf >> cCount;//will read the number of columns from the file
 //The maze will b created
 int **maze = new int *[rCount];
 for(int i = 0; i < rCount; i++)
 {
	maze[i] = new int[cCount];
 }

 for(int x = 0; x < rCount; x++)
 {
	for(int y = 0; y < cCount; y++)
	{
	 maze[x][y] = 0; // Fill with zeroes
	}
 }
 //Filling in the maze in proper position from left to right
 // row by row
 for(int x = 0; x < rCount; x++)
 {
	for(int y = 0; y < cCount; y++)
	{
	 mf >> maze[x][y];
	}
 }

 for(int x = 0; x < rCount; x++)
 {
	for(int y = 0; y < cCount; y++)
	{
	 cout << maze[x][y] << " ";// Show it's filled

	}
	cout << endl;
 }
 mf.close();
}
The two headers are part of the rest of the program and i have a print function which showed the dimension. So im guessing because i didn't zero it out it couldn't accept anything in it. But you are right about the delete, i forgot that. Thanks for the help. i might have problems with the rest of the maze movements so look out for a next post if you think you can help. Thanks again
@jaymez

So im guessing because i didn't zero it out it couldn't accept anything in it


No, that's not right. I zeroed it out because I didn't create the text file with all the 1's and 0's in it, and so was printing out garbage. So, I just zeroed everything to make the output look better. I deleted that section after making a larger text file.
Last edited on
Topic archived. No new replies allowed.