Question
Correct the errors in the following program: // Debugging: debugging.cpp #include #include using namespace std; // class InvalidInputTypeException definition class InvalidInputTypeException { public: // constructor
Correct the errors in the following program:
// Debugging: debugging.cpp #include #include using namespace std; // class InvalidInputTypeException definition class InvalidInputTypeException { public: // constructor InvalidInputTypeException() : message( "entered input of the wrong data type" ) { empty } // end class InvalidInputTypeException // function what definition const char *what() const { return message.c_str(); } // end function what private: string message; }; // end class InvalidInputTypeException // class OutOfRangeException definition class OutOfRangeException { public: // constructor exception OutOfRangeException() : message( "entered a number not in the valid range" ) { // empty } // end class OutOfRangeException constructor // function what definition const char *what() const { return message.c_str(); } // end function what private: string message; }; // end class OutOfRangeException // function inputNumber definition int inputNumber() { int number; cout << "Enter an integer from 1 to 100 (-1 to end): "; cin >> number; if ( cin.fail() == 1 ) throw( InvalidInputTypeException ); if ( number > 100 || number < 1 ) throw exception( OutOfRangeException() ); if ( num == -1 ) throw; return number; } // end function inputNumber int main() { int num1 = 0; int num2 = 0; double result; // only way to exit this loop is an exception while ( true ) { number1 = inputNumber(); number2 = inputNumber(); try { result = static_cast< double >( number1 ) / number2; cout << number1 << " / " << number2 << " = " << result << endl << endl; } // end try catch ( ... ) { cout << "An unknown exception has occurred, " << "exiting the program " << e.what() << endl; exit( 0 ); }; // end catch catch ( InvalidInputTypeException &e ) { cout << "Exception occurred: " << e.what() << ' '; cin.clear(); cin.ignore(); } // end catch catch ( OutOfRangeException &&e ) cout << "Exception occurred: " << e.what() << ' '; } // end while } // end main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started