getchar() problem

A

Alan

I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to press
the ENTER key as well. This gives me 2 input characters - 'a' and
'\n' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to terminate reading
stdin.

I am trying to get the program to respond when I press one key only (ie
without needing to press ENTER as well).

Program
~~~~~~~
#include <stdio.h>
#include <stdlib.h>

int main()
{
char x; //Input character
while (1)
{
printf(">"); //Print prompt '>'
x = getchar(); //Get input character
printf("%d %02x \n", x, x);//Print character in decimal & Hex
if (x == 'q') //Exit if the character is 'q'
exit(0);
}
}

Output
~~~~~
a < input character 97 61
10 0a
b < input character 98 62
10 0a
c < input character 99 63
10 0a
d < input character 100 64
10 0a
q < input character
113 71

I have tried using scanf() but got the same results.

Any ideas as to how I can input ONE character only? TIA :)

Alan
 
E

Eric

Alan said:
I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to
press
the ENTER key as well. This gives me 2 input characters - 'a' and
'\n' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to terminate reading
stdin.

I am trying to get the program to respond when I press one key only (ie
without needing to press ENTER as well).

Program
~~~~~~~
#include <stdio.h>
#include <stdlib.h>

int main()
{
char x; //Input character
while (1)
{
printf(">"); //Print prompt '>'
x = getchar(); //Get input character
printf("%d %02x \n", x, x);//Print character in decimal &
Hex
if (x == 'q') //Exit if the character is 'q'
exit(0);
}
}

Output
~~~~~
113 71

I have tried using scanf() but got the same results.

Any ideas as to how I can input ONE character only? TIA :)

Alan

Search the net for kbhit.c there's a version of it out there for linux, i
use it a lot and it solves these kinds of problems. Just add the kbhit.c
file to your Makefile and put #include "kbhit.h" in your program.
Call init_keyboard() at top of program and close_keyboard() on or before
exit. These two steps are really important, dont forget them.
Warning: failure to call close_keyboard() before your program exits will
leave your keybaord in a really weird state.

Ah hell, its short enough - Here...
Eric

/* The following code is kbhit.c */
#include "kbhit.h"
#include <termios.h>
#include <unistd.h> // for read()

static struct termios initial_settings, new_settings;
static int peek_character = -1;

void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}

void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}

int kbhit()
{
unsigned char ch;
int nread;

if (peek_character != -1) return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1)
{
peek_character = ch;
return 1;
}
return 0;
}

int readch()
{
char ch;

if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
/*
kbhit.c EOF
*/


/* The following code is kbhit.h */
#ifndef __KBHIT_H__
#define __KBHIT_H__
void init_keyboard(void);
void close_keyboard(void);
int kbhit(void);
int readch(void);
#endif

/*
kbhit.h EOF
*/

/*
Use it like this quick and dirty example
*/
#include <stdlib.h>
#include <stdio.h>
#include "kbhit.h"
int main(void)
{
int ch;

init_keyboard(); // run this once per program

// one way to use it
if(kbhit()) ch = readch();

// here's another sample usage
do {
if(kbhit()) // kbhit returns immediately, key pressed or not
{
ch = readch();
// do whatever here based on value of ch
if(ch='q') break;
}
sleep(1);
} while(1);

close_keyboard();

return 0;
}
 
F

Flash Gordon

If a function does not take parameters it is better to be explicit about it.
int main(void)

You should flush stdout here your your prompt might not be displayed.

getchar can return EOF indicating end-of-file or error. Most systems
have a way for the user to signal end-of-file from the keyboard, so even
if your program is only used interactively this can still happen. Get
the result in to a variable of type int (not char) and test for EOF.
Search the net for kbhit.c there's a version of it out there for linux, i
use it a lot and it solves these kinds of problems. Just add the kbhit.c

<snip>

Note that kbhit is non-standard and non-portable and any possible
implementation of it is not portable. If you want to discus how it can
be done please take it to an appropriate system specific group.

The reason that getchar works like it does is that input from stdin is
normally line buffered (i.e. input is loaded in to a buffer until there
is a complete line and only then made available to the program).

Also, what do you want to happen if the user redirects input to coming
from a file? Or pipes in the output of another program? Using the kbhit
solution suggested by Eric would probably be a problem then.
 
C

CBFalconer

Alan said:
I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I
have to press the ENTER key as well. This gives me 2 input
characters - 'a' and '\n' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to
terminate reading stdin.

I am trying to get the program to respond when I press one key
only (ie without needing to press ENTER as well).

Just think about things for a moment. In general, you want to be
able to type, and correct, input on the fly. This means using
things like the backspace key. You want this editing to be done up
front, between the keyboard and your program. This means there has
to be a means of signalling "Editing done, use this". That is the
ENTER key, better known to us old fogies as 'return'.

Now think about how this is actually done, and you will see where
the term 'buffered input' comes from.

Some systems make provisions for bypassing all this. They use
non-standard features, and are thus off topic here. For discussion
of them go to a newsgroup that discusses your particular system.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
A

Alan

Alan said:
I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to
press
the ENTER key as well. This gives me 2 input characters - 'a' and
'\n' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to terminate reading
stdin.

I am trying to get the program to respond when I press one key only (ie
without needing to press ENTER as well).


I changed the program by adding a second getchar()

.....
char x;
.....
x = getchar();
getchar();
....

Program now works the way I wanted it to. Seems like the second getchar()
"absorbs" the '\n' from pressing ENTER.

Thanks for all your replies.

Alan
 
K

Keith Thompson

Alan said:
I changed the program by adding a second getchar()

....
char x;
....
x = getchar();
getchar();
...

Program now works the way I wanted it to. Seems like the second getchar()
"absorbs" the '\n' from pressing ENTER.

That's a good start, but what happens if the user types two characters
before pressing ENTER? Or presses ENTER immediately, without typing
anything else first?

What you really want to do (I suspect) is:

Read a single character from stdin.
If it's a '\n' you're done.
If not, skip everything up to and including the next '\n'.

Another way to express this is:

Read a line from stdin and return its first character.

except that you don't need to store the entire line.

Wrapping this in a function would be a good exercise. And don't
forget to deal with EOF.
 
P

Peter Shaggy Haywood

Groovy hepcat Alan was jivin' on Thu, 11 May 2006 14:07:08 +1000 in
comp.lang.c.
getchar() problem's a cool scene! Dig it!
I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to press
the ENTER key as well. This gives me 2 input characters - 'a' and
'\n' (Hex 61 and 0a)

But of course.
It seems as though the getchar() function needs ENTER to terminate reading
stdin.

I am trying to get the program to respond when I press one key only (ie
without needing to press ENTER as well).

You can't portably do that. However, your implementation or third
party libraries may provide extentions or add-ons that do what you
want.
<OT>
Since you are in a Unix environment, look up getch() in the curses
(or ncurses or lcurses or...) library.
</OT>

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top