My program runs a three dimensional maze (3x3x3 = 27 locations) it has a start room exit room and code room all in a loop so no two are the same location, the start and exit rooms show up on the run compiled program but the code room does not cout? However if run from the executable created it shows all three rooms, re-running of the compiled shows the code room still missing? It compiles fine apart from this and I am not sure if somehow I've coded it wrong, although then it should have thrown up compile errors? (Lost)! Here's the code

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

using namespace std;

int main()
{
   int direction;
   int location, old_location;
   int startRoom;
   int endRoom;
   int codeRoom;   

do {
   time_t now;
   time(&now);
   srand(now);
   
   startRoom = rand() % 26;
   endRoom = rand() % 26;
   codeRoom = rand() % 26;
   system ("cls");
}while  (startRoom == endRoom||startRoom == codeRoom||endRoom == codeRoom);   //while start, code or end match = to re-allocate
    location = startRoom;
    
   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
       }
       };

   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," << endl;
   cout << "1 for Up, 3 for DOWN" << "\n" << endl;
   cout << "starting location = room " <<  location << endl;
   cout << "Final exit = room " << endRoom << endl;
   cout << "Code for final exit room = room " << codeRoom << "\n" << endl; //= no show on compiled version?????????
   cout << "Enter your direction to travel?" << endl;

   while (location !=endRoom)
   {
      cin >> direction;
      
      location = map[0][location][direction];
      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 == endRoom)
         cout << "You've found the exit" << endl;
      }
   }
   system("PAUSE");
   return 0;
}

I know windows and using system, bad, bad!

Any help much appreciated on this confusing issue.
Regards Leppie.

Recommended Answers

All 5 Replies

There is no difference between "the compiled code" and "the executables created". The code compiles INTO the executables! Can you explain how you are running these "different versions"?

It compiles fine apart from this and I am not sure if somehow I've coded it wrong, although then it should have thrown up compile errors? (Lost)!

"Site web computer far opens" will pass your spell checker (and maybe even some grammar checkers) but it doesn't make much if any sense in English. The compiler will pass anything that matches its set of rules whether it runs or not.

Who's teaching these people computer lingo????

My program runs a three dimensional maze (3x3x3 = 27 locations)...

No, it creates a maze. It doesn't run anything.

... the start and exit rooms show up on the run compiled program ...

A "run compiled program?" What the heck does that mean? Run from the IDE?

... but the code room does not cout?

Things don't cout. Things get displayed. A ? is used after a question, not a statement.

However if run from the executable created it shows all three rooms, re-running of the compiled...

The executable created is the compiled...

It compiles fine apart from this ...

It compiles fine because you have no syntax errors. Logic errors do not affect compiling in any way.

... and I am not sure if somehow I've coded it wrong, although then it should have thrown up compile errors?

If it doesn't do what you wanted, you coded it wrong. If you write your code wrong, how is the compiler supposed to know that? If your code is cout << "Yellow Woild" << endl; , do you expect a compiler error because you misspelled Hello World?

do {
   time_t now;
   time(&now);
   srand(now);

srand() and all this time stuff should be at the top of the program, not in the loop. All you are doing here is resetting the random generator to the same value, which generates the same random values each time the loop runs. system ("cls"); -- please don't do this. It's a waste of time and annoying to the user. int map[3][9][10] = -- get this huge definition out of the middle of your code! It makes the program very hard to follow. Put it at the top of main()

I know windows and using system, bad, bad!

Do I need to ask the obvious question? Or are you smart enough to correct your mistake? :icon_rolleyes:

Hi thanks for the replies, I was running the compiled program from within bloodshed, compile and run, and then clicking the executable to run that. My problems not solved but is unrelated to this code as another short programm is also displaying the same problem. Looks like I've probably screwed up my memmory on the hard drive or somethink?

Thanks Walt for your advice, my apologies for the gramatical mistakes in my explanation of the problem. Your totally right about getting srand out of the loop (and yes I should have spotted that!) The annoying system ("cls") was just in there to stop the loop displaying all it's variations if rooms was designated the same location. I take your advice on moving the map itself out of main and will try to implement this, (still a noob, trying my best).
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.