최종 업데이트 : 모든 의견과 도움에 감사드립니다. 나는 교과서로 돌아가 자신의 프로그램을 실행하기 위해 전체 코드를 복사했으며, 던지기 메시지가 나타나지 않는다는 것을 깨달았습니다. 메시지가 나타날 것이라고 생각했는데, 이것이 내가 내 프로그램에 나타나는 메시지를 기대했기 때문입니다. try/catch를 사용하십시오. 도움 녀석/gals을 위해 다시 당신을 감사하십시오.던져 오류 메시지가 표시되지 않습니다. C++
할당 작업을하고 있는데 어떤 이유로 throw invalid_argument
에 "계정 잔액이 너무 낮음"메시지가 표시되지 않습니다.
나는 꽤 많이 내 텍스트 책
void setBaseSalary(double salary) {
if (salary<0.0){
throw invalid_argument("Salary must be >= 0.0");
}
}
의 코드를 가져다가 만든 내 자신 ....
void Account::setAccountBalance(double accountBalanceVar)
{
cout << accountBalanceVar << "B4 IF" << endl;
if (accountBalanceVar < 0.0) {
cout << accountBalanceVar << "B4 while in IF" << endl;
throw invalid_argument("Account balance is too low"); // program err
accountBalanceVar = 0.0;
cout << accountBalanceVar << "In IF" << endl;
}
accountBalance = accountBalanceVar;
}
는 cout을 날 디버깅 된 곳 프로그램 정지 작업, 나를 인도 던지기. 여기
오류를 복제하는 내 전체 코드, 난 내 입력 -5을 사용하고있다
#include <iostream>
#include <iomanip>
#include "savingAccount.h"
#include "checkingAccount.h"
#include "Account.h"
// header
using namespace std;
int main() {
double userAccountBalance{ 0.0 };
cout << "Enter your account Balance: " << endl;
cin >> userAccountBalance;
Account myAccount{ userAccountBalance };
return 0;
}
#include "Account.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <stdexcept>
#include <sstream>
using namespace std;
Account::Account(double accountBalanceVar) {
setAccountBalance(accountBalanceVar);
cout << accountBalance << ": Tester: constructor" << endl;
}
double Account::credit(double addBalance)
{
accountBalance+=addBalance;
return accountBalance;
}
double Account::debit(double withDrawnVar)
{
if (accountBalance < withDrawnVar) {
cout << "Error : You can not withdrawn more than your total balance";
withDrawnVar = 0; // To ensure unchange
}
accountBalance -= withDrawnVar;
return withDrawnVar;
}
Account::~Account()
{
}
void Account::setAccountBalance(double accountBalanceVar)
{
cout << accountBalanceVar << "B4 IF" << endl;
if (accountBalanceVar < 0.0) {
cout << accountBalanceVar << "B4 while in IF" << endl;
throw invalid_argument("Account balance must be >= 0.0");
accountBalanceVar = 0.0;
cout << accountBalanceVar << "In IF" << endl;
}
accountBalance = accountBalanceVar;
}
double Account::getAccountBalance()
{
return accountBalance;
}
#include <iostream>
#include <iomanip>
#include <string>
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
public:
Account(double = 0.0);
double credit(double);
double debit(double);
~Account();
void setAccountBalance(double);
double getAccountBalance();
private:
double accountBalance{ 0.0 };
double zeroBalance{ 0.0 };
};
#endif
업데이트 : 당신이
를보고있는 어떤 증상? 어떤 입력을 사용하고 있습니까? B4 "< < accountBalanceVar < < COUT"입력 후 프로그램 정지 (사용 -5 입력으로) 를 IF에있는 동안 "< < ENDL 「
명령 -5를 표시, 드로 표시되지 않으며 프로그램이 중지됩니다.
교과서의 예가 그것을 사용하지 않았기 때문에 나는 try/catch를 사용하지 않았다. 교과서 버전이 작동하는 이유와 혼란 스럽다. 거의 동일하다.
자세한 내용을 입력해야합니다. 던진 invalid_argument가 일하기를 원하지 않는다는 것은 무엇을 의미합니까? 어떤 증상이 있습니까? 어떤 입력을 사용하고 있습니까? 함수를 호출하는 코드는 어떻게 생겼을 까? –
어디에서 예외를 처리합니까? 당신의'try ... catch' 진술은 어디에 있습니까? –
'throw '뒤에'cout'이 출력 한 텍스트를 보길 원하십니까? 프로그램은 결코 거기에 도착하지 않을 것입니다. 좋은 조언을 제공하기에 충분한 정보를 얻을 수 있도록 [mcve]를 가질 수 있을까요? – user4581301