Click to See Complete Forum and Search --> : simple C++ question
meglamaniac
12-10-2002, 02:16 PM
How can I halt a program while waiting for a keypress (any keypress)?
Someone suggested getchar(), but for some reason that doesn't work (it loads a character into a variable (i checked) but it doesn't halt to wait for input).
Thanks.
Spawn913
12-10-2002, 09:24 PM
hmm. getchar() should work. what value is stored in the variable?
try this:
...
i=getchar();
printf("%d", i);
...
prior to calling getchar(), do you have any stdin(keyboard)-related function calls?
post here the value displayed for the variable you used, then let's try to debug from there.
:cool:
PhatBarren
12-10-2002, 09:28 PM
Originally posted by meglamaniac
How can I halt a program while waiting for a keypress (any keypress)?
Someone suggested getchar(), but for some reason that doesn't work (it loads a character into a variable (i checked) but it doesn't halt to wait for input).
Thanks.
How could you have checked it if the program was still waiting for keyboard input????
Wallex
12-10-2002, 09:51 PM
Originally posted by meglamaniac
How can I halt a program while waiting for a keypress (any keypress)?
Someone suggested getchar(), but for some reason that doesn't work (it loads a character into a variable (i checked) but it doesn't halt to wait for input).
Thanks.
It wasn't until I read '(it loads a character into a variable)' that I realized what could be happening. getch, getche, getchar and all those are used to accept a single char from the keyboard, but you claim that it is 'not waiting' for your input, instead it just goes and fetches a key. Ever wondered which key? where does this key comes from? I know this is kinda unlikely... but there's a keyboard buffer. Each time you press a key in your keyboard, it gets sent to the buffer until an app decides to fetch it (hence why when the system locks up and you start smashing keys, eventually the computer starts beeping back at you.. keyboard buffer full). So if you are using getch or getkey and you don't get a chance to enter the desired key it's because there was a key already in the buffer. There are functions to flush away the keyboard's buffer (which sounds like a good solution to what you need), but i haven't done any C in a while so I don't know/remember the function right now.
It's odd.. most people who use C don't bump into this kind of deal... I mean, why would the keyboard's buffer have keys already there?
Spawn913
12-10-2002, 09:57 PM
It's odd.. most people who use C don't bump into this kind of deal... I mean, why would the keyboard's buffer have keys already there?
Actually, this could happen a lot when working with keyboard related functions -- typically when you're processing the double-byte keys (most common: the ENTER key).
That's why I asked him to print out the value of the data he got so we can determine if what he got was the result of a previous double-byte key press.
:cool:
meglamaniac
12-11-2002, 12:08 AM
It is due to the enter key - there's a "10" left in the buffer.
The only way I can find of getting the damn thing out is to getchar once (to remove it) then getchar again.
Is there a function which probes the keyboard buffer WITHOUT getting the contents of it? This would let me build a simple buffer emptying function.
In Borland C++ (for DOS) there's a function called kbhit which does this. It returns 0 if the buffer is empty, or non zero if it isn't.
This doesn't exist in linux C++ as it is part of Borlands non-standard conio header/library.
Thanks.
Spawn913
12-11-2002, 12:24 AM
Is there a function which probes the keyboard buffer WITHOUT getting the contents of it? This would let me build a simple buffer emptying function.
try select() to check for data without actually getting it.
for a kbhit()-like program, you'll have to change your termios settings (i.e. set it to noncanonical (or raw mode)).