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.
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 variable was the space for new container(as we did’nt clear it) , the program skips the following input of container.
Replacement of the electronic control panel on a new Whirlpool refrigerator John - Cincinnati, OH 11 days ago. Review of A to Z Appliance Service Our control panel was not working correctly immediately after the refrigerator was installed. We called for a replacement and a repair person was sent soon after.
In case of C Programming
// clearing the input buffer #include<stdio.h> { // GeeksforGeeks for example // 'a' for example // prints “GeeksforGeeks”) // character 'a' } |
Input:
Output:
In case of C++
// not clearing the input // outputs #include<vector> { char ch[80]; // Enter input from user cin >> a; // Get input from user - cin.getline(ch,80); // Prints 4 // not print string } |
Input:
Output:
In both the above codes, the output is not printed as desired. Reason to this is an occupied Buffer. The “n” character goes remains there in buffer and read as next input.
How can it be resolved?
In case of C :
- Using “ while ((getchar()) != ‘n’); ” : Typing “while ((getchar()) != ‘n’);” reads the buffer characters till the end and discards them(including newline) and using it after the “scanf()” statement clears the input buffer and allows the input in the desired container.
// 'while ( (getchar()) != 'n');'
// flushes the input buffer
{
// GeeksforGeeks for example
// (clears the input buffer)
// 'a' for example
// prints “GeeksforGeeks”)
// will print 'a' this time
}
Input:Output:
- Using “ fflush(stdin) ” : Typing “fflush(stdin)” after “scanf()” statement also clears the input buffer but use of it is avoided and is termed to be “undefined” for input stream as per the C++11 standards.
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.
// 'cin.ignore(numeric_limits
// discards the input buffer
#include<ios>
// for numeric_limits
using
namespace
std;
int
main()
int
a;
// - 4 for example
cin.ignore(numeric_limits<streamsize>::max(),
'n'
);
// Get input from user -
cin.getline(str, 80);
// Prints 4
// will print string now
}
Input:Output:
- 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 (According to C++11 and above standards).
// discards the input buffer
#include<ios>
using
namespace
std;
int
main()
int
a;
// - 4 for example
cin.sync();
// Get input from user -
cin.getline(str, 80);
// Prints 4
// will print string now
}
Input:Output:
- Using “ cin >> ws ” : Typing “cin>>ws” after “cin” statement tells the compiler to ignore buffer and also to discard all the whitespaces before the actual content of string or character array.
// discards the input buffer along with
#include<vector>
{
string s;
// Enter input from user -
cin >> a;
// Discards the input buffer and
cin >> ws;
// Get input from user -
getline(cin, s);
// Prints 4 and GeeksforGeeks :
cout << a << endl;
}
Input:Output:
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.