Reviving a Dead Input Stream

E

Edward

When cin (or any other istream objects) tries to extract a value into
a wrong type it will not accept any other inputs anymore. All calls to
operator>>() will fail.

code:
int x;
cin >> x;
// when user types 'a' and hits enter cin is no longer functional.

Is there any way to revive an input stream after such an error?
 
S

Sharad Kala

Edward said:
When cin (or any other istream objects) tries to extract a value into
a wrong type it will not accept any other inputs anymore. All calls to
operator>>() will fail.

code:
int x;
cin >> x;
// when user types 'a' and hits enter cin is no longer functional.

Is there any way to revive an input stream after such an error?

std::cin.clear(); // Reinstate good bit
std::cin.ignore(..) // Extract invalid i/p
Finally the FAQ link -
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top