How to Use the ignore() Function in C++? Last Updated : 23 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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() Function In C++, we can invoke the ignore() function with two arguments: the number of characters to discard and the delimiter character. This function discards characters from the input stream until a specified delimiter is encountered, including the delimiter itself or the specified number of characters have been ignored. The remaining characters in the stream are then extracted. Syntax to use ignore() Function in C++istream& ignore(size n, int delim = EOF);Here, n is the maximum number of characters to extract.delim is the delimiter character.EOF value for delim means that it will ignore characters until EOF is encountered or the limit set n is reached.C++ Program to Demonstrate the Use of ignore() FunctionThe below example demonstrates the use of the ignore() function with cin to store only 3 numbers and discard any other user input. C++ // C++ program to demonstrate the use of ignore() #include <iostream> #include <limits> using namespace std; int main() { // infinite loop until user input is terminated. while (true) { // Declare variables to store input numbers. int num1, num2, num3; // Prompt the user to type numbers separated by // spaces. cout << "Type numbers separated by spaces: " << endl; // Read input numbers cin >> num1 >> num2 >> num3; // Check if the first number is 0 to exit the // program. if (num1 == 0) exit(EXIT_SUCCESS); // Ignore any remaining characters in the input // buffer until newline character. cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Print the input numbers. cout << num1 << "; " << num2 << "; " << num3 << endl; } return EXIT_SUCCESS; } Output Type numbers seperated by spaces: 1 2 3 6 4 5 9 8 71; 2; 3Type numbers seperated by spaces: 7 8 9 4 57; 8; 9Type numbers seperated by spaces: Time Complexity: O(1)Auxilliary Space: O(1) Explanation: In the above example we used numeric_limits<std::streamsize>::max() as the first argument of the ignore function for telling the function to ignore as many characters as possible until it reaches the delimiter. Comment More infoAdvertise with us Next Article How to Use the Not-Equal (!=) Operator in C++? K kolisuszprv Follow Improve Article Tags : C++ Programs C++ CPP-Functions CPP Examples Practice Tags : CPP Similar Reads cin.ignore() Function in C++ 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 o 3 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 How to use errno in C++? In C++ errno is a preprocessor macro that is used for error indication and reporting errors that occur during a function call. It contains error codes so if a call to a function fails somehow then the errno is set to a value that corresponds to the error. errno is defined in the header file <cerr 4 min read How to Use the Not-Equal (!=) Operator in C++? The not-equal operator is a fundamental comparison operator in C++ represented by "!=". It is used for making decisions in programming and is hence called a conditional operator. In this article, we will discuss how to use the Not-Equal (!=) operator in C++. Not-Equal (!=) Operator in C++The not-equ 3 min read How to Utilize the goto Statement in C++? The goto statement in C++ is a control flow statement that allows the users to move the control flow from one part to another part of the program. In this article, we will learn how to utilize the goto statement in C++. Utilize the goto Statement in C++In C++, the goto statement transfers the progra 2 min read Like