// 0815.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <istream> #include <iostream> #include <sstream> #include <string> using namespace std; istream& ReadStream( istream &myin ) { string strWord; while ( myin >> strWord, !myin.eof() ){ if( myin.bad() ) throw runtime_error("IO stream corrupted"); if( myin.fail()){ cerr << "bad data,try again";//如果输入非法,如字符 myin.clear(istream.failbit); myin.ignore(200,' ');//加上此行,还是会出现死循环 continue; } cout << strWord << endl; } cout << strWord << endl; myin.clear(); return myin; } int _tmain(int argc, _TCHAR* argv[]) { string strline; string word; cout << "Enter a line:" << endl; getline(cin,strline); istringstream insstream(strline); ReadStream(insstream); return 0; }