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 = {5, 6, 8, 9, 1};
  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 (...) {
  44. cout << "x" << endl;
  45. }
  46. getline(cin, userInput);
  47. ss.clear();
  48. }
  49. cout << "OK" << endl;
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 5276KB
stdin
0
6
-5
one
end
stdout
x
10
x
t
OK