std::basic_istream::ignore in C++ with Examples

The std::basic_istream::ignore is used to extracts characters from the input string and discards them including delimiting character, i.e., if the end of the file is reached this function stops extracting characters. The delimiting character is the new line character i.e ‘\n’. This function will also stop extracting characters if the end-of-file is reached if the input is taken using a file. This function accesses the input sequence by first constructing a sentry object. It extracts characters from its associated stream buffer object and destroys the sentry object before returning.

Header File:

#include <iostream>

Syntax:

istream& ignore(size N,
            int delim = EOF);

Parameters: It accepts the following parameters:

  • N: It represent maximum number of characters to extract.
  • delim: It is used for where stop the extraction.

Return Value: It returns the basic_istream object.



Below are the programs to demonstrate basic_istream::ignore():

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// basic_istream::ignore
  
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
  
    // Input String
    istringstream input(
        "12\n"
        "It is a string\n"
        "14\n");
  
    for (;;) {
  
        int n;
  
        // Taking input streamed string
        input >> n;
  
        // Check for end of file or if
        // any bad  bit occurs
        if (input.eof() || input.bad()) {
            break;
        }
  
        // If any failbit occurs
        else if (input.fail()) {
  
            // Clear the input
            input.clear();
  
            // Use ignore to stream the given
            // input as per delimeter '\n'
            input.ignore(
                numeric_limits<streamsize>::max(),
                '\n');
        }
  
        // Else print the integer in
        // the string
        else {
            cout << n << '\n';
        }
    }
    return 0;
}

chevron_right


Output:

12
14

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// basic_istream::ignore
  
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
  
    char first, last;
  
    cout << "Enter a String: ";
  
    // Get one character
    first = cin.get();
  
    // Ignore string untill space occurs
    cin.ignore(256, ' ');
  
    // Get one character
    last = std::cin.get();
  
    cout << "Your initials are "
         << first << ' '
         << last << '\n';
  
    return 0;
}

chevron_right


Output:

Reference: http://www.cplusplus.com/reference/istream/basic_istream/ignore/

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.