View Single Post
  #2 (permalink)  
Old May 11th, 2006, 11:46 PM
C@uark C@uark is offline
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