Correct the errors in the following program: // Debugging: debugging.cpp #includ
ID: 3900479 • Letter: C
Question
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
Explanation / Answer
I have fixed the program. Please comment If I have missed any of the requirement. I'll help for sure
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
#include<iostream>
#include <exception>
using namespace std;
// class InvalidInputTypeException definition
class InvalidInputTypeException: public exception {
public:
// constructor
InvalidInputTypeException() : message( "entered input of the wrong data type" ) { //empty
}
// end class InvalidInputTypeException
// function what definition
const char *what() const noexcept override { return message.c_str(); }
// end function what
private:
string message;
};
// end class InvalidInputTypeException
// class OutOfRangeException definition
class OutOfRangeException: public exception {
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 noexcept override { 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 ( number == -1 )
throw exception();
if ( cin.fail() == 1 )
throw(InvalidInputTypeException() );
if ( number > 100 || number < 1 )
throw (OutOfRangeException() );
return number;
}
// end function inputNumber
int main() {
int number1 = 0;
int number2 = 0;
double result;
// only way to exit this loop is an exception
while ( true ) {
try {
number1 = inputNumber();
number2 = inputNumber();
result = static_cast< double >( number1 ) / number2;
cout << number1 << " / " << number2 << " = " << result << endl << endl;
} // end try
catch (const InvalidInputTypeException& e ) {
cout << "Exception occurred: " << e.what() << ' ';
cin.clear();
cin.ignore();
} // end catch
catch (const OutOfRangeException& e ) {
cout << "Exception occurred: " << e.what() << ' ';
}
catch (...) {
cout << "An unknown exception has occurred, " << "exiting the program " << endl;
exit(0);
}
// end catch
} // end while
} // end main