fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <stdexcept>
  5. #include <limits>
  6. using namespace std;
  7. int main()
  8. {
  9. int ival;
  10. while(cin >> ival, !cin.eof())
  11. {
  12. cout<< cin.rdstate()<<endl;
  13. if(cin.bad())
  14. throw runtime_error("IO stream corrupted");
  15. if(cin.fail())
  16. {
  17. cout<< "bad data,try again"<<endl;
  18. cin.clear(istream::goodbit); // Set the goodbit
  19. cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
  20. continue;
  21. }
  22. }
  23. }
Success #stdin #stdout 0s 3300KB
stdin
a
b
c
stdout
4
bad data,try again
4
bad data,try again
4
bad data,try again