Help - Search - Members - Calendar
Full Version: Getchar() Problem In C
BleepingComputer.com > Software > Programming
   
Alan-LB
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 smile.gif

Alan
Yourhighness
Hi Alan,
been a while, but I recalled correctly. Pls habe a look at the getchar() part on the site provided.
http://www.friedspace.com/CTutorial2.html

rgds, Johannes
Alan-LB
I followed up your link. I added another getchar() so the program is now
....
int x;
.....
x = getchar();
getchar();
....
The second getchar() seems to "absorb" the '\n' from pressing ENTER

Now works fine Thanks for your help

Alan
Yourhighness
Hi Alan,
exactly! The second getch() is only for the return. There are methods using string, but its been now like 4 years since i have done programming ^^.

regards,
Happy_Reaper
The way it works is that getchar() reads a character from the input buffer. Therefore, if the buffer is empty, it waits for it to obtain values before it reads one. Input is only sent to the buffer from the keyboard after you press the enter key. That is why you are having problems.

So, when you input your first character, it is not yet placed in the buffer, so getchar doesn't read it. Then, when you press enter, both the previous character and the newline are placed in the buffer, so your first getchar would get you the desired character while your second one would get you a newline (which is why the above code snippet gets rid of it in order to avoid unexpected results).

For more info, see : http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

That is just a general FAQ of topics, you'll have to find the one that best suits your needs.

If you just want to get one character from the user, you could probably just use scanf and you would be fine.
Swandog46
scanf would work, if you make sure to flush the stdin buffer afterwards:

scanf("%c", &my_char);
fflush(stdin);

depending on your exact implementation, you might try getch() also, which is declared in curses.h:
http://www.opengroup.org/pubs/online/79087...s/curses.h.html
frealx
Hi young peoples,

I'm surprised - so many posts and no solution at all ?! No solution, because no one from above mentioned answers is not correct. For examle, this one:

QUOTE(Alan-LB @ May 13 2006, 01:37 AM) *
I followed up your link. I added another getchar() so the program is now
....
int x;
.....
x = getchar();
getchar();
....
The second getchar() seems to "absorb" the '\n' from pressing ENTER

Now works fine Thanks for your help

Alan


will work only if the user press the ONLY ONE KEY before pressing ENTER. The second getchar will "absorb" only the second character and that's it sad.gif
But the sting is that so many guys give advices but nothing more sad.gif So, voilą one way to take only one character from the prompt:
CODE
        int i;
        char choice='0', *pn;

        ...
        pn = &choice;
        while ( (atoi(pn) > i) || (atoi(pn) <= 0) ) // if you need any additional conditions
        {
            printf("Your choice [1"); if ( i > 1 ) printf("-%i", i); printf("] :");
            do scanf("%c",&choice); while ( getchar() != '\n' );
        }
        printf("Your choice was: %i",atoi(pn));


Good luck,

freealx
groovicus
In general, we try to not just give the people answers to their programming issues, but rather try to point them to resources that will help them find a solution on their own. Your solution is not necessarily any better than the one Alan-LB found on his own. And seeing how his specifications were to read a single letter character, your solution is actually contrary to the specifications, which makes your solution wrong.

It's all a matter of perspective, isn't it. smile.gif
petocities
QUOTE(groovicus @ Sep 26 2006, 05:25 PM) *
In general, we try to not just give the people answers to their programming issues, but rather try to point them to resources that will help them find a solution on their own. Your solution is not necessarily any better than the one Alan-LB found on his own. And seeing how his specifications were to read a single letter character, your solution is actually contrary to the specifications, which makes your solution wrong.

It's all a matter of perspective, isn't it. smile.gif


lol... how smooth... smile.gif
Actually, I came intending to help, but everything has been said, and I don't remember C that well anymore... so I'm kinda useless here, hehe...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.