Hi everyone. Little new here but hope for everyone's help =). I am doing a maze for my Computer Science class. My program needs to read the maze from a .txt file and print the solution after. Im only in beginning stages but that's alright. The problem I'm having is reading the maze into a 2 dim array. I put cout statements to show the result I'm getting....crazy. What is working is the passing of reference variables between functions as requested in the HW I believe once we get this fixed the rest should flow. I've tried everything....from getline to get char. Not sure what I'm missing. Please let me know.

// Maze program ver 1.0
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;

//Read Maze function
void readMaze(string maze1[][100],int &rows, int &cols)

// Reads in the maze file.
ifstream maze_in("maze1.txt");
int i, j;
char ch, test;
string line;

    if(!maze_in.is_open()) {
        cout << "Can not open file..." << endl;
maze_in >> rows;
maze_in >> cols;
//for (i=0; i<=rows; i++) {
	// Added to test first 2 rows
for (i=0; i<=2; i++) {
	for (j=0; j<=cols; j++) {
	 maze_in.get(ch) >> maze1[i][j];
	 // Why does get(ch) produce stupid results
	 // Used to debug where maze chars are in the 2 dim array
	cout << "r=" << i << " c=" << j << " value=" << maze1[i][j] << endl;
	// Used to debug as well
cout << endl;
	cout << "rows = " << rows << endl;
	cout << "cols = " << cols << endl;


int main()

// Original Maze
string mazeIn[100][100];

// Solved Maze
string mazeOut[100][100];

//Maze size
int rows, cols;

//Loop variables
int i, j;


// Call Read Maze function
readMaze(mazeIn,rows, cols);

	return 0;
9 Years
Discussion Span
Last Post by nucleon

get() will also add the newlines to your maze as well.
Read the char, then test it to see if it's a valid "maze" char, and then add it to your array.


You have maze_in.get(ch) >> maze1[i][j]; It should be more like maze_in.get(ch); maze1[i][j] = ch; or maze_in.get(maze1[i][j]); Also, maze1 may be better as type char instead of string.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.