exit if a key is hit, else continue...
I have a problem. I have a program that will run continuously. But I want to have an interupt to stop the program. How do I do it??
I tried use getch but this will cause the program to pause at getch until a key is hit (which is not what I want). I want it to continue running until a key is hit before it stops.
My program consist of a for loop which does the continuous running.
I am using Borland C++ to compile and run my program.
Re: exit if a key is hit, else continue...
Simply press Ctrl+C to abort your program. Use atexit to execute cleanup stuff.
Re: exit if a key is hit, else continue...
Is this what you want?
Code:
while(!kbhit())
{
// do something
}
Re: exit if a key is hit, else continue...
I only want a specific key that can stop the program. If I use kbhit(), it will allow any key hit to do that something rite?? How do I write so that it only allow a specific key, like ESC, to stop the program.
Re: exit if a key is hit, else continue...
yeah, the Kbhit() is triggered when ever a character is hit. but since u are lookin for a specific charecter to be hit ....after the Kbhit() u can call getch() to retrive the char that has been waitin ...n then compare it with the required hit that u want..( Esc )....before u give call to the Exit() .
hope this should help u.
cheers :-)
sreehari
Re: exit if a key is hit, else continue...
but if i user getch(), it will pause there until a key is hit (this is not what i want).
I want my program (which consist of a for loop) to continue running until ESC is hit. When ESC is hit, the program will stop.
Re: exit if a key is hit, else continue...
Quote:
Originally Posted by tlp0605
but if i user getch(), it will pause there until a key is hit (this is not what i want).
I want my program (which consist of a for loop) to continue running until ESC is hit. When ESC is hit, the program will stop.
No, because KbHit() has just told you that there is a key hit waiting, so there's no pause.
Re: exit if a key is hit, else continue...
On each iteration in your for loop call _kbhit() to see if a key was pressed. This function will not wait. If a key is pressed it will return non-zero. If that's the case use getch to get the key that was pressed and if it's esc, abort execution.
Re: exit if a key is hit, else continue...
u mean if i use kbhit 1st then getch to check what key is hit, it will not pause??
Re: exit if a key is hit, else continue...
Quote:
Originally Posted by tlp0605
u mean if i use kbhit 1st then getch to check what key is hit, it will not pause??
Yes, something like:
Code:
if (_kbhit())
{
// use getch to check key
}
Re: exit if a key is hit, else continue...
The program wil not pause executing as the scope of the Getch() vil only be inside the If( Kbhit()) block....so the Getch() vil only be called if any char has been hit by the user
Re: exit if a key is hit, else continue...
Quote:
Originally Posted by tlp0605
u mean if i use kbhit 1st then getch to check what key is hit, it will not pause??
Exactly because the char is already in the input buffer so getch() will receive it from there.
BTW Marc, with the Borland C++ compiler the function is kbhit() and not _kbhit().
Re: exit if a key is hit, else continue...
Quote:
Originally Posted by cilu
BTW Marc, with the Borland C++ compiler the function is kbhit() and not _kbhit().
Sorry, my mistake ;)