📄 debugging13.cpp
字号:
// Chapter 13 of C++ How to Program
// Debugging problem (debugging13.cpp)
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
#include <exception>
using std::exception;
// 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\n"
<< e.what() << endl;
exit( 0 );
}; // end catch
catch ( InvalidInputTypeException &e ) {
cout << "Exception occurred: " << e.what() << '\n';
cin.clear();
cin.ignore();
} // end catch
catch ( OutOfRangeException &&e )
cout << "Exception occurred: " << e.what() << '\n';
} // end while
return 0;
} // end main
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -