Reading into a 2D array

Hey all, new here, I'm attempting to read a file into a 2d array and am getting rather stumped, whenever I run the soon to be posted code it compiles fine, however it gives me the error "Expression string subscript out of range" when attempting to display the 2D array. I'll post my code below, you can ignore the two middle functions.


#include <iostream>
#include <fstream>
#include <array>
#include <string>
//#include <vector>
using namespace std;

// Declarations.

int rows;
int columns;
int i;
int j;
char mazesize[][100];
char mazetext;
string s = " ";
string solve;
ifstream maze;

//Read the maze into an array and print the unsolved maze to the terminal.
void readMaze (char mazesize[][100], int &rows, int &columns){

//Open maze file to be used.
maze.open ("Maze 3.txt");

//Test to make sure file is open.
if (!maze){
cout << "Error opening file" << endl;
system ("pause");
maze.close();
}

//Read how many rows and columns are in the maze.
maze >> rows >> columns;

//Print number of rows and columns in the maze.
cout << endl;
cout << "Unsolved Maze:" << endl;
cout << "This maze has " << rows << " rows" << " and " << columns << " columns." << endl;

//Create maze with proper amount of rows and columns.
while (!maze.eof()){
//maze.get(mazetext);
for ( i = 0; i < rows; i ++){
getline (maze, s);
for ( j = 0; j < columns; j ++){
mazesize[i][j] = s [j];

}
}
//cout << mazetext;
cout << mazesize[i][j];
}
}

//Use an algorithm to solve the maze using the proper path.
void solveMaze (int &rows, int &columns){

}

//Print the solved maze onto the display.
void printMaze (int rows, int columns){






}


int main(){

char mazesize[100][100];
cout << "Enter 'Solve' to solve the maze" << endl;
cin >> solve;
if (solve == "Solve"){
readMaze(mazesize, rows, columns);
solveMaze(rows, columns);
printMaze(rows, columns);

}



maze.close();

return 0;
}


Any help is greatly appreciated.
Last edited on
hi, first of all you should put your code
in asuorce 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
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
#include <iostream>
#include <fstream>
#include <array>
#include <string>
//#include <vector>
using namespace std;

// Declarations.

int rows;
int columns;
int i;
int j;
char mazesize[][100];
char mazetext;
string s = " ";
string solve;
ifstream maze;

//Read the maze into an array and print the unsolved maze to the terminal.
void readMaze (char mazesize[][100], int &rows, int &columns){

//Open maze file to be used.
maze.open ("Maze 3.txt");

//Test to make sure file is open.
if (!maze){
cout << "Error opening file" << endl;
system ("pause");
maze.close();
}

//Read how many rows and columns are in the maze.
maze >> rows >> columns;

//Print number of rows and columns in the maze.
cout << endl;
cout << "Unsolved Maze:" << endl;
cout << "This maze has " << rows << " rows" << " and " << columns << " columns." << endl;

//Create maze with proper amount of rows and columns.
while (!maze.eof()){
//maze.get(mazetext);
for ( i = 0; i < rows; i ++){
getline (maze, s);
for ( j = 0; j < columns; j ++){
mazesize[i][j] = s [j];

}
}
//cout << mazetext;
cout << mazesize[i][j];
}
}

//Use an algorithm to solve the maze using the proper path.
void solveMaze (int &rows, int &columns){

}

//Print the solved maze onto the display.
void printMaze (int rows, int columns){






}


int main(){

char mazesize[100][100];
cout << "Enter 'Solve' to solve the maze" << endl;
cin >> solve;
if (solve == "Solve"){
readMaze(mazesize, rows, columns);
solveMaze(rows, columns);
printMaze(rows, columns);

}



maze.close();

return 0;
}
to be readable
can you please specifiy what is in the file text?!
and inwhich line is the error?!
Thank you for doing that.

The line is not specified, sorry it is a runtime error. I believe that is what it is called. and in the file is one line of text with two numbers and then a maze made up up stars.
Changing my declaration of mazesize from char mazesize[][100] to char mazesize[100][100] fixed the runtime error however it still does not print out the array to the terminal.
Last edited on
Anyone have any idea?
Even if it's a runtime error you can still track it down by stepping through the program with a debugger. And it's a little hard to read your program because it has no indentation.
Well I have fixed the runtime error by changing mazesize[][100] to mazesize[100][100] when declaring it. Now after some tinkering I get it to post the actual characters of the file, however it does not post the spaces or character returns, how do I change it to allow it to display those. I will repost my code below with proper indentation.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <array>
#include <string>
using namespace std;

// Declarations.

int rows;
int columns;
int i;
int j;
char mazesize[100][100];
char mazetext;
string s = " ";



//Read the maze into an array and print the unsolved maze to the terminal.
void readMaze (char mazesize[][100], int &rows, int &columns){

ifstream maze;

//Open maze file to be used.
maze.open ("Maze 3.txt");

//Test to make sure file is open.
if (!maze){
	cout << "Error opening file" << endl;
	system ("pause");
}

//Read how many rows and columns are in the maze.
maze >> rows >> columns;

//Print number of rows and columns in the maze.
cout << endl;
cout << "Unsolved Maze:" << endl;
cout << "This maze has " << rows << " rows" << " and " << columns << " columns." << endl;

//Create maze with proper amount of rows and columns.
while (!maze.eof()){
maze.get(mazetext);
	for ( i = 0; i < rows; i ++){
		getline (maze, s);
		for ( j = 0; j < columns; j ++){
			mazesize[i][j] = s [j];

				}
			}
//cout << mazetext;

		}

for ( i = 0; i < rows -1; i++){
	for (j = 0; j < columns - 1; j ++){
			cout << mazesize[i][j];
	}
}

maze.close();
}

//Use an algorithm to solve the maze using the proper path.
void solveMaze (int &rows, int &columns){



	
}

//Print the solved maze onto the display.
void printMaze (int rows, int columns){
	





}


int main(){

string solve;

char mazesize[100][100];
cout << "Enter 'Solve' to solve the maze" << endl;
cin >> solve;

while ( solve != "Exit"){
	if (solve == "Solve"){
		readMaze(mazesize, rows, columns);
		solveMaze(rows, columns);
		printMaze(rows, columns);

		return -1;
		}
	else if (solve != "Solve"){
		cout << endl;
		cout << "Invalid command, try again." << endl;;
		cin >> solve;
		cout << endl;
		}
	
	}

	if (solve == "Exit"){
		cout << "Goodbye" << endl;
		return -1;
	}

	return 0;
}


I know I have to change something in my first function, specifically my last set of for loops in order to make them take into account spaces and character returns.
Last edited on
Topic archived. No new replies allowed.