Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 10th, 2006, 10:55 PM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default getchar() problem

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



Reply With Quote
  #2 (permalink)  
Old May 11th, 2006, 11:46 PM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

flush the input buffer before getchar() i.e.

int main()
{
    char x; //Input character
    while (1)
    {
        printf(">"); //Print prompt '>'
        fflush(stdin); // need to flush the stdin buffer
        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);
    }
}
this will flush the stdin buffer before a call to getchar. getchar will retrieve form the buffer if your in put was 'a' and then enter '\n', the '\n' was inserted in to the buffer. on the next successive call to getchar it will retrieve '\n', so flush before getchar to flush the '\n' or any other garbage that you may not want out of the buffer.
Reply With Quote
  #3 (permalink)  
Old May 13th, 2006, 12:39 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

fflush() didn't work. I changed the program by adding a second getchar() -
...
char x;
...
x = getchar();
getchar();
...
The second getchar() seems to "absorb" the '\n' from pressing the ENTER key. Program works fine now.

Thanks for your help anyway

Alan


Reply With Quote









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.