Code:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
int a;
cout << "Enter number\n";
while (!(cin >> a))
{
cout << "number only\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
If the stream formatter can't make the necessary type conversion then the stream goes into the fail state and the data in the buffer remains, thus to "start over" you have to clear() the fail state, then empty out the buffer, which you can do by calling ignore(), it ignores the maximum stream size number of characters or until it reaches the delimiter (EOL in this case)