array dilemma

hi iam new to c++ and i have been working on a array it is fairly simple but i have a small dilemma. well you see when i move around in my 2d array it all works fine, but when i walk outside of the map it glitches up. so i was wondering if there is a way to restrict the user from going outside the map

here is the code so far:

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main (){
string direction;
string room [3][3];
bool game =true;
room[0][0]="this is the northwest room ";
room[0][1]="this is the west room ";
room[0][2]="this is the southeast room ";
room[1][0]="this is the north room ";
room[1][1]="this is the middle room ";
room[1][2]="this is the south room ";
room[2][0]="this is the northeast room ";
room[2][1]="this is the east room ";
room[2][2]="this is the southeast room ";
int x=0;
int y=0;

while(game){
cout << room[x][y];
cin >> direction;

if (direction =="east"){
x++;
}
if (direction =="south"){
y++;
}
if (direction =="west"){
x--;
}
if (direction =="north"){
y--;
}//fixa ett slut
}
getch ();
return 0;
}
Check or the value you're about to chance already reached it's limits, eg for "east":

1
2
3
4
5
6
7
if (direction == "east")
{
    if (x == MAX)
        cout<<"You just walked agains a wall!";
    else
        x++;
}
there is still a little problem, it says: MAX : undeclared identifier

do i need some #include or anything like that?
You will need to define MAX.
By MAX I just ment the maximal value x can has: in your case thats 2. The same way, you need to define the maximum value for y and for both x and y a minumium value, for the other directions.
oooh i see know thanks allot for the help
Topic archived. No new replies allowed.