This is the input that I am trying to load up the array with:
11 13
7 10
XXXXXXXXXXXXX
X JX AX X X
XXXX XXXXBX X
X X SX X X
X XXXX XXX X X
X X X X X
X XXXXXXXXX X
X X X X
XXXXXX XXXX X
XL X X
XXXXXXXDXXX X
the numbers at the top are to determine the width and length of the array and the the 7 and the 10 are where the D is located but that does not matter at this point that is for searching later on in the program.
Also for some reason I can't get the white space characters to show up but there should be an even line of X's down the right side.

#include <iostream>

using namespace std;

//int loadMaze(char [][14], int , int);
//int printMaze(char [][20]);

int main()
{
   int length = 0;
   int width = 0;
   int doorx;
   int doory;
   char maze[width][length];

   maze[length][width];

   cin>> length;
   cin >> width;
   cin>> doorx;
   cin>> doory;

   for(int i = 0; i < length; i++ )
   {
      for(int j = 0; j < width; j++ )
      {
         cin >> maze[j][i];
      }
   }
   for ( int i = 0; i < length; i++ )
   {
      for ( int j = 0; j < width; j++ )
      {
         cout << maze[j][i];
      }
      cout <<endl;
   }
}

Seemed to work for me, after fixing a few things. I just hard-coded the strings because I didn't want to type them every time I ran the program :)

XXXXXXXXXXX
X X     XLX
XJXXXXXXX X
XXX X X XXX
X  SXXXXX X
XAXXX X XXX
XXX  XXX  X
X XXX X X D
XXX XXX X X
X BXX X X X
XXX   X X X
X   X
X X   X X X
    X

Press any key to continue . . .
int main()
{
   const int length = 15;
   const int width = 11;
//   int doorx;
//   int doory;
   char maze[width][length] = {
"XXXXXXXXXXXXX",
"X JX AX X X",
"XXXX XXXXBX X",
"X X SX X X",
"X XXXX XXX X X",
"X X X X X",
"X XXXXXXXXX X",
"X X X X",
"XXXXXX XXXX X",
"XL X X",
"XXXXXXXDXXX X"
   };
#if 0
  maze[length][width];

   cin>> length;
   cin >> width;
   cin>> doorx;
   cin>> doory;

   for(int i = 0; i < length; i++ )
   {
      for(int j = 0; j < width; j++ )
      {
         cin >> maze[j][i];
      }
   }
#endif
   for ( int i = 0; i < length; i++ )
   {
      for ( int j = 0; j < width; j++ )
      {
         cout << maze[j][i];
      }
      cout <<endl;
   }
}

I'm trying to get it to work with I/O and I dont think it is loading the array correctly

If it's the input part that is giving you problems then why not just use getline() to input an entire line at a time. The >> operator will not allow you to input white space, but getline() will.

like this

cin.getline( maze[j]);

because I get a lot of problems when I try to compile that.

try this

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


int main()
{
   int length = 0;
   int width = 0;
   int doorx = 0;
   int doory = 0;

  char** maze = 0;

   cin>> length;
   cin >> width;
   cin>> doorx;
   cin>> doory;

   maze = new char*[length];
   for(int i = 0; i < length; ++i)
   {
       maze[i] = new char[width+1];
    // Here you type all the characters you want on
    // this line before hitting the <Return> key.
       cin.getline(maze[i], width);
   }
   for ( int i = 0; i < length; i++ )
   {
         cout << maze[i] << "\n";
   }
}

I tried that with I/O and it gave me this


XXXXXXXXXXXX

I'm trying to take in the data with the whitespace and then output the maze as shown on the input but with the correct spacing. Sorry if I'm frustrating you I'm not very familiar with two dimensional arrays or the getline function.

int main()
{
   const int length = 11;
   const int width = 14;
   int idx;
   char maze[length][255] = {0};

   for(idx = 0; idx < length; ++idx)
   {
       cin.getline(maze[idx], width+1, '\n');
   }
   for (idx = 0; idx < length; idx++ )
   {
         cout << maze[idx] << "\n";
   }
   cin.ignore();
}

Here is a more simplified version. The second parameter to getlin() is the line width which must be one more than the width of the maze to allow for the '\n' character. So if you want the width integer to be 14 then pass 15 to getline(). You can test that by typing one more character than you pass to getline(), the result is that getline() will not stop for keyboard input.

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