The test for input stream failure occurs after the input has been used. The line if (cin.fail())... should immediately follow the cin >> ... statement.