C++ basic_ios Library - ignore


It is used to extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.


Following is the declaration for std::basic_istream::ignore.

basic_istream& ignore (streamsize n = 1, int_type delim = traits_type::eof());


  • n − Maximum number of characters to write to s (including the terminating null character).

  • delim − Explicit delimiting character: The operation of extracting successive characters stops as soon as the next character to extract compares equal to this (using traits_type::eq).

Return Value

Returns the basic_istream object (*this).


Basic guarantee − if an exception is thrown, the object is in a valid state.

Data races

Modifies the stream object.


In below example for std::basic_istream::ignore.

#include <iostream>     

int main () {
   char first, last;

   std::cout << "Please, enter your first name followed by your surname: ";

   first = std::cin.get();     
   std::cin.ignore(256,' ');   

   last = std::cin.get();      

   std::cout << "Your initials are " << first << last << '\n';

   return 0;

The output should be like this −

Please, enter your first name followed by your surname: John Smith
Your initials are JS