Check out example codes for "fail() in c++". It will help you in understanding the concepts better.

Code Example 1

#include <iostream>
using namespace std;

int main()
{
    int number;
    do{
        cin >> number;
        if(cin.fail())
            cout << "Not a number " << endl;
    }while(!cin.fail());
    cout << "number is " << number << endl;
    system("pause");
    return 0;
}

Learn ReactJs, React Native from akashmittal.com