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