读书人

新手:关于错误的有关问题,一题老报错,

发布时间: 2012-03-05 11:54:03 作者: rapoo

新手:关于异常的问题,一题老报错,不明白
#include <iostream>

using namespace std;

//ClassDivideByZeroException to be used in eception

class DivideByZeroException
{
public:
DivideByZeroException()
:message( "attempted to divide by zero "){}
const char *what() const{return message;}

private:
const char *message;
};

//Definition of function quotient.Demonstrates throwing
double quotient(int numerator, int denominator)
{
if (denominator = 0)
throw DivideByZeroException();

return static_cast <double> (numerator) / denominator;

}

//driver program
int main()
{
int number1, number2;
double result;

cout < < "Enter tow integers(end-of-file to end): ";

while (cin > > number1 > > number2)
{
//the try block wraps the code that way throw an
//exception and the code that should not execute
//if an exception occurs
try
{
result = quotient(number1, number2);
cout < < "The quotient is: " < < result < < endl;
}
catch(DivideByZeroException ex)
{//exception handler
cout < < "Exception occurred: " < < ex.what() < < '\n ';
}
cout < < "\nEnter two integers (end-of-file to end): ";
}

cout < < endl;

//system( "pause ");
return 0;//terminate normally
}

这是小弟看书照书写的,但是运算结果不正常1.#INF,大家帮看看

[解决办法]

double quotient(int numerator, int denominator)
{
if (denominator == 0) //应该是双等号
throw DivideByZeroException();

return static_cast <double> (numerator) / denominator;

}

读书人网 >C++

热点推荐