首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-03-05 
新手:关于异常的问题,一题老报错,不明白#includeiostreamusingnamespacestd//ClassDivideByZeroExcepti

新手:关于异常的问题,一题老报错,不明白
#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;

}

热点排行