So close (I think)! I've been working on this for some time now (on and off, coming back to it) and now I'm stuck. Hopefully there's not to much wrong with the coding of it, the compiler throws up lines 50 & 63 with errors?
Not to sure if I've got the array right [9][10][3], which way do these run?

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
   int direction;
   int location, old_location;

   int map[9][10][3] =
      {
      {
      {
     //  0,  1,  2,  3,  4,  5,  6,  7,  8,  9

       {99,  9,  3, 99, 99, 99,  1, 99, 99, 99},  // 0
       {99, 10,  4, 99,  0, 99,  2, 99, 99, 99},  // 1
       {99, 11,  5, 99,  1, 99, 99, 99, 99, 99},  // 2
       {99, 12,  6, 99, 99, 99,  4, 99,  0, 99},  // 3
       {99, 13,  7, 99,  3, 99,  5, 99,  1, 99},  // 4
       {99, 14,  8, 99,  4, 99, 99, 99,  2, 99},  // 5
       {99, 15, 99, 99, 99, 99,  7, 99,  3, 99},  // 6
       {99, 16, 99, 99,  6, 99,  8, 99,  4, 99},  // 7
       {99, 17, 99, 99,  7, 99,  9, 99,  5, 99}  // 8
      },
      {
       {99, 18, 12,  0, 99, 99, 10, 99, 99, 99},   // 9
       {99, 19, 13,  1,  9, 99, 11, 99, 99, 99},  // 10
       {99, 20, 14,  2, 10, 99, 99, 99, 99, 99},  // 11
       {99, 21, 15,  3, 99, 99, 13, 99,  9, 99},  // 12
       {99, 22, 16,  4, 12, 99, 14, 99, 10, 99},  // 13
       {99, 23, 17,  5, 13, 99, 99, 99, 11, 99},  // 14
       {99, 24, 99,  6, 99, 99, 16, 99, 12, 99},  // 15
       {99, 25, 99,  7, 15, 99, 17, 99, 13, 99},  // 16
       {99, 26, 99,  8, 16, 99, 99, 99, 14, 99}  // 17
      },     
      {     
       {99, 99, 21,  9, 99, 99, 19, 99, 99, 99},  // 18
       {99, 99, 22, 10, 18, 99, 20, 99, 99, 99},  // 19
       {99, 99, 23, 11, 19, 99, 99, 99, 99, 99},  // 20
       {99, 99, 24, 12, 99, 99, 22, 99, 18, 99},  // 21
       {99, 99, 25, 13, 21, 99, 23, 99, 19, 99},  // 22
       {99, 99, 26, 14, 22, 99, 99, 99, 20, 99},  // 23
       {99, 99, 99, 15, 99, 99, 25, 99, 21, 99},  // 24
       {99, 99, 99, 16, 24, 99, 26, 99, 22, 99},  // 25
       {99, 99, 99, 17, 25, 99, 99, 99, 23, 99}  // 26
       }
       }
       };                                          //compiler throws up  initializer

   location = 0;
   old_location = location;
   cout << "You awake to find yourself locked in a grid of rooms" << endl;
   cout << "\n" << "Directions of travel are 2 for N, 6 for E, 8 for South, 4 for West, 1 for UP, 3 for DOWN" << "\n" << endl;
   cout << "starting location = room " <<  location << "\n" << endl;
   cout << "Enter your direction to travel?" << endl;

   while (location !=26)
   {
      cin >> direction;

      location = map[location][direction];         //compiler throws up as invalid

      if(location == 99)
      {
         cout<<"Incorrect move...!!!... Enter direction again."<<endl;
         location = old_location;
      }
      else
      {
         cout << "Your at location " << location << endl;
         old_location = location;

         if (location == 26)
         cout << "You've found the exit" << endl;

      }
   }
   return 0;
}

As always, your help is well appreciated

Leppie

