cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Catching bad input
Catching bad input
Sep 19, 2011 at 7:36pm UTC
YellowDog
(3)
int x;
cin >> x;
Is there any way to detect that the user entered an non numeric value. Seems if I feed cin "Hello" it assigns 0 to x; I was expecting to use some sort of try/catch block to detect this type of error.
Sep 19, 2011 at 8:24pm UTC
Galik
(2254)
1
2
3
4
5
6
int
x;
if
(cin >> x) {
// read was successful (user typed an int)
}
Sep 19, 2011 at 8:27pm UTC
Duthomhas
(13206)
Check for !good() and respond appropriately. Don't forget that the user will press the ENTER key after
every
input.
http://www.cplusplus.com/forum/beginner/18258/#msg92955
http://www.cplusplus.com/forum/beginner/15655/#msg77321
http://www.cplusplus.com/forum/beginner/13044/#msg62827
Good luck!
Sep 19, 2011 at 9:20pm UTC
YellowDog
(3)
Thanks that worked.
Topic archived. No new replies allowed.