c++ - float 的异常处理 C++

标签 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()) 语句以使用我的代码。

最佳答案

当您尝试读入值时,流会遇到错误,因此您无法在 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 ..
        }
    }
}

关于c++ - float 的异常处理 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32047385/

相关文章:

c++ - 异常的生命周期是否受嵌套处理程序的影响?

swift - 使用 NSExpression 时捕获 NSInvalidArgumentException 的正确方法

.net - 在 ASP.NET MVC 应用程序中在哪里定义异常处理和错误日志记录的最佳实践?

c++ - 在 C++ 模板中禁用代码的示例

c++ - 空指针指针(void **)

c++ - 使用 CoCreateInstance 时内存泄漏

c++ - opencv 3.0.0 c++ detectMultiScale 检测到大量的人脸

multithreading - MonoTouch-AppDomain.CurrentDomain.UnhandledException挂起

scala - 为抛出异常的函数评估 bool 值的函数方法

C++ 协程 : implementing task<void>