You have 4 {, every dimension of array requires a { so you have 1 too many.

You inner most array initialisers have 10 entries, but the last dimension of you array has a size of 3. The sizes do not match.

Looking how you have laid out the data I suspect you should have declared int map[3][9][10] or of course fix the initialiser.

Thanks Banfa, removed the extra brace (whoops) and redeclared the array as you suggested, (thought I might have got that wrong!) Line 63 is still invalid though and I can't see why?

location = map[location][direction];

This line works perfectly fine on the same, as a 2D array, any suggestions why it won't work here going up to a 3D array? Map now equals a 3D array and so location is within its boundaries and direction is still just that, direction? Can't see why it wont work?

You only have 2 array dimensions, what is the type of location , what is the type of map[location][direction]

All are of type int.

Nope because map is declared as int map[3][9][10] , map[1][1][1] has type int so map[1][1] must surely be something different.

Do you not get an error similar to
error: invalid conversion from 'int*' to 'int'
when you compile it. Surely that gives you a clue to the type of map[1][1] ?

Nope it stops compiling at line 61 (previously line 63) location = map[location][direction]; throwing up invalid and stopping the compile. Origanally this started off as a 2D array with a big if else statement covering direction, someone on C++ forum gave me the code as an alternative to the if else's. I played around with it, then tried to implement what I'd learnt into this 3D array. So no, everythings declared as an int.

int main()
{
   int direction;
   int location, old_location;

   int map[3][9][10] =

Here's the whole code below if it helps

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
   int direction;
   int location, old_location;

   int map[3][9][10] =
      {
      {
     //  0,  1,  2,  3,  4,  5,  6,  7,  8,  9

       {99,  9,  3, 99, 99, 99,  1, 99, 99, 99},  // 0
       {99, 10,  4, 99,  0, 99,  2, 99, 99, 99},  // 1
       {99, 11,  5, 99,  1, 99, 99, 99, 99, 99},  // 2
       {99, 12,  6, 99, 99, 99,  4, 99,  0, 99},  // 3
       {99, 13,  7, 99,  3, 99,  5, 99,  1, 99},  // 4
       {99, 14,  8, 99,  4, 99, 99, 99,  2, 99},  // 5
       {99, 15, 99, 99, 99, 99,  7, 99,  3, 99},  // 6
       {99, 16, 99, 99,  6, 99,  8, 99,  4, 99},  // 7
       {99, 17, 99, 99,  7, 99,  9, 99,  5, 99}   // 8
      },
      {
       {99, 18, 12,  0, 99, 99, 10, 99, 99, 99},   // 9
       {99, 19, 13,  1,  9, 99, 11, 99, 99, 99},  // 10
       {99, 20, 14,  2, 10, 99, 99, 99, 99, 99},  // 11
       {99, 21, 15,  3, 99, 99, 13, 99,  9, 99},  // 12
       {99, 22, 16,  4, 12, 99, 14, 99, 10, 99},  // 13
       {99, 23, 17,  5, 13, 99, 99, 99, 11, 99},  // 14
       {99, 24, 99,  6, 99, 99, 16, 99, 12, 99},  // 15
       {99, 25, 99,  7, 15, 99, 17, 99, 13, 99},  // 16
       {99, 26, 99,  8, 16, 99, 99, 99, 14, 99}   // 17
      },     
      {     
       {99, 99, 21,  9, 99, 99, 19, 99, 99, 99},  // 18
       {99, 99, 22, 10, 18, 99, 20, 99, 99, 99},  // 19
       {99, 99, 23, 11, 19, 99, 99, 99, 99, 99},  // 20
       {99, 99, 24, 12, 99, 99, 22, 99, 18, 99},  // 21
       {99, 99, 25, 13, 21, 99, 23, 99, 19, 99},  // 22
       {99, 99, 26, 14, 22, 99, 99, 99, 20, 99},  // 23
       {99, 99, 99, 15, 99, 99, 25, 99, 21, 99},  // 24
       {99, 99, 99, 16, 24, 99, 26, 99, 22, 99},  // 25
       {99, 99, 99, 17, 25, 99, 99, 99, 23, 99}   // 26
       }
       };

   location = 0;
   old_location = location;
   cout << "You awake to find yourself locked in a grid of rooms" << endl;
   cout << "\n" << "Directions of travel are 2 for N, 6 for E, 8 for South, 4 for West, 1 for UP, 3 for DOWN" << "\n" << endl;
   cout << "starting location = room " <<  location << "\n" << endl;
   cout << "Enter your direction to travel?" << endl;

   while (location !=26)
   {
      cin >> direction;

      location = map[location][direction];         //compiler throws this line up as  invalid ?

      if(location == 99)
      {
         cout<<"Incorrect move...!!!... Enter direction again."<<endl;
         location = old_location;
      }
      else
      {
         cout << "Your at location " << location << endl;
         old_location = location;

         if (location == 26)
         cout << "You've found the exit" << endl;

      }
   }
   return 0;
}

Reread Banfa's post as he has given you the answer. Specifying map[1][1] gives you a pointer to the row on line 28 in the code above, so it is of type int * . You need the third dimension in there for "map" to resolve down to an int.

Sorry Banfa, thanks jonsca, working now with

location = map[0][location][direction];

Didn't get what you meant at first

Leppie

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.