Hello. I am creating a program to read in a maze and solve it but when I read in the maze file into a dynamically allocated array using get I notice it reads the newline which messes up where everything is placed in the array and I'm not sure how to avoid this issue. Any Ideas?
1 2 3 4 5 6 7 8 9 10 11 12
char** mazechars = newchar*[temprow+1];
for(int i = 0; i < temprow; i++){
mazechars[i] = newchar[tempcol+1];
}
//then this reads in the symbols
ifstream maze(filename);
for(int i = 0; i < temprow; i++){
for(int j = 0; j <= tempcol+1; j++){
maze.get(mazechars[i][j]);
cout << mazechars[i][j] << " " << i << " " << j << endl;
}
}
I was trying to debug and I saw this is what it printed out which is wrong
std::istream::get() is an unformatted input function; it reads in all characters including white space characters. A formatted input function (operator>>) would skip leading white space characters.
You should use getline() instead of get().
Below a simple example showing how to manage strings in a file, but it means that your file must respect maze size ++