#include <iostream>
#include <stdexcept>
#include
<cmath>
using namespace std;
class FutureValueError : public logic_error {
public:
FutureValueError(const char reason[]);
};
FutureValueError::FutureValueError(const char reason[])
: logic_error(reason){}
double future_value(double initial_balance, double p, int n)
{ if (p < 0 || n < 0)
throw FutureValueError("illegal future_value parameter");
return initial_balance * pow(1 + p / 100, n);
}
void read(double& init, double& interest, int& years)
{ cout << "Enter initial value, interest and years: ";
cin >> init >> interest >> years;
if (years > 100) throw logic_error("too many years!");
cout << init << " " << interest << " " << years << endl;
}
int main()
{ bool more = true;
while (more)
{ double init, interest;
int years;
try
{ read(init, interest, years);
double fvalue = future_value(init, interest, years);
cout << "The future value is " << fvalue << endl;
}
/**/
catch (FutureValueError& e)
{ cout << "A FutureValueError has occurred: "
<< e.what() << "\n";
}
/**/
catch (logic_error& e)
{ cout << "A logic error has occurred: "
<< e.what() << "\n";
}
cout << "Retry? (y/n)";
char input;
cin >> input;
if (input == 'n') more = false;
}
return 0;
}