A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/ranges/../../algorithm/../io/basic_istream/ignore.html below:

std::basic_istream<CharT,Traits>::ignore - cppreference.com

basic_istream& ignore( std::streamsize count = 1, int_type delim = Traits::eof() );

Extracts and discards characters from the input stream until and including delim.

ignore behaves as an UnformattedInputFunction. After constructing and checking the sentry object, it extracts characters from the stream and discards them until any of the following conditions occurs:

[edit] Parameters count - number of characters to extract delim - delimiting character to stop the extraction at. It is also extracted [edit] Return value

*this

[edit] Exceptionsfailure

if an error occurred (the error state flag is not

goodbit

) and

exceptions()

is set to throw for that state.

If an internal operation throws an exception, it is caught and badbit is set. If exceptions() is set for badbit, the exception is rethrown.

[edit] Example

The following example uses ignore to skip over non-numeric input:

#include <iostream>
#include <limits>
#include <sstream>
 
constexpr auto max_size = std::numeric_limits<std::streamsize>::max();
 
int main()
{
    std::istringstream input("1\n"
                             "some non-numeric input\n"
                             "2\n");
    for (;;)
    {
        int n;
        input >> n;
 
        if (input.eof() || input.bad())
            break;
        else if (input.fail())
        {
            input.clear(); // unset failbit
            input.ignore(max_size, '\n'); // skip bad input
        }
        else
            std::cout << n << '\n';
    }
}

Output:

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior LWG 172 C++98 the type of count was misspecified as int corrected to std::streamsize [edit] See also extracts characters
(public member function) [edit] extracts characters until the given character is found
(public member function) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4