How do I find the reason for a (!cin)?

Suppose an input failed.

1
2
3
  int a;
  cin >> a;
  if(!cin) cout << "Input failed";

The program outputs "Input failed" if user types in "hello" or a very long number. So how do I differentiate between the two reason the input failed so that I can give a better error message?
If the input is not a valid number a is set to zero
If the number is too large a is set to std::numeric_limits<int>::max() (the maximum value that an int can handle).
If the number is too small a is set to std::numeric_limits<int>::min().
Last edited on
Thanks a lot, mate. If other have doubts like this, where do I refer to?
I found this information at cppreference.com: http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

If extraction fails, zero is written to value and ...
Last edited on
Topic archived. No new replies allowed.