My assignment is to take in values that will create a maze, and then let the user traverse the maze by use of a stack to store values.

I thought I had it down to it working, but now it isn't doing what it should and then it stops after only moving a few places (the program just won't take in more values). As far as what I've discovered, it doesn't store the values correctly (which is probably what's messing up the program) and it does something weird when a person enters another value.

There are basically two functions that I'm worried about (cause it worked when those weren't there) so if anyone can find what's wrong and suggest how to fix it, I'd really appreciate it. I'll give you the two functions and the text file. If you want me to post the full code, I can but it's really long.

And here are some picture to show where the issues are.
Stores 2 values then stops
http://i21.photobucket.com/albums/b272/DemonGal711/Program%20Pic/test1.png

Stores 1 value, is suppose to pop it out and move back to previous spot (but doesn't), then stops
http://i21.photobucket.com/albums/b272/DemonGal711/Program%20Pic/test2.png

Not a valid response, doesn't have an issue until two valid values are entered (though I didn't show that cause the first two show that).
http://i21.photobucket.com/albums/b272/DemonGal711/Program%20Pic/test3.png

What my text file t.txt looks like

4 4
0 0 1 0 
0 1 0 0 
0 0 0 1 
1 1 0 0

TWO FUNCTIONS

void ask (stack& s, int& x, int& y, cell **a, int r, int c, int& count)
{ char cDir[5] = {'U', 'D', 'L', 'R', 'B'};
  int cVal[5] = {0, 0, 0, 0, 0};
  int ran = 1;
  char val;
  cout << "Where would you like to move:\n";
  goToPts (x, y, r, c, s, a, cVal);
  cin >> val;
  val = toupper(val);
  
  for (int i = 0; i < 5; i++)
      { if (cDir[i] == val)
           { if (cVal[i] == 1) ran = 0;  } } // END FOR

  if (ran == 1) { cout << "That was not a valid direction.  Please enter a valid direction.\n\n\n"; }
  else   
     { s.push(val);
       count++;
       switch (s.top())
          { case 'L':
                 if (s.prev() == 'L') a[x][y].replace = "\xC4";
                 if (s.prev() == 'U') a[x][y].replace = "\xD9";
                 if (s.prev() == 'D') a[x][y].replace = "\xBF";
                 y--;
              break;
            case 'R':
                 if (s.prev() == 'R') a[x][y].replace = "\xC4";
                 if (s.prev() == 'U') a[x][y].replace = "\xC0";
                 if (s.prev() == 'D') a[x][y].replace = "\xDA";
                 y++;
              break;
            case 'U':
                 if (s.prev() == 'R') a[x][y].replace = "\xD9";
                 if (s.prev() == 'L') a[x][y].replace = "\xC0";
                 if (s.prev() == 'U') a[x][y].replace = "\xB3";
                 x--;
              break;
            case 'D':
                 if (s.prev() == 'R') a[x][y].replace = "\xBF";
                 if (s.prev() == 'D') a[x][y].replace = "\xB3";
                 if (s.prev() == 'L') a[x][y].replace = "\xDA";
                 x++;
              break;
            case 'B':
                 a[x][y].replace = " ";
                 switch (s.prev())
                    { case 'L':
                         y++;
                         break;
                      case 'R':
                         y--;
                         break;
                      case 'U':
                         x++;
                         break;
                      case 'D':
                         x--;
                         break;
                    } // END switch
               s.pop();
               s.pop();
               count--;
               break;
            } // END switch(val)
     } // END else

} // END ask



void goToPts (int a, int b, int r, int c, stack s, cell **array, int check[])
{
  if (a != 0   && array[a-1][b].num == 0 && s.top() != 'D') 
     { cout << "U) Up   \t"; 
       check[0] = 1; }
  if (a != r-1 && array[a+1][b].num == 0 && s.top() != 'U') 
     { cout << "D) Down \t"; 
       check[1] = 1; }

  if (b != 0   && array[a][b-1].num == 0 && s.top() != 'R') 
     { cout << "L) Left \t"; 
       check[2] = 1; }
  
  if (b != c-1 && array[a][b+1].num == 0 && s.top() != 'L') 
     { cout << "R) Right\t"; 
       check[3] = 1; }
  if (b == c-1 && a == r-1) // if at the exit, allow them to go right
     { cout << "R) Right\t"; 
       check[3] = 1; }
  if (s.top() != 'E') 
     { cout << "B) Back";
       check[4] = 1; }  
  cout << "\n\n";
}

