int main()
{
int temp;
while (cin>>temp, !cin.eof())
{
if (cin.bad())
{
throw r
This has to do with data still being in the input buffer. cin.clear()
is simply clearing the error state of cin
rather than clearing the buffer. Try doing something like this:
int main()
{
int temp;
while (cin>>temp, !cin.eof())
{
if (cin.bad())
{
throw runtime_error("IO stream corrupted");
}
if (cin.fail())
{
cerr<<"bad data, try again";
cin.clear();
cin.sync();
continue;
}
}
return 0;
}
cin.sync()
will effectively clear the buffer for you. See http://www.cplusplus.com/reference/iostream/istream/sync/ for more information on it.