How do i stop the program from closing?

How do i stop the program from closing when the robot reaches the boundaries of the grid?



[code]#include <iostream.h>
#include<conio.h>
#include <windows.h>


int mx=30;
int my=10;
int i, j;
char direction;
const char left_key='a', right_key='d';
char keypress;

char grid[24][79];


void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}


void printgrid()
{
for (i=0; i<24; i++)
{
for (j=0; j<79; j++)
{
cout<<grid[i][j];
}
cout<<endl;
}
}

#define VMAX 3

#define HMAX 3



void printRobot(char robot[][3],int x, int y);

void fillIn(int x,int y);

int main()
{
int x,y;

// clock_t pause = 50;

char robot[VMAX][HMAX]={

{'.','o','.'},

{'-','¦','-'},

{'/',' ','\\'}

};



HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

//SetConsoleTextAttribute(hOut, BACKGROUND_BLUE|
//BACKGROUND_INTENSITY);
SetConsoleTextAttribute(hOut, BACKGROUND_GREEN|
BACKGROUND_INTENSITY);


//loadgrid();
printgrid();
gotoxy(mx,my);
printRobot(robot,x,y);

while (mx<70 && my<20 && my>0 && mx>0)
{
if (kbhit())
{
keypress=getch(); //keypress=(char)getchar()

if ((keypress == right_key) || (keypress == left_key))
direction = keypress;


if (direction == 'a')
{
gotoxy(mx,my);
cout<<" "<<flush;

mx=mx-1;
gotoxy(mx,my);

printRobot(robot,x,y);
fillIn(x,y);
gotoxy(mx,my);
}


if (direction == 'd')
{
gotoxy(mx,my);
cout<<" "<<flush;

mx=mx+1;
gotoxy(mx,my);
printRobot(robot,x,y);
fillIn(x,y);
gotoxy(mx,my);
}


direction = ' ';
}
}




// cout << "NOTE: MAKE SURE ALL ENTERED LETTERS ARE LOWER CASE!!!!!!!";


return 0;
}





void printRobot(char robot[][3],int x, int y) {

int r,c;



for(c=0,r=2;c<HMAX;c++)

cout<<robot[r][c]<<flush;

gotoxy(x,y-1);

for(c=0,r=1;c<HMAX;c++)

cout<<robot[r][c]<<flush;

gotoxy(x,y-2);

for(c=0,r=0;c<HMAX;c++)

cout<<robot[r][c]<<flush;

}

void fillIn(int x,int y) {

int i;

for(i=VMAX;i>-1;i--) {

gotoxy(x,y-i);
cout<<" "<<flush;

}

}









[\code]
Last edited on
General advice, avoid having numbers like 79 or 70 or 20 etc scattered throughout your code. Better to use defined constants like this:
1
2
3
4
const int VMAX = 3;
const int HMAX = 3;
const int HEIGHT = 24;
const int WIDTH  = 79;

and use them like this:
char grid[HEIGHT][WIDTH];

1
2
3
4
5
6
7
8
9
10
11
void printgrid()
{
    for (i=0; i<HEIGHT; i++)
    {
        for (j=0; j<WIDTH; j++)
        {
            cout<<grid[i][j];
        }
        cout<<endl;
    }
}


And to answer your question. Check the value of mx, my before moving the robot.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
            if (direction == left_key)
            {
                if (mx > 0)
                {
                    goto_xy(mx,my);
                    cout<<" "<<flush;

                    mx=mx-1;
                    goto_xy(mx,my);

                    printRobot(robot,x,y);
                    fillIn(x,y);
                    goto_xy(mx,my);
                }
            }

            if (direction == right_key)
            {
                if (mx < (WIDTH - HMAX))
                {
                    goto_xy(mx,my);
                    cout<<" "<<flush;

                    mx=mx+1;
                    goto_xy(mx,my);
                    printRobot(robot,x,y);
                    fillIn(x,y);
                    goto_xy(mx,my);
                }
            }


If you do this properly, you won't need the existing checks like this:
while (mx<70 && my<20 && my>0 && mx>0)
new version:
while (mx<=(WIDTH-HMAX) && my<20 && my>0 && mx>=0)
... the checks on mx will not be needed, since it has been caught when handling the key-presses. Thus you might just end up with something like
while (true) or maybe while (!quit)
Last edited on
Topic archived. No new replies allowed.