Correct way to use cin.fail()
What is the correct way to use cin.fail(); ? I am making a program where you need to input something. It is not very clear if you need to input a number or character. When a user inputs a character instead of a number the console goes crazy. How can I use cin.fail() to fix this? Or is there a better way? Shumail cin.fail() returns true if the last cin command failed, and false otherwise. An example: int main() { int i, j = 0; while (1) { i++; cin >> j; if (cin.fail()) return 0; cout << "Integer " << i << ": " << j << endl; } } Now suppose you have a text file - input.txt and it's contents are: