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.
|