+ 1
Buffer
How to use .sync() and .clear() and how does it work in a program
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
+ 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.
+ 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
0
Thank you very much