0
I have seen in several forums that you should use setbuf(stdin(NULL), or getchar I am new in C language and I am doubtful on this subject
0
I have seen in several forums that you should use setbuf(stdin(NULL), or getchar I am new in C language and I am doubtful on this subject
1
Problems with the keyboard buffer is usually caused by the scanf() function;(As far as I know so far).
A brief description of this "problem":
This problem only occurs (I may be wrong) when we want the program to read a character or a character set (char array), because we will always have to enter(' n') to "quit" the scanf function and this enter will be in the input buffer, but so what if enter was in the input buffer(stdin)? We must remember that the scanf function will always read what is stored in stdin, when we type enter it "closes" the scanf function but continues there, the next scanf(if any) will "catch" that enter and we have the problem, if we test this "error" reading integers will not occur because enter(' n') is not integer.
I will present two ways to solve this problem without adding any extra function to the code.
1 - The buffer problem only occurs from the second forward implementation, so we can add a space before the type indicator, scanf(" %c", &character);
2 - Add an additional parameter in the first scanf() that reads a character right after the parameter "main", scanf("%c%*c", &character);
In the first example I used a space (this will be useful later as in the sscanf() function because it will ignore all whitespace such as spaces, enters, tabs. Already in the second I used * because it will indicate to the program to ignore the data that should be there (the enter in the case).
I recommend reading this article: https://www.hardware.com.br/comunidade/vicios-devem/1302291/
To use all(or almost) the benefits that the scanf() function offers suggest searching for scansets.
And my explanation about buffers is not so good, research on too.
I guess that was it ;)
I just remembered, on the sites where I read about buffer problems they recommend avoiding the use of fflush() to clean the input buffer(stdin), because in this case the function fflush() has undefined behavior (according to the guys who maintain the link).
While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision
There, sorry for before I’m not used to using forums.
How to delete comments?
When hovering over the comment a mouse will appear X
to remove after your name. Take advantage and read: Stack Overflow in Portuguese is a forum?
0
Look, one way that my teacher used to clear the keyboard buffer for all kinds of reading was the fflush(stdin) and really I recommend you, because I used too much, and avoided too many bug when I was reading from the keyboard.
Browser other questions tagged c
You are not signed in. Login or sign up in order to post.
If you’re talking about
setvbuf
has to do with buferization and not clean things off the keyboard. Maybe you meanfflush(stdin)
, but although it works in some implementations it is not guaranteed that it will work in all.– Isac
I use setbuf always worked, use for many times the reading with space between the strings and it is always necessary to clean the Buff ... never had problem with setbuff at first in your example has a more simplified version of cleaning the Buff
– Maurício Z.B