
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Clear the cin Buffer in C++
Before knowing the solution to clearing the cin buffer in C++, let us first see what the buffer is in C++.
What is The Buffer in C++
A buffer is a temporary storage area that holds data while standard I/O devices are in use. In C++, a stream also work as a buffer. When a key is pressed, the input is not immediately sent to the program; instead, it is stored in a buffer. The operating system keeps this data in the buffer until the program is allocated time to process it.
Why We Clear The cin Buffer in C++
Sometimes we need to clear the unwanted buffer so that when the next input is taken, it is stored in the desired container, but not in the buffer of the previous variable. For example, after entering into the cin statement, we need to input a character array or string. So we need to clear the input buffer, otherwise, it will occupy the buffer of the previous variable. By pressing the "Enter" key after the first input, as the buffer of the previous variable has space to hold new data, the program skips the following input of the container.
Example
This is simple console-based input/output example, which demonstrates how to read an integer and a string from the user ?
#include<iostream> #include<vector> using namespace std; int main() { int x; char str[80]; cout << "Enter a number and a string:\n"; cin >> x; cin.getline(str,80); //take a string cout << "You have entered:\n"; cout << x << endl; cout << str << endl; }
Following is the output of the above code ?
Enter a number and a string: 8 You have entered: 8
There are two cin statements for integer and string, but the only number is taken. When we press enter key, it skips the getLine() function without taking any input. Sometimes it can take input but inside the buffer of integer variable, so we cannot see the string as output.
Now to resolve this issue, we will use the cin.ignore() function: This function is used to ignore inputs upto given range. If we write the statement like this. Then it ignores input including the new line character also.
cin.ignore(numeric_limits::max(), ?\n')
Example
The following program takes an integer and a full line string input from the user. It clears the input buffer using cin.ignore() to ensure getline() works correctly after cin >>.
#include <iostream> #include <limits> // For numeric_limits using namespace std; int main() { int x; char str[80]; cout << "Enter a number and a string:\n"; cin >> x; // Clear the newline character left in the buffer cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Take a line of input (including spaces) into the character array cin.getline(str, 80); cout << "You have entered:\n"; cout << x << endl; cout << str << endl; return 0; }
Following is the output of the above code ?
Enter a number and a string: 10 tutorialspoint You have entered: 10 tutorialspoint
Example
In this example clearing the cin
buffer even though there is no real/user input in it ?
#include <iostream> #include <limits> using namespace std; int main() { int x = 42; char str[80] = "Hello, this is a tutorialspoint India."; cout << "You have entered:\n"; cout << x << endl; cout << str << endl; // Using the cin.ignore function cin.ignore(numeric_limits::max(), '\n'); cout << "cin buffer cleared (conceptually)." << endl; return 0; }
The above code generates the following output ?
You have entered: 42 Hello, this is a tutorialspoint India.