Horje
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() 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() Function

The 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 7
1; 2; 3
Type numbers seperated by spaces: 7 8 9 4 5
7; 8; 9
Type 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.




Reffered: https://www.geeksforgeeks.org


C++

Related
Difference Between Struct and Typedef Struct in C++ Difference Between Struct and Typedef Struct in C++
How to Round a Double to an int in C++? How to Round a Double to an int in C++?
How to Declare a Global Variable in C++? How to Declare a Global Variable in C++?
C++ Program to Implement Stack using array C++ Program to Implement Stack using array
How to Convert Char Array to Int in C++ How to Convert Char Array to Int in C++

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13