C++ std::istream::ignore() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, ignore() reads characters from an input stream and throws them away. You choose when it stops: after a number of characters, or when it has removed a chosen delimiter such as the new line.

It is the standard way to clean up the input buffer, for example to remove the leftover Enter key after cin >> number. Include <iostream> to use it.

This is a member function of std::istream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call ignore on an input stream. Both arguments are optional.

C++
in.ignore();                      // skip one character
in.ignore(5);                     // skip five characters
in.ignore(1000, '\n');            // skip up to and including '\n'

#include <limits>
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
CallWhat it does
in.ignore()Skips exactly one character.
in.ignore(n)Skips up to n characters.
in.ignore(n, '\n')Skips characters until it has removed a new line, or after n characters, whichever comes first.

For a limit that can never be too small, use std::numeric_limits<std::streamsize>::max() from <limits>.

Library declarations
C++
istream& ignore(streamsize count = 1, int_type delim = EOF);

Parameters

ParameterDescription
countOptional. The largest number of characters to skip. The default is 1.
delimOptional. A character that stops the skipping. The delimiter itself is also removed. The default is EOF, which means no delimiter.

Return Value

Return ValueDescription
istream&A reference to the stream, so calls can be chained.

C++ Examples

Example 1 — Skip a Number of Characters

Skip the first three letters and read the fourth one.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("abcdef");

    in.ignore(3);

    char c = in.get();
    std::cout << "next character: " << c << '\n';
    return 0;
}

How It Works

  1. in.ignore(3) removes a, b, and c from the stream without storing them.
  2. The read position is now at d.
  3. in.get() reads that character, so the program prints d.

Example 2 — Skip the Rest of a Line

Use the new line as the delimiter, then read the second line.

C++
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::istringstream in("first line\nsecond line\n");
    std::string line;

    in.ignore(100, '\n');
    std::getline(in, line);

    std::cout << line << '\n';
    return 0;
}

How It Works

  1. ignore(100, '\n') skips characters until it has removed the first new line. The limit 100 is just a safe maximum.
  2. The whole first line is gone, new line included.
  3. getline() reads from the start of the second line, so only second line is printed.

Example 3 — Skip a Comment Behind a Number

Read a number, ignore the comment on the same line, then read the next number.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("10 # first value\n20 # second value\n");
    int a = 0;
    int b = 0;

    in >> a;
    in.ignore(100, '\n');
    in >> b;

    std::cout << "a = " << a << '\n';
    std::cout << "b = " << b << '\n';
    return 0;
}

How It Works

  1. in >> a reads 10 and stops at the space. The comment text is still waiting.
  2. ignore(100, '\n') removes the comment and the new line at its end.
  3. The next in >> b starts at 20. Without ignore() it would meet the # sign and fail.

Frequently Asked Questions

It reads characters from cin and discards them. With no arguments it skips one character. With a count and a delimiter it skips until the delimiter has been removed or the count is reached.
cin >> x leaves the Enter key (a new line) in the buffer. Skipping it lets a following getline() read the real next line.
It is the largest possible count, so ignore() will skip everything up to the delimiter, however long the line is. It needs #include <limits>.
Yes. The delimiter is read and discarded together with the characters before it.
A failed stream refuses all reading. Call cin.clear() first to reset the flags, then call cin.ignore().

Next: is_open()

Check whether a file stream is connected to a file.

is_open() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful