Thread: press ESCAPE to exit

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    Arrow press ESCAPE to exit

    hi
    my program is a tollbooth my program is working i just want to know if want to terminate my program using ESCAPE key what should i as it is a funtion key
    here is my code
    Code:
    # include <iostream>
    # include <conio.h>
    using namespace std;
    class tollbooth
    {
    private:
    	long int total_cars;
    	float total_cash;
    public:
    	void payingcar()//inline function
    	{
    		total_cars++;
    		total_cash=total_cash+0.50;
    	
    	}//end of function
    	void nonpayingcar()//inline function
    	{
    		total_cars++;
    	}//end function
    
    	void print () //inline function
    	{
    		cout<<"total cars passed:"<<total_cars<<endl;
    		cout<<"total cash received:"<<total_cash<<endl;
    	
    	}//end function
    	tollbooth ()
    	{
    		total_cars=0;
    		total_cash=0;
    	
    	}//end constructor
    
    
    };//end class
    int main ()
    {
    	tollbooth lahore;
    	char push_key;
    	cin>>push_key;
    	
    	do
    	{
    		
    	if (push_key=='1')
    	{
    		lahore.payingcar();
    	
    	}//end if
    	if (push_key=='0')
    	{
    		lahore.nonpayingcar();
    	}//end else
    		cin>>push_key;
    	}//end do
    	
    	while (push_key!='@');
    	
    		lahore.print ();
    	
    
    	getch ();
    	
    	
    
    }//end main
    here i have used '@' key to get out from loop

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You can capture it with getch(). As escape is a function key, there is a functional part (to distinguish it from other keys) and the escape part. Then you just compare that to whatever escape is on the system and you can branch to a return statement to quit.

    You could also capture it with buffered input as in cin>> , but the buffering may change what you are looking for. To take an example from another function key, you could capture an enter key with getch() too. You might get CRLF (carriage return, line feed -- /c/r) on windows. But in buffered input the sequence is mashed to \n (newline).

    As for what escape is on your system, or the computers you want to support, I don't remember. Try setting up a simple input routine and debugging for a while to see the actual keys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  4. Einstine calculator
    By Mach_ie in forum C Programming
    Replies: 2
    Last Post: 08-30-2003, 09:37 AM
  5. Exit on key press?
    By dazed in forum C Programming
    Replies: 3
    Last Post: 12-09-2002, 07:46 PM