I'm just messing around with a program right now. I want to set up the program in a while loop so it will continually run, but I want it to stop when I press a certain key. How do I set up an interrupt to do this?
I'm just messing around with a program right now. I want to set up the program in a while loop so it will continually run, but I want it to stop when I press a certain key. How do I set up an interrupt to do this?
State your OS and Compiler.
See the FAQ entry on reading key presses without waiting for a newline.
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
In XP, you don'tHow do I set up an interrupt to do this?
In DOS, you hook the keyboard interrupt, but this would work just fine.
I don't know if it works in console b/c frankly I've never coded console programs. I went from DOS to DX to MFC.Code:while (!kbhit()) { ..run your loop }
Last edited by VirtualAce; 08-27-2006 at 12:39 PM.
kbhit() works in my somewhat old version of Dev-C++; it's in <conio.h>.
[edit] If you want the loop to end only if a certain key is pressed (and not just any key), you might try something like this:
[/edit]Code:while(!kbhit() || getche() != ' ') { }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I checked MSVC 2005 and kbhit() is in there. Originally I thought it was only in dos.h but I was wrong.
you could look into signals maybe in linux. They might be useful.
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c