|
-
January 4th, 2006, 11:56 PM
#1
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.
-
January 5th, 2006, 03:07 AM
#2
Re: exit if a key is hit, else continue...
Simply press Ctrl+C to abort your program. Use atexit to execute cleanup stuff.
-
January 5th, 2006, 03:20 AM
#3
Re: exit if a key is hit, else continue...
Is this what you want?
Code:
while(!kbhit())
{
// do something
}
-
January 5th, 2006, 03:57 AM
#4
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.
-
January 5th, 2006, 04:18 AM
#5
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
-
January 5th, 2006, 04:39 AM
#6
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.
-
January 5th, 2006, 04:50 AM
#7
Re: exit if a key is hit, else continue...
 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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 5th, 2006, 04:51 AM
#8
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.
-
January 5th, 2006, 04:56 AM
#9
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??
-
January 5th, 2006, 04:58 AM
#10
Re: exit if a key is hit, else continue...
 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
}
-
January 5th, 2006, 05:24 AM
#11
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
-
January 5th, 2006, 08:38 AM
#12
Re: exit if a key is hit, else continue...
 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().
-
January 5th, 2006, 08:49 AM
#13
Re: exit if a key is hit, else continue...
 Originally Posted by cilu
BTW Marc, with the Borland C++ compiler the function is kbhit() and not _kbhit().
Sorry, my mistake
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|