处理浮点数c ++的异常

时间:2015-08-17 09:51:14

标签: c++ exception-handling

嘿,如果用户输入了除数值以外的任何内容,我试图让我的代码抛出错误。

这是我到目前为止所做的,但是当我运行程序并输入一个字符而不是一个数字时,它只是跳过其余的输入并输出计算而不会实际抛出任何错误信息。

#include "classes.h"
#include <iostream>
#include <math.h>
using namespace std;
//class constructor
thevenin::thevenin()
{
    Resistance_1 = 1;
    Resistance_2 = 1;
    Resistance_3 = 1;
    Voltage_1 = 1;
    Voltage_2 = 1;


}
//empty class destructor
thevenin::~thevenin()
{

}
//output script for One loop problems
void thevenin::One_loop_thev(float tr1o, float tv1v)
{
    //inputs



    if (cin.fail()) //checking whether failbit or badbit is set
    {
        cout<<"error";
        cin.clear(); //sets a new value for the stream's internal error state flags.
        cin.ignore(numeric_limits<streamsize>::max(),'\n'); //ignores rest of the input ..
    }
    else;

            cout<<"Please enter the value of R1"<<endl;
            cin>>Resistance_1;
            cout<<"Please enter the value of R2"<<endl;
            cin>>Resistance_2;
            cout<<"Please enter the value of R3"<<endl;
            cin>>Resistance_3;
            cout<<"Please enter the value of V1"<<endl;
            cin>>Voltage_1;

            //calculations
            tv1v = (Voltage_1 * Resistance_3)/ (Resistance_1 + Resistance_3);
            tr1o = Resistance_2 + ((Resistance_1*Resistance_3)/(Resistance_1+Resistance_3));

            cout<<"The Thevenin equivalent resistance is: "<<tr1o<<"ohms"<<endl;
            cout<<"The Thevenin equivalent voltage is: "<<tv1v<<"volts"<<endl;

我只是想知道如何调整if(cin.fail())语句以使用我的代码。

2 个答案:

答案 0 :(得分:3)

当您尝试读取值时,流会遇到错误,因此您无法在cin之前检查错误。

抛出错误的一种方法是告诉iostream为你抛出错误:

cin.exceptions(istream::failbit);

然而,您应该处理出现的错误。一个小型演示:

 #include <iostream>                                                                                                                                                                                                                                                                                                      
 #include <limits>                                                                                                                                                                                                                                                                                                        

 using namespace std;                                                                                                                                                                                                                                                                                                     

 int main () {                                                                                                                                                                                                                                                                                                            
     cin.exceptions(istream::failbit);                                                                                                                                                                                                                                                                                    
     float a;                                                                                                                                                                                                                                                                                                             
     while (!cin.eof()) {                                                                                                                                                                                                                                                                                                 
         try {                                                                                                                                                                                                                                                                                                            
             cin >> a;                                                                                                                                                                                                                                                                                                    
             cout << a<< '\n';                                                                                                                                                                                                                                                                                            
          } catch (istream::failure& e) {                                                                                                                                                                                                                                                                                  
             cout << "bad input\n";                                                                                                                                                                                                                                                                                       
             cin.clear();                                                                                                                                                                                                                                                                                                 
             cin.ignore(numeric_limits<streamsize>::max(),'\n');                                                                                                                                                                                                                                                          
         }                                                                                                                                                                                                                                                                                                                
      }                                                                                                                                                                                                                                                                                                                    
  }

因此,对于您的代码(已编辑以添加重试次数):

//output script for One loop problems
void thevenin::One_loop_thev(float tr1o, float tv1v)
{
    cin.exceptions(istream::failbit); // throw errors

    while (true) {
        try {
            cout<<"Please enter the value of R1"<<endl;
            cin>>Resistance_1;
            cout<<"Please enter the value of R2"<<endl;
            cin>>Resistance_2;
            cout<<"Please enter the value of R3"<<endl;
            cin>>Resistance_3;
            cout<<"Please enter the value of V1"<<endl;
            cin>>Voltage_1;

            //calculations
            tv1v = (Voltage_1 * Resistance_3)/ (Resistance_1 + Resistance_3);
            tr1o = Resistance_2 + ((Resistance_1*Resistance_3)/(Resistance_1+Resistance_3));

            cout<<"The Thevenin equivalent resistance is: "<<tr1o<<"ohms"<<endl;
            cout<<"The Thevenin equivalent voltage is: "<<tv1v<<"volts"<<endl;

            return; // break out of the loop
        }
        catch (istream::failure& e) {
            cout<<"error";
            cin.clear(); //sets a new value for the stream's internal error state flags.
            cin.ignore(numeric_limits<streamsize>::max(),'\n'); //ignores rest of the input ..
        }
    }
}

答案 1 :(得分:2)

if (cin.fail()) //checking whether failbit or badbit is set
    {
        cout<<"error";
        cin.clear(); //sets a new value for the stream's internal error state flags.
        cin.ignore(numeric_limits<streamsize>::max(),'\n'); //ignores rest of the input ..
    }
    else//;<--- why semicolon?
{
            cout<<"Please enter the value of R1"<<endl;
            cin>>Resistance_1;
            cout<<"Please enter the value of R2"<<endl;
            cin>>Resistance_2;
            cout<<"Please enter the value of R3"<<endl;
            cin>>Resistance_3;
            cout<<"Please enter the value of V1"<<endl;
            cin>>Voltage_1;

            //calculations
            tv1v = (Voltage_1 * Resistance_3)/ (Resistance_1 + Resistance_3);
            tr1o = Resistance_2 + ((Resistance_1*Resistance_3)/(Resistance_1+Resistance_3));

            cout<<"The Thevenin equivalent resistance is: "<<tr1o<<"ohms"<<endl;
            cout<<"The Thevenin equivalent voltage is: "<<tv1v<<"volts"<<endl;
}
这是你的意思吗?在其他人之后我摆脱了分号..

除此之外尝试简单的try-catch语句...... 另外,你在哪里将价值传递给cin? (你在哪里得到它)。告诉我们这个代码很好。