Hello. I'm very new to Compuer Science, and I'm taking a class for fun but I recieved a really hard assignment for homework that I was hoping to get a little bit of help on here and there. It's only compter science for beginners, so I hope it isn't that hard. The assignment is making a program that can solve mazes.

My first problem so far is reading the maze file from the txt file I recieved in class.

The file looks like this:

30 30
**************
* ************
* *********
**** ********

The first number in the file is the number of rows that the maze has and the second number is the number of columns. The way I've broken down the task, I need to take this information off of the file into two int variables (row and cols) and one 2D array (maze[30][30])

From what I've learned about reading files, I came up with this:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char data[10000];
	int i = 0;

	ifstream mazefile;
	mazefile.open ("maze1.txt");
	if(mazefile.is_open())
	{
		while(!mazefile.eof())
		{
			mazefile >> data[i];
			i++;
		}
	}

	for(i=0; i<=10000; i++)
	{
	cout << data[i];
	}

	return 0;
}

This is about how far I've gotten in working to get this done, and so far, on the screen, I see 3030********************************* and then a bunch of jumbled up characters for the undeclared parts of the 1D array.

I believe I need to use the >> to get the two numbers at the start of the file, but then I need to use the "get" identifier to get the rest of the maze which is made up of asteriks and whitespaces which will be put in the 2D array.

Could someone please help me get on the right track? I've only really read from files on one other assignment and the online information gets a bit too complicated for me.

Recommended Answers

All 12 Replies

You are seeing all that garbage on the screen because the program is printing more array values than what was read from the file. In your example, 30 * 30 is 900, not 10,000, so the loop should stop at 900. You need to save the counter from line 17 so that you can use it as the maximum value in the loop beginning on line 21.

The next big problem with your program is that the entire loop starting at line 17 is wrong. the >> operator stops reading at the first space or at the '\n' character. I guess the lines in the data file can contain spaces since that's what you posted.

The array on line 7 is also wrong. You need a 2d array, ont a 1d array. char array[30][30]; or whatever sice you want to make it. Larger is ok, but too small is disasterous.

// declare array and set everything to 0
char array[50][80] = {0};
int i = 0;
int rows = 0;
int cols = 0;
// read number of rows and columns from the file
mazefile >> rows >> cols;
// read only the number of required rows.  Here we
// can ignore cols because the entire line is read in ad
// a slingle string.
while( i < rows && mazefile.getline( array[i], sizeof(array[i]) )
    ++i;

You are on the right track. Hopefully this can advance your progress somewhat.

#include <iostream>
#include <fstream>

using namespace std;

void printArray(char array[], int rows, int cols)
{
   for (int i = 0; i < rows; ++i)
   {
      for (int j = 0; j < cols; ++j)
      {
         cout << array[(i * cols) + j];
      }

      cout << endl;
   }
}


int main()
{
   ifstream mazefile("maze1.txt");

   //check file opened correctly
   if (! mazefile)
   {
      cout << "Error opening input file\n";
      exit(1);
   }

   int rows, cols;

   mazefile >> rows >> cols;

   //read and discard end-of-line
   mazefile.ignore(10, '\n');

   //allocate your array with proper dimensions
   char *data = new char[rows * cols];

   int i = 0;

   while(i < rows)
   {
      mazefile.getline(&data[i * rows], cols + 1, '\n');
      ++i;
   }

   printArray(data, rows, cols);

   delete[] data;

   return 0;
}

The way I've broken down the task, I need to take this information off of the file into two int variables (row and cols) and one 2D array (maze[30][30])

You have both missed one important piece of information -- according to the above you must use a 2d array. Both of you are using just 1d array.

Alright. Thanks a ton for your help. That part of the program works great now (I've got the 2D array all set now). My next trouble though is I need to make a function that will place periods on the solution to the maze. So, I need to make a function that will:

"Look right, then up, then left, then down, until you find an adjacent blank element to which to move. If you find such an element, put a period at the current element in the maze to mark the path and then move to the adjacent blank element next to the current one."

I believe I can handle that algorithm on my own (at least I have some ideas of how I could do that). But I don't understand how to use function notation properly for a function that you need to repeat over and over again until a certain condition. Could someone help me figure out how to make a skeleton for this repeated function? I really just don't understand how to use functions very well, looking around the web hasn't been too much help.

Even if someone could supply me to a link with a really good tutorial on functions, that'd be great! Thank you.

In order to make a function that repeats over and over again, you'll just need to loop it until a certain condition. In your case, until a space is found. You could accomplish this with a do/while or a while loop.

You can nest a loop inside of the main one to loop through the second dimension of the array and then move back out to the first array. The inside loop could be a for loop with the outside a do/while or do.

If you have questions on loops, just ask.

Thanks for the help, Amoebal, I think I can manage the loop. But how do you write a function which can relate position in the maze (via variable) and such like that?

Thanks for the help, Amoebal, I think I can manage the loop. But how do you write a function which can relate position in the maze (via variable) and such like that?

The program is really small enough that you could just put the loop in main. If you're required to create a function, or just want to for practice, you could just pass the entire array to the function and keep track of where you are while traversing it inside the function and return the new array with the '.'s instead of the spaces. Alternatively, you could pass the array to the function, and do the changing in the function, and also print the results there so no return is needed.

http://www.cplusplus.com/doc/tutorial/arrays.html has a tutorial on arrays with a section to pass them into functions. Here's their example program for reference:

// arrays as parameters
#include <iostream>
using namespace std;

void printarray (int arg[], int length) {
  for (int n=0; n<length; n++)
    cout << arg[n] << " ";
  cout << "\n";
}

int main ()
{
  int firstarray[] = {5, 10, 15};
  int secondarray[] = {2, 4, 6, 8, 10};
  printarray (firstarray,3);
  printarray (secondarray,5);
  return 0;
}

Ah, thank you very much. I'm getting there. My function is all set now. Thanks everyone.

I'm going to keep working on it and I'll post back if I hit another snag, I definitely want to try working on it a little more before I ask for help again.

Thanks again.

I have finally finished the program to solve mazes. However, I need to expand my program to the guidelines presented in the homework packet.

I'm currently having trouble allowing the user to enter their own data. The homework packet reads:

"The program should ask the user for the filename that contains the maze. Read the file name into a string variable. Remember that the argument to the open operator is a string, so you can use this variable as the argument for the open operator."

My program has the following included:

#include <string>

string filename;

cout << "Enter file name:" << endl;
cin >> filename;

ifstream mazefile;
mazefile.open (filename);

however, I keep getting an error that says something on the lines of "unable to convert parameter string to const char*" and then the program fails. Could someone please help me figure this out? I tried delcaring filename as a const char*, but that didn't seem to help much either.

Edit:

I edited it a bit so it looks like this:

mazefile.open (filename.c_str())

Is that okay? It works for the program.

try

mazefile.open(filename.c_str());

Awesome! I wish I knew all of these functions. My next trouble is that I'm trying to combine two strings and a char (which could be a string) into one.

string word1;
string word2;
string bigword;

cout << "Enter word1:" << endl;
cin >> word1;

cout << "Enter word2:" << endl;
cin >> word2;

bigword = word1 & "_" & word2; (this doesn't work)

cout << bigword;

The idea is that if word1 = "cherry" and word2 = "pie" then bigword = "cherry_pie". Is there a function I could use for that. Thanks for all of help again. I don't know how long this would take me without the help.

try

bigword = word1 + "_" + word2;

I'm fairly certain it will work. I'm a little rusty in my C++ syntax these days.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.