[英]Exceptions handling for floats c++
嘿,如果用戶輸入的數字以外的任何內容,我試圖讓我的代碼拋出錯誤。
這是我到目前為止所做的,但是當我運行程序並輸入一個字符而不是數字時,它只是跳過其余的輸入和輸出計算而不會實際拋出任何錯誤消息。
#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 ..
}
}
}
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? (你在哪里得到它)。 告訴我們這個代碼很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.