GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Mar-2009, 13:33
quddos quddos is offline
New Member
 
Join Date: Mar 2009
Posts: 3
quddos has a little shameless behaviour in the past

Exit a running program by pressing "Esc" key


How can I exit a running 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.
  #2  
Old 07-Mar-2009, 04:54
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Exit a running program by pressing "Esc" key


Quote:
Originally Posted by quddos
How can I exit a running 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.

Hope that you don't mind that this is a "C" response to your "C++" question, but it may work for your needs.

CPP / C++ / C Code:
#include <cstdio>

using namespace std;

const char* ASCII[] =  { "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS",  "TAB", "LF",  "VT",  "FF",  "CR",  "SO",  "SI",
                         "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US",
                         "SPC", "!",   "\"",  "#",   "$",   "%",   "&",   "'",   "(",   ")",   "*",   "+",   ",",   "-",   ".",   "/",
                         "0",   "1",   "2",   "3",   "4",   "5",   "6",   "7",   "8",   "9",   ":",   ";",   "<",   "=",   ">",   "?",
                         "@",   "A",   "B",   "C",   "D",   "E",   "F",   "G",   "H",   "I",   "J",   "K",   "L",   "M",   "N",   "O",
                         "P",   "Q",   "R",   "S",   "T",   "U",   "V",   "W",   "X",   "Y",   "Z",   "[",   "\\",  "]",   "^",   "_",
                         "`",   "a",   "b",   "c",   "d",   "e",   "f",   "g",   "h",   "i",   "j",   "k",   "l",   "m",   "n",   "o",
                         "p",   "q",   "r",   "s",   "t",   "u",   "v",   "w",   "x",   "y",   "z",   "{",   "|",   "}",   "~",   "DEL"};

int main()
{
    const static char ESC   = 0x1B;
    char c = 0;
    do
    {
        c = getchar();
        printf("key pressed = %s\n", ASCII[(size_t)c]);
    } while(c != ESC);
    return 0;
}



Output:

Code:
$ ./esc_key key pressed = LF a key pressed = a key pressed = LF b key pressed = b key pressed = LF c key pressed = c key pressed = LF D key pressed = D key pressed = LF 4 key pressed = 4 key pressed = LF $ key pressed = $ key pressed = LF ^[ key pressed = ESC


MxB
  #3  
Old 07-Mar-2009, 13:05
quddos quddos is offline
New Member
 
Join Date: Mar 2009
Posts: 3
quddos has a little shameless behaviour in the past

Re: Exit a running program by pressing "Esc" key


yar your code is a little bit helpful i need to exit the runnig program at any instant.but your code only exit when compiler reades getchar() statement.thankx buddy.
  #4  
Old 07-Mar-2009, 18:34
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Exit a running program by pressing "Esc" key


Quote:
Originally Posted by quddos
yar your code is a little bit helpful i need to exit the runnig program at any instant.but your code only exit when compiler reades getchar() statement.thankx buddy.

Hello?! What did you tell us about "your program?" Nothing. What did you tell us about the system that you're using? Nothing. My code only "exits" when the compiler reads getchar is NOT TRUE. My code only exits when a conditional expression evaluates to true and that is when a character "c" is equal to the ASCII value for the escape key.

If you want to exit the program "at any instant" then you probably will need a PLATFORM SPECIFIC method of trapping and/or handling the escape key. You'll note that you did not provide the platform you're using in any of your remarks. Therefore, I provided you with a platform agnostic way of responding to key presses. If that doesn't satisfy your requirements, do not "blame" my response, blame YOUR set of requirements as you posted.

If you are using Windoze, which is highly likely considering your attention to detail, I'd recommend that you trap VK_ESC in your main Windows procedure when looking for WM_KEYDOWN. Of course, that is assuming a lot, which I didn't do previously.

If you want to modify your requirements after posting, please do so, but don't act like I didn't "solve" the problem as you presented it simply because I didn't give you the answer that you wanted, but failed to ask in a specific and direct way. What makes you think that I will give you any more useful information than the computer that you're trying to program? It will do exactly what you tell it to do. If that isn't what you want, you need to approach the problem from a different perspective...because you haven't yet found the one that works, huh?


MxB
  #5  
Old 08-Mar-2009, 01:27
quddos quddos is offline
New Member
 
Join Date: Mar 2009
Posts: 3
quddos has a little shameless behaviour in the past

Re: Exit a running program by pressing "Esc" key


I have posted complete code of my assignment. I am using Windows as operating system and dev as compiler. Now kindly tell me how would I exit the program at any instant by pressing esc key.

CPP / C++ / C Code:
/*
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;
       }
Last edited by admin : 08-Mar-2009 at 07:42. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adsense Integration Guide Extreme89 AdSense Forum 1 10-Jul-2006 07:04
EbizWebDirectory.com - Best Ecommerce Directory your one stop online ecommerce guide ebwebdirectory Member Announcements, Advertisements & Offers 0 16-Jan-2006 03:01
complete e-marketing guide primelibra Search Engine Optimization Forum 4 01-Aug-2004 20:45
IRC Services Admin help guide Allowee Open Discussion Forum 2 25-Apr-2004 08:32
Sydog's DVD to Divx Conversion Guide asanthadenz Computer Software Forum - Windows 1 14-Mar-2003 14:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:38.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.