"Opposite" of cin.fail()?

Jul 27, 2014 at 4:57pm
Is there any way to detect when cin succeeds, like how cin.fail() looks for when it, well... fails?
Jul 27, 2014 at 5:05pm
Yes. There are several ways to test C++ streams.
1
2
3
4
5
6
7
8
9
10
11
12
13

if(!cin.fail()) 
..,

if(cin.good())
...


if(!cin)
...

if(cin)
...


Jul 27, 2014 at 5:06pm
Normally simply if (cin).

Often, rather than doing say:
1
2
3
    int n;
    cin >> n;
    if (cin)

one would prefer this:
1
2
    int n;
    if (cin >> n)

Last edited on Jul 27, 2014 at 5:07pm
Jul 27, 2014 at 5:18pm
Alright, that makes sense. Thanks!
Topic archived. No new replies allowed.