To disregard or remove single or maybe more letters from the input buffer using the cin.ignore() method. Maybe we need to remove the undesirable buffer so that the next data is stored in the intended container rather than the preceding variable’s cache. For instance, we must provide a character array or string after inputting the cin command. As a result, we must empty the input buffer; else, the buffer of the preceding variable would be occupied. Because the cache of the preceding element lacks room to retain fresh data, hitting the “Enter” button just after the initial input ignores the container’s next input. Let’s start with the examples of the cin.ignore() function with the launch of new C++ files through the terminal shell. The file must be created through the touch instruction and the file’s name. Ubuntu’s “nano” editor has been used so far to open the file to edit.
Example 01:
On applying the nano command, ignore.cc file would be opened in the editor. It’s time to start our first example. Firstly, we will look at the c++ code without using the cin.ignore() function. So, we have started it with two header libraries, i.e., vector and iostream. The namespace is utilized after that. The main() function is started with the declaration of an integer variable “num” and character type array “Str” of size 100 used to store some string value. The cout statement asks a user to add one integer and one string type value. The cin statement is used to put the integer value to variable “num,” and cin.getline() is used to get the string type values entered by a user to save it in the “Str” variable up to 100 characters. The next cout statement is utilized to print out the integer and string value entered by the user on the shell.
#include<vector>
using namespace std;
main() {
int num;
char Str[100];
cout << "Please add one Integer and one String value:\n";
cin >> num;
cin.getline(Str,100); //take a string
cout << "Number :" << num << ", String: " << Str << endl;
}
Now, the code has been compiled and executed. The user has entered two values separated by a space, i.e., integer and string. Both the values have been printed out as specified.
Upon executing again, the User has added 1 value and pressed Enter. The output shows one value in return, and the string value is empty.
On the third execution, a user has added a string value without adding an integer value. In return, the output shows the 0 value for number and NULL for string.
Let’s update the code by adding the “cin.ignore()” function after the cin statement. It takes two arguments. One is numeric_limits stream size header to clear the buffer cache after 1st value and a compelling new line i.e. “\n”. So, the next variable “Str” will get the value after the user gets to the next line.
#include<vector>
using namespace std;
main() {
int num;
char Str[100];
cout << "Please add one Integer and one String value:\n";
cin >> num; //clear buffer before taking new line
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.getline(Str,100); //take a string
cout << "Number :" << num << ", String: " << Str << endl;
}
A user has added two values with a space. After pressing Enter, a user has added another value. As a result, the output shows the first integer value and the string value-added on the next line. The cin.ignore() will take the first value before the space and the second value from the next line after clearing the buffer cache. You can see, it has ignored the string value “Ignore” value from the first input line.
Example 02:
Now, we have been taking a look at another example. Some header files have been added before the main() function. Within a function, the while loop continues to execute some statements. Three integer variables are declared, and the cout statement asks to add input in those variables. The cin statement saves the input values to variables, and the “if” statement is used to check if the first value is equal to 0 or not. If yes, it will exit the program immediately. Otherwise, the program continues. The cin.ignore() statement is here to clear the cache before the next line, and the cout statement will display the inputted values.
#include <sstream>
#include <limits>
#include <vector>
#include <fstream>
using namespace std;
int main() {
while(true) {
int n1, n2, n3;
cout << "Please Type Space-separated 3 Numbers: " << endl;
cin >> n1 >> n2 >> n3;
if (n1 == 0) exit(EXIT_SUCCESS);
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout <<"1st: "<<n1<<" , 2nd:"<<n2<<" , 3rd: "<< n3<<endl;
}
return EXIT_SUCCESS;
}
After executing this code example, the user has added three values. The first value is not 0; it will display the 1st, 2nd, and 3rd. The “while” loop continues again, and the user adds 0 as 1st integer value. In return, the program exited without further execution.
Example 03:
The last example is taking two-character type variables as input from the user. Between both cin.get() lines to take values, we have utilized the cin.ignore() function to clear buffer cache when encountering the space. If the user adds any string, cin.get() will only take its initial and cout will display it as per the below code.
#include <sstream>
#include <limits>
#include <vector>
#include <fstream>
using namespace std;
int main() {
char v1, v2;
cout << "Type Two words: " << endl;
v1 = cin.get();
cin.ignore(numeric_limits<std::streamsize>::max(), ' ');
v2 = cin.get();
cout << "Initials of both words are: "<<v1<<v2<<endl;
return EXIT_SUCCESS;
}
After the execution, the user added two words while the cout statement displays only initials.
Conclusion:
So, this was all about the use of the cin.ignore() function in C++ to clear cache buffers. We have used the space and next line characters as delimiters. The implemented examples are quite easy to learn and understand. Therefore, we are sure that you will take help from all examples.