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.
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];
#include <iostream>
#include <fstream>
#include <array>
#include <string>
//#include <vector>
usingnamespace 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
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.
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.
#include <iostream>
#include <fstream>
#include <array>
#include <string>
usingnamespace 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;
}
elseif (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.