Howto use getchar() multiple times?
Hi,
Can someone tell me why the second getchar(); in the following code is skipped, and can you show me what would be the correct way of doing this? Code:
#include <stdio.h> Regards, Ben |
Not that there are no issues with your program, but what makes you feel the second getchar() is skipped given the fact you do not get its return value.
|
Given your rather short post, I can only guess that you don't realize that the new line entered when you press return is a character as well and that it is what the second getchar is returning. If this is not what your talking about, we're going to need a short example (ie. input, expected output, actual output) or something, because it should work otherwise.
|
Quote:
Thanks for your help guys. Regards, Ben |
Beware that your program is using the default cooked (buffered) tty mode, which is inconvenient with getchar. You may want to have a look at this thread about the issue and some solutions to fix it: http://www.linuxquestions.org/questi...d.php?t=122663
|
Thanks jlliagre :). I am going to look at this tommorow.
Ben |
Something you might additionally consider is a SIGCONT signal handler since pausing a program in a shell will likely cause the shell to turn ICANON back on, severely screwing up your program:
Code:
#include <stdio.h> Note that the SIGTERM handler won't catch a [Ctrl]+C press, which generates a SIGKILL signal. Luckily Bash will reset to canonical mode itself, but it's polite to have the SIGTERM handler in there anyway. Take a look at tcgetattr, struct termios, and signal in the libc infopages for a whole lot more info on this. ta0kira |
Thanks ta0kira.
I will take a look at this. Regards, Ben |
ํAs 95se said, the 'enter' key is also stored in the input-buffer so when the second call of getchar() come to play, it will be skipped. You can fix this by call fflush(stdin) -which means clear the input-buffer out, placing this function between the 2 calls.
|
Avoid fflush(stdin) or any input stream. The behaviour of such a command is undefined according to the C standard.
http://www.iso-9899.info/wiki/Implementation |
I have looked at cooked mode and I have tried the tcflush function, but I cannot get it working. Can someone please provide working example code for me please, and explain what is happening?
I want someone to be able to enter a character whereafter he should use the enter key to enter the value, and I want to do this twice in a row like this: Code:
#include <stdio.h> |
if you want the user to press the enter key after each character, then you can stay in cooked mode. Just call getchar() twice, one for the actual character and the second one to pick the return.
|
Great! Thanks jlliagre.
Salut Ben |
Quote:
|
Quote:
I would just use gets to obtain an entire line, then strlen it and use the first character if it's longer than 3. Using 2 getchars in non-canonical mode won't do what you want. Whether the input be a then [Enter] or a then [Backspace] you will still have an uncontrolled input operation. ta0kira |
All times are GMT -5. The time now is 03:10 PM. |