fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdexcept>
  5. using namespace std;
  6.  
  7. int main() {
  8. stringstream ss;
  9. string userInput;
  10. int value;
  11.  
  12. // Failed conversion throws ios_base::failure
  13. ss.exceptions(ios::failbit);
  14.  
  15. getline(cin, userInput);
  16.  
  17. while (userInput != "end") {
  18. try {
  19. ss.str("");
  20. ss.clear();
  21. ss << userInput;
  22. ss >> value;
  23.  
  24. // Division by zero throws runtime_error
  25. if (value == 0) {
  26. throw runtime_error("z");
  27. }
  28.  
  29. cout << 60 / value << endl;
  30. }
  31. catch (ios_base::failure& excpt) {
  32. cout << "t" << endl;
  33. }
  34. catch (runtime_error& excpt) {
  35. cout << excpt.what() << endl;
  36. }
  37. getline(cin, userInput);
  38. ss.clear();
  39. }
  40. cout << "OK" << endl;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5280KB
stdin
0
one
10
-3
end
stdout
z
t
6
-20
OK