For problems like this, I would step through the code (via F10 to step through and F11 to step 'in' in VC++). Keep an eye on your local variable list, and as their values change make sure that they are changing as expected. If not, you can isolate the exact location that the data is being handled incorrectly.

Well, one thing I've notice when I was testing values was that after it pushed the first value, and came around to push again, if I put a cout statement to see what the top and previous things were, they were all messed up. That seems to be the only things that's wrong.

I'm wondering is that I'm saving my directions as characters. Should I try saving them as strings or something? Do you think that would help?

I have the full code posted here if anyone wants to see it all. Didn't want to make this thread too long.
http://www.dreamincode.net/forums/showtopic70064.htm

Hmmm I think using chars should be fine. Post the whole program and I'll race you to debug it.

Okay, but you'll win.

Also, I even tried changing it to store integers to see if perhaps the character values were messing it up and I'm still getting the same problem with it changing my values. I think it's something wrong with my stack.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

struct cell
{ int num;
  string replace;
};
   
struct edge
{ string begin;
  string end;
};

struct NodeType
    { char item;
      NodeType* next;
    };

class stack
{ public:
     stack()
        { ptr = NULL; } // END CONSTRUCTOR

     ~stack()
          { NodeType* temp;
            while (ptr != NULL)
                  { temp = ptr;
                    ptr = ptr->next;
                    delete temp; }
          } // END DESTRUCTOR

     void push(char item)
          { NodeType* location;
            location = new NodeType;
            location->item = item;
            location->next = ptr;
            ptr = location;
           } // END OF PUSH

     void pop()
          { if (ptr == NULL) ;
            else
               { NodeType* temp;
                 temp = ptr;
                 ptr = ptr->next;
                 delete temp; }
          } // END OF POP

     char top()
          { if (ptr == NULL) return 'E';
            else return ptr->item; } // END OF TOP

     char prev()
          { if (ptr->next == NULL) return 'R';
            else return (ptr->next)->item;
          } // END OF PREV

  private:
     NodeType* ptr;
};

// Functions
void getFile (ifstream&);
void decor (int);
void print (cell**, edge*, int, int);
void createShell (cell**, string&, string&, int, int, int, int);
void ask (stack&, int&, int&, cell**, int, int, int&);
void goToPts (int, int, int, int, stack, cell**, int*);

main()  // M is the row; N is the col
{   ifstream InData;
    int row, col;
    string line, str;
    edge *mark;
    cell **array;
    stack a;
    

    getFile (InData);
    InData >> row >> col;
    
    mark = new edge [row];
	array = new cell*[row];
    for (int a = 0; a < row; a++)
        { array[a] = new cell[col]; }

    // add values to the 2-D array 
    for (int b = 0; b < row; b++)
        { for ( int c = 0; c < col; c++)
              { InData >> array[b][c].num; }
        }

    for (int i = 0; i < row; i++)
        { for ( int j = 0; j < col; j++)
              { createShell (array, str, line, i, j, row, col); }
          mark[i].begin = line;
          mark[i].end = str;
        }
    
    // Begins the actual maze questioning    
    cout << "The question mark indicates your position in the maze.\n\n";
    int x, y, count;
    x = y = count = 0;
    
    mark[0].begin = "\xC4";
    while (x != row-1 && y != col)
          { array[x][y].replace = '?';
            print (array, mark, col, row);
            ask (a, x, y, array, row, col, count); }
    mark[row].end = "\xC4";

    // free array
    for(int f = 0; f < row; f++)
        { delete array[f]; }
    delete array;
    system("pause");
    return 0;
}

