cin.ignore() Function in C++
Last Updated :
22 Aug, 2024
The cin.ignore() function in C++ is a member function of the std::istream. It is used to ignore (or discard) certain number of characters from the input buffer. This function is very useful when we have to deal with leftover characters in the input stream that could interfere with subsequent input operations, such as newlines or other delimiters.
In this article, we will learn about the cin.ignore() function, its syntax, and practical examples to demonstrate its importance in input handling.
Syntax of cin.ignore() in C++
cin.ignore(count, delimiter);
Parameters of cin.ignore()
The cin.ignore() function in C++ accepts the following two parameters:
- count: (Optional) The maximum number of characters to ignore. The default is 1.
- delimiter: (Optional) The delimiter character at which the function stops ignoring characters. The default is the newline character '\n'.
Return Value of cin.ignore()
The cin.ignore() function does not return any value. Its main purpose is to discard characters from the input buffer up to the specified limit.
Examples of cin.ignore() in C++
The examples below demonstrate how to use the cin.ignore() function to handle unwanted input characters and ensure correct input in C++.
Example 1: Ignoring a Newline Character
C++
// C++ program to demonstrate the use of cin.ignore() function
// to discard the newline character after reading an integer.
#include <iostream>
using namespace std;
int main(){
int age;
char initial;
// Prompt the user to enter their age
cout << "Enter your age: ";
cin >> age;
// Use cin.ignore() to discard the newline character
cin.ignore();
// Prompt the user to enter the first letter of their name
cout << "Enter the first letter of your name: ";
cin >> initial;
// Display the entered age and initial
cout << "Your age is " << age << " and your initial is " << initial << endl;
return 0;
}
Output
Enter your age: 10
Enter the first letter of your name: G
Your age is 10 and your initial is G
Time Complexity: O(n), where n is the number of characters specified by the count parameter or the position of the delimiter.
Auxiliary Space: O(1) as it does not involve any additional memory allocation.
Explanation: In the above example, we used the cin.ignore() to discard the newline character left in the input buffer after reading the integer age, ensuring that the next input (for initial) is handled correctly.
Example 2: Ignoring Multiple Characters
C++
// C++ program to demonstrate the use of cin.ignore() function
// to skip multiple characters in the input stream.
#include <iostream>
using namespace std;
int main(){
cout << "Enter a sentence: ";
// Use cin.ignore() to skip up to 10 characters or until a space is encountered.
cin.ignore(10, ' ');
char nextChar;
// Read the next character from the input stream
cin >> nextChar;
// Output the character that was read after ignoring the specified characters
cout << "Next character after ignoring: " << nextChar << endl;
return 0;
}
Output
Enter a sentence: Hellowelcometo geeksforgeeks
Next character after ignoring: m
Time Complexity: O(n), where n is the number of characters specified by the count parameter or the position of the delimiter.
Auxiliary Space: O(1) as it does not involve any additional memory allocation.
Explanation: In the above example, cin.ignore(10, ' ') is used to skip up to 10 characters or until a space is encountered, that allows the program to process the next meaningful input.
Similar Reads
cin.clear() function in C++ The cin.clear() function in C++ is a member function of the std::istream class, which is used to clear the error flags on the cin stream. When an input operation fails (for example, due to incorrect data type input), the cin stream enters a fail state, and further input operations are blocked until
4 min read
erase_if() Function in C++ The std::erase_if() is a utility introduced in C++20 that is used to remove elements from containers based on a specified condition. This function erases all elements that satisfy a given predicate from standard containers like std::vector, std::deque, std::list, std::forward_list, std::string, and
3 min read
How to Use the ignore() Function in C++? In C++, the ignore() function is a part of std::basic_istream that is used to discard the characters in the stream until the given delimiter(including it) is reached and then extracts the left-out remainder. In this article, we will learn how to use the ignore() function in C++. C++ ignore() Functio
2 min read
How to Use cin.fail() Method in C++? In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp
2 min read
How to Use Deleted Functions to Prevent Object Copying in C++? In C++, the class always has a copy constructor and assignment operator, whether it is default or user-defined which allows the program to create copies of the objects of that class. But sometimes, we need to create a class whose object should not be copied. In this article, we will learn how to use
2 min read