+ 1

Buffer

How to use .sync() and .clear() and how does it work in a program

17th Apr 2020, 11:10 AM
Michael Oguidan
Michael Oguidan - avatar
4 RĂ©ponses
+ 6
What is a buffer? A temporary storage area is called buffer. All standard input and output devices contain an input and output buffer. In standard C/C++, streams are buffered, for example in the case of standard input, when we press the key on keyboard, it isn’t sent to your program, rather it is buffered by operating system till the time is allotted to that program. How does it effect Programming? On various occasions you may need to clear the unwanted buffer so as to get the next input in the desired container and not in the buffer of previous variable. For example, in case of C after encountering “scanf()” , if we need to input a character array or character ,and in case of C++, after encountering“cin” statement, we require to input a character array or a string , we require to clear the input buffer or else the desired input is occupied by buffer of previous variable, not by the desired container.On pressing “Enter” (carriage return) on output screen after the first input , as the buffer of previous var
27th May 2020, 10:33 AM
Ashutosh k.
Ashutosh k. - avatar
+ 5
In case of C++ : Using “ cin.ignore(numeric_limits::max(),’\n’); ” :- Typing “cin.ignore(numeric_limits::max(),’\n’);” after the “cin” statement discards everything in the input stream including the newline.
27th May 2020, 10:33 AM
Ashutosh k.
Ashutosh k. - avatar
+ 5
Using “ cin.sync() ” : Typing “cin.sync()” after the “cin” statement discards all that is left in buffer. Though “cin.sync()” does not work in all implementations
27th May 2020, 10:34 AM
Ashutosh k.
Ashutosh k. - avatar
0
Thank you very much
28th May 2020, 7:02 PM
Michael Oguidan
Michael Oguidan - avatar