void ask (stack& s, int& x, int& y, cell **a, int r, int c, int& count)
{ char cDir[5] = {'U', 'D', 'L', 'R', 'B'};
  int cVal[5] = {0, 0, 0, 0, 0};
  int ran = 1;
  char val;
  cout << "Where would you like to move:\n";
  goToPts (x, y, r, c, s, a, cVal);
  cin >> val;
  val = toupper(val);
  
  for (int i = 0; i < 5; i++)
      { if (cDir[i] == val)
           { if (cVal[i] == 1) ran = 0;  } } // END FOR

  if (ran == 1) { cout << "That was not a valid direction.  Please enter a valid direction.\n\n\n"; }
  else   
     { s.push(val);
       count++;
       switch (s.top())
          { case 'L':
                 if (s.prev() == 'L') a[x][y].replace = "\xC4";
                 if (s.prev() == 'U') a[x][y].replace = "\xD9";
                 if (s.prev() == 'D') a[x][y].replace = "\xBF";
                 y--;
              break;
            case 'R':
                 if (s.prev() == 'R') a[x][y].replace = "\xC4";
                 if (s.prev() == 'U') a[x][y].replace = "\xC0";
                 if (s.prev() == 'D') a[x][y].replace = "\xDA";
                 y++;
              break;
            case 'U':
                 if (s.prev() == 'R') a[x][y].replace = "\xD9";
                 if (s.prev() == 'L') a[x][y].replace = "\xC0";
                 if (s.prev() == 'U') a[x][y].replace = "\xB3";
                 x--;
              break;
            case 'D':
                 if (s.prev() == 'R') a[x][y].replace = "\xBF";
                 if (s.prev() == 'D') a[x][y].replace = "\xB3";
                 if (s.prev() == 'L') a[x][y].replace = "\xDA";
                 x++;
              break;
            case 'B':
                 a[x][y].replace = " ";
                 if (s.prev() == 'L') y++;
                 if (s.prev() == 'R') y--;
                 if (s.prev() == 'U') x++;
                 if (s.prev() == 'D') x--;
                 if (s.prev() == 'B') cout << "\nerror";
                 s.pop();
                 count--;
               break;
            } // END switch(val)
     } // END else

} // END ask



void goToPts (int a, int b, int r, int c, stack s, cell **array, int check[])
{
  if (a != 0   && array[a-1][b].num == 0 && s.top() != 'D') 
     { cout << "U) Up   \t"; 
       check[0] = 1; }
  if (a != r-1 && array[a+1][b].num == 0 && s.top() != 'U') 
     { cout << "D) Down \t"; 
       check[1] = 1; }
  if (b != 0   && array[a][b-1].num == 0 && s.top() != 'R') 
     { cout << "L) Left \t"; 
       check[2] = 1; }
  
  if (b != c-1 && array[a][b+1].num == 0 && s.top() != 'L') 
     { cout << "R) Right\t"; 
       check[3] = 1; }
  if (b == c-1 && a == r-1) // if at the exit, allow them to go right
     { cout << "R) Right\t"; 
       check[3] = 1; }
  if (s.top() != 'E') 
     { cout << "B) Back";
       check[4] = 1; }  
  cout << "\n\n";
}


//  ANYTHING BELOW HERE IS COMPLETE AS FAR AS CODING
void createShell (cell **a, string& s, string& l, int x, int y, int m, int n)
// Creating the board and marks between lines.
   { string at, next;
     if (x == m-1 && y == n-1)
                   { s = ' '; }
                else if (a[x][n-1].num == 1) s = "\xDB";
                else s = "\xDE";
     if (y == 0)
        { if (x == 0)
             { l = " "; }
          else if (a[x][y].num == 1) l = "\xDB";
          else l = "\xDD"; }

// Using ycode
     switch (a[x][y].num)  // left and right ATM
            { case 0:
                   a[x][y].replace = " "; // 4 spaces
                   break;
              case 1:
                   a[x][y].replace = "\xDB"; // 4 blocks
                   break;
             }
    } // End of create



// CORRECT CODE
void getFile (ifstream& InData)
// Get the file from the user;
// keep asking until a valid file is found
     { string file;
       cout << "Enter the name of the file: ";
       cin >> file;
       InData.open (file.c_str());
       while(!InData)// Prompt for new file name if not able to open
            { cout << "Unable to open file. Enter a different name: ";
	          InData.clear();
              cin >> file;
	          InData.open(file.c_str()); 
     }      }


// OUTPUT

void print (cell **ar, edge *mark, int n, int m)
   { cout << "\n\t\xDC\xDC";
     for (int i = 0; i < n; i++)
         { cout << "\xDC"; }
        
     for (int x = 0; x < m; x++)
         { cout << endl << '\t' << mark[x].begin;
           for (int y = 0; y < n; y++)
               { cout << ar[x][y].replace; }
           cout << mark[x].end;
         }
     cout << "\n\t\xDF\xDF";
     for (int i = 0; i < n; i++)
         { cout << "\xDF"; }
     cout << endl;
   }

Anyone see anything, cause I'm at the point of gonna write this thing again if I can't find something and it's due tomorrow so I really can't afford to rewrite it.

Well, I figured that part out. Shouldn't have passed the stack to goToPts(). Just passing the s.top() value to that was enough.

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.