Hi guys, I pretty new to c++ and I have a problem with getline. I am supposed to call in a text file into a 2d character array and the text file is a maze. Here is my code:

void maze::open()
{
	ifstream file;
	int row=0;
	file.open("standard.txt");
	while (!file.eof())
	{
		getline(file, store);

		for ( int col = 0 ; col < store.size() ; col++ )
		{
			Maze[row][col] = store[col];//store is a string variable
		}
		row++;
	}
	file.close();
}
void maze::printmaze()
{
	for(int i=0;i<50;i++)
	{
		for(int j=0;j<20;j++)
		{
			cout<<Maze[i][j];
		}

	}
	cout<<endl;
}

I have been getting weird outputs and I tried searching through this forum for similiar problems but i didn't find any. Please help me.

Recommended Answers

All 4 Replies

> for ( int col = 0 ; col < store.size() ; col++ )
Limit this by the size of the maze, not the size of the line.

> for(int i=0;i<50;i++)
Consider
const int mazeWidth = 50;
and use that constant in all the relevant places.

How did you declare maze anyway?

> while (!file.eof())
Use
while ( getline(file, store) )
which reads a line, or exits the loop.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351

Do you use the >> or any other input method in the program? Whenever you use more than one input method in a program you need to be aware of the status of the input buffer before each input.

It looks like you attempted to input the maze line by line, but you output the maze char by char. Was that intended?

To start a new line of char in the output section teh call to endl should be placed after the inner loop completes each sequence, not after the outer loop completes it's sequence.

I don't see "store" defined anywhere?

Dave

Hey guys I have solved the problem. It lies it the printing out of the maze. Thanks for the help anyways and also about the problems with file.eof.

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.