
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is the use of cin.ignore() in C++?
The cin.ignore() function is used which is used to ignore or clear one or more characters from the input buffer.
To get the idea about ignore() is working, we have to see one problem, and its solution is found using the ignore() function. The problem is like below.
Sometimes we need to clear the unwanted buffer, so when next input is taken, it stores into the desired container, but not in the buffer of 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 previous variable. By pressing the “Enter” key after the first input, as the buffer of previous variable has space to hold new data, the program skips the following input of container.
Example
#include<iostream> #include<vector> using namespace std; 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; }
Output
Enter a number and a string: 8 You have entered: 8
There are two cin statements for integer and string, but 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 −
cin.ignore(numeric_limits::max(), ‘\n’)
Then it ignores input including the new line character also.
Example
#include<iostream> #include<ios> //used to get stream size #include<limits> //used to get numeric limits using namespace std; main() { int x; char str[80]; cout << "Enter a number and a string:\n"; cin >> x; cin.ignore(numeric_limits<streamsize>::max(), '\n'); //clear buffer before taking new line cin.getline(str,80); //take a string cout << "You have entered:\n"; cout << x << endl; cout << str << endl; }
Output
Enter a number and a string: 4 Hello World You have entered: 4 Hello World
- Related Articles
- What is the use of MySQL IGNORE INSERT statement?
- What is the difference between cin and cout streams in c++?
- What are cin, cout and cerr streams in C++?
- Standard Input Stream (cin) in C++
- How do I clear the cin buffer in C++?
- What is the use of "is" keyword in C#?
- What are the mistakes in CRM Security You Should Never Ignore?
- What is the use of sinon.js?
- What is the use of NCHAR in MySQL?
- What is the use of %n in printf()?
- What is the use of OBJECT.assign() in javascript?
- What is the use of $ErrorView in PowerShell?
- What is the use of Action in JSP?
- What is the use of tag in JSP?
- What is the use of tag in JSP?
