8.1
#include <iostream>
#include <string>
using namespace std;
istream & Process(istream & i)//對於流對象,不能拷貝和賦值,必須使用引用來傳遞實參
{
int test;
while (i >> test||!i.eof())
{
if (i.bad())
throw runtime_error("IO流發生不可恢復的錯誤");
if (i.fail())
{
cout << "輸入數據類型錯誤,請重試" << endl;
//此時別忘記要重新復位
i.clear();
i.ignore(2, '\n');//會跳過錯誤字符的後2個字符(包括錯誤字符),或者遇到'\n'結束
continue;
}
cout << test<<endl;
}
i.clear();//對標誌位復位
return i;
}
int main()
{
if (Process(cin).good())
cout << "已經將cin復位" << endl;
system("pause");
return 0;
}