To store that type of information in an array I would use a 2D array of char. I would read in one char at a time, using a nested for loop where the outer loop controlled which row I was in and the inner loop controlled which column I was in. Then I could explicitly access any element within the array by using syntax such as myArrayofChar[row][col]. Remember that the indexes of an array are zero based, so the first row has index 0 and the 5 column has index 4, not five. If I knew ahead of time the number of rows and columns I'd declare the array using syntax, assuming it wasn't huge in size. Otherwise, I'd declare the array on the heap using dynamic memory with the keywords new[] and delete[] and loops as befitting the mulidimensional characterstics of the array.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
In the code you've posted I don't see where you initialize numrows to a value before you call the postfix increment operator on it. That would be a no-no.
For some other comments, see below.
//Block of code to find number of columns
////////////////////////////
string str;
//numrows should have a value of zero here
getline(infile, str);
//you somehow need to give numrows the value of one here
numcols=str.length();
infile.close();
////////////////////////////
//infile is already open here, don't reopen it.
infile.open("cavemap.txt");
//don't use eof() as the terminating condition for the while loop. If you want to know the technical reason why, ask. Otherwise just accept the advice. Use while(getline(infile, str)) instead and don't call getline() within the loop body.
while(!infile.eof()) //while-loop to count rows
{
//you really don't need another variable, just keep using str
string temp;
getline(infile, temp);
numrows++;
}
/*This loop will stop when EOF is found or something else causes infile to go into a
failed state. To be sure all of file was read you can check .eof(). If it is true, then you know you successfully read the whole file.
However, you want to reread the file now char by char. To do that you need
to get back to the beginning of the file. To do that I would call the istream clear() method on infile (because finding EOF put infile into a failed state) and then I would either use the istream seekg() method with the appropriate parameters or close infile and reopen it (when you reopen it it should default to the beginning of the file you associate it with). Now you can reread the file char by char to fill in multarray using the nested loop.
*/
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
In main() you have the following two lines:
cave caveobj;
caveobj.cavemap();
The first line calls the default constructor for the cave class. Since you don't explicitly declare/define a default constructor the compiler does it for you. The default default constructor will not initialize any data members for you. So numrows is left uninitialized at this time.
The second line calls the cavemap() method on the object called caveobj which is an instance of the cave class. However, numrows isn't initialized in that method either because there is no explicit initialization/assignment of a value before the postscript operator is called and there is no call to set_numrows() from within cavemap() that would set numrows to 1. Either use an initializer list in your own default constructor to set the value of numrows to the desired default value, OR explicitly assign a default value to numrows within cavemap() before you call the postscript operator, OR call set_numrows() within cavemap() before you call the postscript operator on numrows.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
When you clear() the stream it doesn't reposition the pointer to the beginning of the file. You can do that by using seekg() with the appropriate parameters, or by closing and reopening the same file.
Then, don't destroy the memory of the cave before you actually search the maze! Destroy the memory after you've found a solution, prove there is no solution, and do something with that knowledge. As it is now, when you successfully load the maze from the file to your program you won't be able to do anything with make_move(), because the maze will be gone already before you call make_move().
Maze searching can be a maze itself. The idea of using an array of neighbors and somehow keeping track of which ones have already been visited sounds logical. Then one approach would be to consider having a dequeue of cells representing the successfully visited cells (the "path" or "solution"). Each successfully visited cell would be pushed on the back of the dequeue. If a dead end was found then pop the current cell from the end stack to go "back" to the next available cell. In this scenario, stop searching if "goal" is found or if the dequeue is empty. IF "goal" found, then print the dequeue from begining to end to print a given solution. IF the dequeue was empty, then the maze has no solution. The number of steps from start to goal would be the size of the dequeue if you wanted to search the maze again to see if there was another, or shorter, solution.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396