Advertisement
alfps

Untitled

Jun 4th, 2020
1,432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <exception>
  2. #include <iostream>
  3. #include <string>
  4. using   std::exception, std::runtime_error, std::invalid_argument, std::out_of_range,
  5.         std::cin, std::cout, std::cerr, std::endl,
  6.         std::string, std::getline, std::stoi;
  7.  
  8. auto fail( const string& s ) -> bool { throw runtime_error( s ); }
  9.  
  10. #define FAIL( s ) fail( string() + __func__ + " - " + s )
  11.  
  12. auto input()
  13.     -> string
  14. {
  15.     string line;
  16.     getline( cin, line ) or FAIL( "std::getline failed" );
  17.     return line;
  18. }
  19.  
  20. auto input_int( const string& prompt )
  21.     -> int
  22. {
  23.     for( ;; ) {
  24.         cout << prompt;
  25.         try {
  26.             return stoi( input() );
  27.         } catch( const invalid_argument& ) {
  28.             cout << "^ Not a valid integer." << endl;
  29.         } catch( const out_of_range& ) {
  30.             cout << "^ That value is too large." << endl;
  31.         }
  32.     }
  33. }
  34.  
  35. void cpp_main()
  36. {
  37.  
  38.     const int n = input_int( "Enter a number: " );
  39.     const string kind = (n % 2 == 0? "EVEN" : "ODD");
  40.     cout << endl;
  41.     cout << "\bThe number you entered is an " << kind << " NUMBER." << endl;
  42.     cout << "Thank you." << endl;
  43. }
  44.  
  45. #include <stdlib.h>     // EXIT_...
  46.  
  47. auto main()
  48.     -> int
  49. {
  50.     try {
  51.         cpp_main();
  52.         return EXIT_SUCCESS;
  53.     } catch( const exception& x ) {
  54.         cerr << "!" << x.what() << endl;
  55.     }
  56.     return EXIT_FAILURE;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement