Please guide me

how can i exit a runnig program by pressing "esc " key.
actually i want to use it in my minesweeper assignment.the game is complete .
but only that portion is incomplete.
Are you using some external libraries?
yes i am using
stdlib,conio,string,stdio,
conio.h has the getch() function which returns an ascii code corresponding to keyboard input, Esc key should be 27.

--Function from conio aren't good if you can use curses, windows or something similar is better--
Guy i want a statement that wait for esc key through out the running process.the moment we press esc key our program should exit.could you please tell me some statements regarding this problem.
As I said you can use getch:
1
2
3
4
5
6
#include <conio.h>
int main()
{
   while(getch()!=27);
   return 0;
}

You can use windows API:
1
2
3
4
5
6
#include <windows.h>
int main()
{
    while( !GeatAsyncKeyState( VK_ESCAPE ) ); 
    return 0;
}

You can use curses
1
2
3
4
5
6
7
8
#include <curses.h>
int main()
{
    WINDOW *wnd = initscr();
    while (getch()!=27);
    endwin();
    return 0;
}


And there are lots of other different ways to do this
Last edited on


i was unable to implement your code in my program.when i run your code seperately it works perfectly.i need a bit more kind help from you.

regards
Quddos








/*
Abdul quddos
bte-fa07-003-8c
*/

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>

