i want 2 use the ignore function 2 ignore some of my inputs data, but i dont know how
i'm using C++

Dani AI

Generated

The replies in this thread correctly zeroed in on the standard istream facility: ignore discards characters left in the input buffer. The short notes below fill in precise behavior, common pitfalls, and safer patterns that avoid magic constants. Mentions in the thread by , , and are consistent with the discussion: ignore is an istream member and "flushing the input buffer" usually means discarding leftover characters from the input stream.

basic_istream::ignore has the signature that accepts a count (std::streamsize) and a delimiter (int_type) with defaults. It extracts and discards characters until either the requested count is reached, the delimiter is found (the delimiter is also extracted and discarded), or EOF is reached; EOF sets the stream’s eofbit. When the count equals std::numeric_limits<std::streamsize>::max() the count check is effectively disabled and only the delimiter (or EOF) stops the operation. (en.cppreference.com)

A robust way to drop the rest of the current input line (instead of using an arbitrary number like 100) is to use std::numeric_limits<std::streamsize>::max() together with the newline delimiter. This is also the recommended approach when switching between formatted extraction (operator>>) and line-oriented input (std::getline), or when discarding any trailing junk on the current line. Example patterns:

#include <limits>

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Use std::getline or std::ws when actual line content is needed. (cppreference.com)

When a formatted extraction fails (for example attempting to read an int and the token is non-numeric) the stream sets failbit. Clearing the error flags and discarding the rest of that line re-synchronizes the stream before continuing; clear() clears flags and ignore() removes the offending input. Also prefer extraction loops that test the extraction directly (for example while (std::cin >> a >> b >> c)) rather than testing while (std::cin) after reads. (en.cppreference.com)

Summary: ignore discards characters (it does not reset error flags), use numeric_limits::max() plus '\n' to drop an entire line reliably, clear error flags with clear() after a failed extraction, and prefer extraction-checked loops to avoid processing bad data. (en.cppreference.com)

Recommended Answers

All 8 Replies

Member Avatar for Member #46692

You've heard of if / else statements haven't you?

Try using them:

if (good)
  then
    //do stuff
  endif

elseif (bad)
   then
     //ignore and carry on
   endif

yes of course i know the if statment.
there's an ignore function
ignore (.... , .... );
this function 2 ignore some data from the input stream.
u can choose 3 or 4 chars for example 2 ignore them . i'm asking how i can use it. with an example

Member Avatar for Member #46692

Huh, do you have a better example?

ignore() is member of the istream class. It will ignore char stored in the input stream buffer. It is commonly used as part of an overall data validation process, but it can be used in other circumstance as well.

I agree, a better description/example of what the OP is trying to do will make it more apparent whether the istream ignore() method is what they want or whether they would be better writing a parsing algorhithm of sort, etc., as iamthwee initially suggested.

there's an ignore function
ignore (.... , .... );
this function 2 ignore some data from the input stream.

It looks like they mean the standard cin.ignore function.

Why do you need to ignore three chars? Do you want to flush the input buffer?

i found it
it's ignore(int,char)
the 1 st parameter is the number of inputs that 'll b ignored.
the 2nd is the char which the input will b ignored till this char.
which comes first.

in my program i used cin.ignore(100,'\n');
and so i ignored the rest of the line.


in fact i need 2 read the 1 st 3 inputs from each line and the rest of the line in neglected so i do the flowing:

int i,j,k;
while(cin)
{
cin>>i>>j>>k;
cin.ignore(100,'\n');
}


it works good
each line i got the right inputs with neglecting the rest of the line.
my input stream is something like that:
6 8 9 khjgaguw gvmdvjfvnjnv sjbsdhkb.
8 66 0 b dbnfdhbfdjknbdmh9

what is the flash input buffer?
if u don't mind 2 explain for me

what is the flash input buffer?
if u don't mind 2 explain for me

He said "Flush the input buffer" meaning empty all the data from the input stream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.