//get.h #ifndef GET_H #define GET_H #include <iostream> std::istream& get(std::istream& in); #endif
//get.cpp #include "get.h" std::istream& get(std::istream& in){ int ival; while (in>>ival , !in.eof())//遇到文件结束符之前一直读入数据 { if (in.bad())//系统级故障 throw std::runtime_error("io stream corrupted!"); if (in.fail()){//可恢复错误 std::cerr << "bad data, try again"; in.clear();//恢复流 in.ignore(200, ' ');//跳过类型非法的输入项。(跳过200个字符或遇到空格或者是文件结束为止) continue; } std::cout << ival << " "; } in.clear(); return in;
#include <iostream> #include "get.h" using namespace std; int main(int argc, char **argv) { double dval; get(cin); cin >> dval; cout<<"yyy" << dval << endl; cout << endl; system("pause"); return 0; }