Default User wrote, On 01/10/08 23:01:
bint wrote:
>Please help. Trying to just get simple keyboard input from a C
program. Ideally would like to be able to just press a key without
having to press return,
To do that you would need to use implementation specific extensions. If
you ask in a group dedicated to your specific system they will probably
be able to tell you how to do it.
but at this point I don't care anymore. I
>can get one character using getchar(), but I have to press return.
When I do, it completely skips over my second call to getchar().
Why? How do I make successive calls that read the keyboard?
c = getchar();
if (c == 'y') {
bwok = 1;
printf("OK. You pressed 'y'\n"
The line above is broken. In the future, post your actual code. Don't
try to retype it.
> }
c = getchar();
printf("char :%c\n",c);
That's because the carriage return also places a character in the
stream. The second getchar() gets that.
Add another one right after the printf("Ok. You pressed 'y'\n");
That would solve the immediate problem, however if a user types "yes"
instead of just "y" it won't. One solution to this is a simple loop
reading until getchar() returns either EOF (end of file or an error) or
'\n'.
--
Flash Gordon
If spamming me sent it to
sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at
http://clc-wiki.net/