how do you modify the calcMortgage application to handle exceptions such as indi
ID: 3773230 • Letter: H
Question
how do you modify the calcMortgage application to handle exceptions such as indication of a letter as apposed to numbers being input.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int n;
double i,i_m,P,M;
cout<<"Enter Principal:";
cin>>P;
cout<<"Enter rate of interest:";
cin>>i;
i_m = i/1200;
cout<<"Number of months:";
cin>>n;
M = P * (i_m * pow((i_m+1),n) / (pow((i_m+1),n)-1));
cout<<"Monthly mortage is "<<M;
return 0;
}
Explanation / Answer
try
{
int n;
double i,i_m,P,M;
cout<<"Enter Principal:";
cin>>P;
cout<<"Enter rate of interest:";
cin>>i;
i_m = i/1200;
cout<<"Number of months:";
cin>>n;
M = P * (i_m * pow((i_m+1),n) / (pow((i_m+1),n)-1));
cout<<"Monthly mortage is "<<M;
throw -1;
}
catch (DOUBLE X)
{
// eXCEPTION CAUGHT
cerr << "We caught an exception of type int" << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.