Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <exception>
- #include <iostream>
- #include <string>
- using std::exception, std::runtime_error, std::invalid_argument, std::out_of_range,
- std::cin, std::cout, std::cerr, std::endl,
- std::string, std::getline, std::stoi;
- auto fail( const string& s ) -> bool { throw runtime_error( s ); }
- #define FAIL( s ) fail( string() + __func__ + " - " + s )
- auto input()
- -> string
- {
- string line;
- getline( cin, line ) or FAIL( "std::getline failed" );
- return line;
- }
- auto input_int( const string& prompt )
- -> int
- {
- for( ;; ) {
- cout << prompt;
- try {
- return stoi( input() );
- } catch( const invalid_argument& ) {
- cout << "^ Not a valid integer." << endl;
- } catch( const out_of_range& ) {
- cout << "^ That value is too large." << endl;
- }
- }
- }
- void cpp_main()
- {
- const int n = input_int( "Enter a number: " );
- const string kind = (n % 2 == 0? "EVEN" : "ODD");
- cout << endl;
- cout << "\bThe number you entered is an " << kind << " NUMBER." << endl;
- cout << "Thank you." << endl;
- }
- #include <stdlib.h> // EXIT_...
- auto main()
- -> int
- {
- try {
- cpp_main();
- return EXIT_SUCCESS;
- } catch( const exception& x ) {
- cerr << "!" << x.what() << endl;
- }
- return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement