Hello All,

I seem to be having a relatively small problem with a Maze program I'm working on. Basically, I read a txt file containing a maze into a 12 x 12 2D array, but the problem is when I try to display it, none of the white spaces that are in the maze show up. I know it has something to do with the getline() function, but I have no clue as to where to put it.

Here is what an example of a txt file used contains:

~~~~~~~~~~~~
~ =   ?   *~
~ = ^ ? ^ *~
~ = ^ ? ^ *~
~   ^   ^ =~
~***^~~~^ =~
~         =~
~ ===???^^^~
~         ^~
~~  ~~    ^~
~     ~   $~
~~~~~~~~~~~~

Here is my code so far:

#include "Maze.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
 
using namespace std;
 
Maze::Maze()
{
   mazeStore[12][12] = ' ';
}

int Maze::inputMaze()
{
   string fileName;
   char ch;


   cout << "Enter the file name: ";
   cin >> fileName;

   ifstream inFile;
   inFile.open(fileName.c_str());

   if (!inFile)
   {
      cout << "File could not be opened." << endl;
      exit(1);
   }
   else
   {
      while (!inFile.eof())
      {
         for (int i = 0; i < 12; i++)
         {
            for (int j = 0; j < 12; j++)
            {
               inFile >> mazeStore[i][j];         
            }
         }
      }
      inFile.close();
   }   
}

void Maze::outputMaze()
{
   cout << "MAZE:\n";
   cout << "          111\n";
   cout << " 123456789012\n"; 
    
   for(int a = 0; a < 12; a++)
   {
      for(int b = 0; b < 12; b++)
      {         
         cout << mazeStore[a][b];
      }
      cout << endl;
   }
   return;
}

Thanks in advance for any help given!

Edited 6 Years Ago by Menathradon: n/a

Hello All,

I seem to be having a relatively small problem with a Maze program I'm working on. Basically, I read a txt file containing a maze into a 12 x 12 2D array, but the problem is when I try to display it, none of the white spaces that are in the maze show up. I know it has something to do with the getline() function, but I have no clue as to where to put it.!

What getline() function? You have no getline() function.

Or do you mean you want to use it but don't.... OK, I guess that makes sense.

Let's see, you have input which doesn't work. You think getline() will make it work. How about where you do the input right now? That would probably be a good place in my opinion.

What getline() function? You have no getline() function.

Or do you mean you want to use it but don't.... OK, I guess that makes sense.

Let's see, you have input which doesn't work. You think getline() will make it work. How about where you do the input right now? That would probably be a good place in my opinion.

Yes, I want to use the getline() function, considering it reads the white spaces I need. But I'm lost on how to use it and where to put it at.

I've tried messing around with it. But every time I use it the program doesn't run correctly. :sad:

But I'm lost on how to use it

Look it up in a reference book, a tutorial, google, whatever. If you want to use something, you need to find out how. That's extremely simple.

and where to put it at.

What did I suggest? Do I need to be clearer?

I've tried messing around with it. But every time I use it the program doesn't run correctly. :sad:

We have no idea what you tried, so we can't suggest a correction. No info, no fix.

Ok, this was one of my attempts....

#include "Maze.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
 
using namespace std;
 
Maze::Maze()
{
   mazeStore[12][12] = ' ';
}

int Maze::inputMaze()
{
   string fileName;
   char ch[12];


   cout << "Enter the file name: ";
   cin >> fileName;

   ifstream inFile;
   inFile.open(fileName.c_str());

   if (!inFile)
   {
      cout << "File could not be opened." << endl;
      exit(1);
   }
   else
   {
      while (!inFile.eof())
      {
         for (int i = 0; i < 12; i++)
         {
            for (int j = 0; j < 12; j++)
            {
               inFile.getline(ch,12);
               ch[0] = mazeStore[i][j];         
            }
         }
      }
      inFile.close();
   }   
}

void Maze::outputMaze()
{
   cout << "MAZE:\n";
   cout << "          111\n";
   cout << " 123456789012\n"; 
    
   for(int a = 0; a < 12; a++)
   {
      for(int b = 0; b < 12; b++)
      {         
         cout << mazeStore[a][b];
      }
      cout << endl;
   }
   return;
}

As you can see I put the inFile.getline(ch,12); in the nested for loop. But this still isn't right.
Any suggestions?

Do you understand the difference between inFile >> mazeStore[i][j]; and inFile.getline(ch,12); :icon_question:
Is one a perfect replacement for the other? In other words, do they do exactly the same thing? If not, why did you simply replace one with the other?

I am a huge believer the that stuff between the ears needs to be used. If all you're going to do is blindly try any thing in the hopes you'll hit on the magic code, forget it. You need to use your brain to program. What does the statement do? How is it best used? How does using something else change the code needed around it?

Original code:

for (int i = 0; i < 12; i++)
         {
            for (int j = 0; j < 12; j++)
            {
               inFile >> mazeStore[i][j];         
            }
         }

This uses 2 loops to read a single mazeStore entry 144 times -- whatever mazeStore is.

You change it to read an entire line:

for (int i = 0; i < 12; i++)
         {
            for (int j = 0; j < 12; j++)
            {
               inFile.getline(ch,12);
               ch[0] = mazeStore[i][j];         
            }
         }

which reads a line 144 times. And after each read moves one unknown character into ch[0] -- which is the line just read.

This article has been dead for over six months. Start a new discussion instead.