using namespace std;
int main()
{
// clrscr();
int i,j,x,x1,l2,y,t,r,x2,i1,j1,l=0;
char a[8][8]={'o','m','o','o','m','o','o','m',
'm','o','o','o','m','o','m','o',
'o','o','m','o','o','m','m','o',
'm','o','o','m','o','o','m','o',
'o','o','o','m','o','m','o','m',
'o','m','o','m','o','o','m','m',
'o','m','o','m','o','o','o','m',
'm','o','m','m','o','m','m','o'};

char s[]={"abdul quddos"};
char s1[15];
cout<<"Enter Password :";
tag4:
gets(s1);
l2=strcmp(s1,s);
if(l2!=0)
{
cout<<"Invalid Password "<<endl;
getch();
cout<<"Enter valid password :";
goto tag4;
}
else if(l2==0)
{
cout <<" Login successfully "<<endl;
}
cout<<"\t\t\t\tMINESWEEPER\n\n\t\t\t\t BY\n\n\t\t\t\tABDUL QUDDOS\n\n\n\n";
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n\n";
}
tag:
tag2:
cout<<"\nenter your location : ";
cout<<"\nenter row : ";
cin>>x;
if(x<0||x>8)
{
cin.clear();
cin.ignore();
cout<<"\nInvalid input";
getch();
goto tag2;
}
tag3:
cout<<"enter column :";
cin>>y;
if(y<0||y>8)
{
cin.clear();
cin.ignore();
cout<<"\nInvalid input";
getch();
goto tag3;
}
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(i==x && j==y)
{
if(a[i][j]=='m')
{
cout<<"\n\nYour value is : "<<a[i][j]<<" Game end";
break;
}
else if(a[i][j]=='o')
{
a[i][j]=x1;
l=0;
if((i==0)&&(j>0&&j<8)) //row is 0 and column is (1-7)
{

for(t=i;t<=i+1;t++)
{

for(r=j-1;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}
else if((i>0&&i<8)&&(j==0)) //row is(1-7) and column is 0
{

for(t=i-1;t<=i+1;t++)
{

for(r=j;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}
else if(i==0&&j==0) //row is 0 and column is 0
{


for(t=i;t<=i+1;t++)
{

for(r=j;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}


else if((i>0&&i<7)&&(j>0&&j<7)) //row(1-6) and column (1-6)
{
for(t=i-1;t<=i+1;t++)
{

for(r=j-1;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}
else if(i==7&&j==7) //row is 7 and column is 7
{
for(t=i-1;t<=i;t++)
{

for(r=j-1;r<=j;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}
else if(i==7&&j>0) //row is 7 and column is greater than 0
{
for(t=i-1;t<=i;t++)
{

for(r=j-1;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}

}
else if(i>0&&j==7) //row is greater than 0 and column is 7
{
for(t=i-1;t<=i+1;t++)
{

for(r=j-1;r<=j;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}

}





for(i1=0;i1<8;i1++)
{
for(j1=0;j1<8;j1++)
{
if(x1==a[i1][j1])
{

for( r=48;r<57;r++)
{

x2=r;
if(l+48==x2)
{
a[x][y]=r;
cout<<"\t"<<a[i1][j1];
}
}
}
else
cout<<"\t"<<a[i1][j1];

}

cout<<"\n\n";

}


}
goto tag;

}
}
}
getch();
return 0;
}
Please use [code][/code] tags
I suggest you to use loops instead of gotoes

When you want to have the option of exiting your program by pressing Esc?
I'm guessing he wants it where he's written getch() at the end, so code should be:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Abdul quddos
bte-fa07-003-8c
*/

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include <windows.h>

using namespace std;
int main()
{
// clrscr();
int i,j,x,x1,l2,y,t,r,x2,i1,j1,l=0;
char a[8][8]={'o','m','o','o','m','o','o','m',
'm','o','o','o','m','o','m','o',
'o','o','m','o','o','m','m','o',
'm','o','o','m','o','o','m','o',
'o','o','o','m','o','m','o','m',
'o','m','o','m','o','o','m','m',
'o','m','o','m','o','o','o','m',
'm','o','m','m','o','m','m','o'};

char s[]={"abdul quddos"};
char s1[15];
cout<<"Enter Password :";

do{
     gets(s1);
     l2=strcmp(s1,s);
     cout<<"Invalid Password "<<endl;
     getch();
     cout<<"Enter valid password :";
     goto tag4;
}while(l2!=0)

else if(l2==0)
{
     cout <<" Login successfully "<<endl;
}

cout<<"\t\t\t\tMINESWEEPER\n\n\t\t\t\t BY\n\n\t\t\t\tABDUL QUDDOS\n\n\n\n";
for(i=0;i<8;i++)
{
     for(j=0;j<8;j++)
     {
          cout<<"\t"<<a[i][j];
     }
     cout<<"\n\n";
}
tag:
tag2:
cout<<"\nenter your location : ";
cout<<"\nenter row : ";
cin>>x;
if(x<0||x>8)
{
     cin.clear();
     cin.ignore();
     cout<<"\nInvalid input";
     getch();
     goto tag2;
}
tag3:
cout<<"enter column :";
cin>>y;
if(y<0||y>8)
{
     cin.clear();
     cin.ignore();
     cout<<"\nInvalid input";
     getch();
     goto tag3;
}
for(i=0;i<8;i++)
{
     for(j=0;j<8;j++)
     {     
          if(i==x && j==y)
          {
               if(a[i][j]=='m')
                    {
                         cout<<"\n\nYour value is : "<<a[i][j]<<" Game end";
                         break;
                    }
                    else if(a[i][j]=='o')
                    {
                         a[i][j]=x1;
                         l=0;
                         if((i==0)&&(j>0&&j<8)) //row is 0 and column is (1-7)
                         {

                              for(t=i;t<=i+1;t++)
                              {
                                   for(r=j-1;r<=j+1;r++)
                                   {
                                        if(a[t][r]=='m')
                                        {
                                             l++;
                                        }
                                   }
                              }
                         }
                         else if((i>0&&i<8)&&(j==0)) //row is(1-7) and column is 0
                         {
                              for(t=i-1;t<=i+1;t++)
                              {
                                   for(r=j;r<=j+1;r++)
                                   {
                                        if(a[t][r]=='m')
                                        {
                                             l++;
                                        }
                                   }
                              }
                         }

//
// After this point I gave up on formatting...
//

else if(i==0&&j==0) //row is 0 and column is 0
{


for(t=i;t<=i+1;t++)
{

for(r=j;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}


else if((i>0&&i<7)&&(j>0&&j<7)) //row(1-6) and column (1-6)
{
for(t=i-1;t<=i+1;t++)
{

for(r=j-1;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}
else if(i==7&&j==7) //row is 7 and column is 7
{
for(t=i-1;t<=i;t++)
{

for(r=j-1;r<=j;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}
}
else if(i==7&&j>0) //row is 7 and column is greater than 0
{
for(t=i-1;t<=i;t++)
{

for(r=j-1;r<=j+1;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}

}
else if(i>0&&j==7) //row is greater than 0 and column is 7
{
for(t=i-1;t<=i+1;t++)
{

for(r=j-1;r<=j;r++)
{
if(a[t][r]=='m')
{
l++;
}
}
}

}





for(i1=0;i1<8;i1++)
{
for(j1=0;j1<8;j1++)
{
if(x1==a[i1][j1])
{

for( r=48;r<57;r++)
{

x2=r;
if(l+48==x2)
{
a[x][y]=r;
cout<<"\t"<<a[i1][j1];
}
}
}
else
cout<<"\t"<<a[i1][j1];

}

cout<<"\n\n";

}


}
goto tag;

}
}
}
while( !GeatAsyncKeyState( VK_ESCAPE ) );
return 0;
}


after editing, partially formatting, and replacing tag4 with a do while loop
hi buddy.whats about that error.i dont understand this error.


regards
Quddos

241 F:\Mines new.cpp `GeatAsyncKeyState' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
hi Bazzy.i want to terminate the program at any moment.like we open a windows we can exit it at any time by clicking close button.i want this feature in my program.hope you will understand my problem.thanks



regards
Quddos
That was a typo, change GeatAsyncKeyState to GetAsyncKeyState
oh dam... I noticed that it was a typo when you wrote it, and was going to point that out but then forgot a copied+pasted your code

hi.it still cant solve my problem.by using your code the program terminates when game is over .ie a mine is selected.please help me .
Try inserting this line if ( GetAsyncKeyState( VK_ESCAPE ) ) return 0; just after tag2, if the user pressed escape during the previous input, the program should terminate
it dont work program doesnt exit .

regards
quddos
Topic archived. No new replies allowed.