Reputation: 2257
This problem really perturbs me on how to do it in C way:
Display series of numbers (1,2,3,4, 5....etc) in an infinite loop. The program should quit if someone hits a specific key (Say Backspace key).
My try was to make a loop and then get the user input every time.
int main()
{
char userInput;
int i = 0;
while(true)
{
Sleep(1000);
printf("%d", ++i);
userInput = getch();
fflush(stdin);
if (userInput == '\b'){
break;
}
}
getch();
return 0;
}
But this is not the answer the problem is looking for. It does want to continue printing numbers while checking the user input.
Anyone could help a newbie with this? Thanks! :D
Update: Mr.Mark Wilkins just gave me the answer by using the _kbhit() function. And this was what my solution looked like:
int main()
{
int i = 0;
char userInput;
while( !_kbhit() && userInput != '\b' )
{
Sleep(500);
printf("%d", ++i);
}
getche();
getch();
return 0;
}
Upvotes: 4
Views: 5415
Reputation: 33637
kbhit is a <conio.h>
function and represents a very dated way of doing things.
http://en.wikipedia.org/wiki/Conio.h
Note the remark that "It is not described in The C Programming Language book, and it is not part of the C standard library, ISO C nor is it required by POSIX."
If the only special feature you want in a standard I/O program is allow for user-termination during processing, there's a basic understanding that a user can do this with Ctrl-C:
http://en.wikipedia.org/wiki/Control-C
If you desire to do advanced things, like know when the user is holding down two keys on the keyboard at once (such as a cursor key and a key used to indicate firing) you'll need to dig up a cross-platform library of some sort. Ask around among game developers:
cross platform keyboard/mouse input recommendation
Development tools in GUI environments are generally based on an event loop, and generally offer events like "keydown" and "keyup" that you can monitor, instead of only being able to receive printable characters. With those events you can (for instance) generally tell the difference between someone pressing "1" on a numeric keypad vs on the top row...or when the left control key is pressed instead of the right.
Upvotes: 0
Reputation: 2675
Short answer: You can't, if you want your program to be portable.
Long answer: There is no portable way to 'listen' for a keystroke in standard C or C++ for that matter. You'll have to use operating system specific libraries for that.
By the way, since you use getch(), I assume that you're using an ancient DOS compiler from the pre-1989 era. Bad, bad idea. Unless if you're using Curses on UNIX, which provides a getch() implementation.
I assume the source of this programming question is ancient too. Use a modern, standards-compliant compiler such as gcc/g++ or MSVC. :-)
If you are just beginning programming, stick to standards, don't use platform-specific code.
Upvotes: 1
Reputation: 401
You can use threads to accomplish what you need but thread should be handled carefully.
http://www.albahari.com/threading/
Make a boolean that is initially true. When the user inputs the character that you want then set it to false. The loop should be inside your thread and checking this boolean. The loop should quit when the boolean is set to false.
Upvotes: 1
Reputation: 41262
You can call _kbhit
to check if input is waiting. If there is, then you can read it with _getch
.
Upvotes: 3