handle cin errors

N

Nafai

Hello. I need to handle cin errors like these:

....
int n;
cout << "Type a number: ";
cin >> n;
// Check user actually typed a number.
....


CONSOLE:

Type a number: asdf >ENTER
....
Programs does strange things
....


How can I do that?

Thanks!
 
V

Victor Bazarov

Nafai said:
Hello. I need to handle cin errors like these:

...
int n;
cout << "Type a number: ";
cin >> n;
// Check user actually typed a number.
...


CONSOLE:

Type a number: asdf >ENTER
...
Programs does strange things
...


How can I do that?

Read the FAQ (http://www.parashift.com/c++-faq-lite/), your question
and an answer has been put there.

V
 
E

E. Robert Tisdale

I couldn't tell which FAQ you were talking about
and I don't think that Nafai could either.
[15.3] How can I get std::cin to skip invalid input characters?

[5.5] What do I do if someone else posts a question
that's already in the FAQ?

And see note #1.

Note #1: Please don't give them the location
of the appropriate FAQ. E.g., don't say,
"Look at FAQ [10.3]" or "Look in section [10]".
It's the old give-them-a-fish vs. teach-them-to-fish problem.

This is bad advice. It contradicts

Note #2: Please be polite. I'm hoping we can avoid
"RTFM" or "RTFFAQ" euphemisms (or worse!).

Please *don't* send new subscribers on a "wild goose chase".
Cite and quote the relevant FAQ
just as if you were citing and/or quoting a relevant passage
from the ANSI/ISO C++ standards documents.
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top