Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey, guys. I got a bit of a doozy here! I decided to try to build my own mortgag

ID: 666760 • Letter: H

Question

Hey, guys. I got a bit of a doozy here! I decided to try to build my own mortgage calculator in c++ and it works...barely. It opens, but doesn't do much else. I need that fixed. I also need it to be able to handle exceptions. I would like these done individually so I can see what was done to it to fix it and what was done to it for it to handle exceptions. Thanks again for any help!

Once again this is C++

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>
#include <cctype>
#include <exception>
#include <sstream>


using namespace std;

struct calcMortgage

{
double Principal;
double numYears;
double IntRate;
double monthlyPayments;
};

int main()
{
calcMortgage myMortgage;

cout <<" Welcome to ABC Bank! " <<endl <<endl;

cout <<" How much are you wanting to finance? " <<endl;

{
double retValue;
std::string Principal;

try
{
std::string exempt;
std::stringstream ss; ss.clear ();
cin >>exempt;

if (exempt.find (',')==std::string::npos)
ss <<exempt;

else
throw exempt;
ss>> myMortgage.Principal;
}

catch(string Principal) //Exception for comma in Principal input by user
{
if(Principal.find_first_not_of("0123456789.") != std::string::npos)
{
size_t pos;
while((pos = Principal.find_first_not_of("0123456789.")) != std::string::npos) Principal.erase(pos, 1);
}

int retValue;
cin >> retValue;

if(!cin)
{
cin.clear(); //Clears the error flags
cin.ignore(1024, ' '); //Clears the input buffer
}

cout << " Do you want a 15 or 30 year loan? "<<endl;
cin >> myMortgage.numYears;

{
if (myMortgage.numYears == 15)
{
myMortgage.IntRate=3.28;
}
   else
{
myMortgage.IntRate=4.03;
}
}
double n = myMortgage.numYears*12; //n = Loan Length in months or number of payments
double i = myMortgage.IntRate/1200; //i = interest rate times principal

myMortgage.monthlyPayments=i*pow(1+i, n)*myMortgage.Principal/pow(1+i, n)-1; //Formula for calculating monthly payments

{
if ((myMortgage.Principal/pow(1+i, n)-1)==0) //exception for 0 denominator condition
{
throw "Divide by zero condition in calculate payment method. Please check your numbers.";
}
else
{
myMortgage.monthlyPayments=i*pow(1+i, n)*myMortgage.Principal/pow(1+i, n)-1; //Formula for calculating monthly payments

std::cout << std::fixed << std::setprecision(2) << myMortgage.monthlyPayments << std::endl;
cout << "Your monthly mortgage payment is " << myMortgage.monthlyPayments << " at an annual Interest rate of "<< endl;
cout << myMortgage.IntRate << "% for " << myMortgage.numYears << " years." << endl << endl;
}
}
return 0;

}
}
}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>
#include <cctype>
#include <exception>
#include <sstream>


using namespace std;

struct calcMortgage

{
double Principal;
double numYears;
double IntRate;
double monthlyPayments;
};

int main()
{
calcMortgage myMortgage;

cout <<" Welcome to ABC Bank! " <<endl <<endl;

cout <<" How much are you wanting to finance? " <<endl;

}

{

cout << " Do you want a 15 or 30 year loan? "<<endl;
cin >> myMortgage.numYears;

{
if (myMortgage.numYears == 15)
{
myMortgage.IntRate=3.28;
}
   else
{
myMortgage.IntRate=4.03;
}
}
double n = myMortgage.numYears*12; //n = Loan Length in months or number of payments
double i = myMortgage.IntRate/12; //i = interest rate per month

myMortgage.monthlyPayments=i*pow(1+i, n)*myMortgage.Principal/pow(1+i, n)-1; //Formula for calculating monthly payments

{
if ((myMortgage.Principal/pow(1+i, n)-1)==0) //exception for 0 denominator condition
{
throw "Divide by zero condition in calculate payment method. Please check your numbers.";
}
else
{
myMortgage.monthlyPayments=i*pow(1+i, n)*myMortgage.Principal/pow(1+i, n)-1; //Formula for calculating monthly payments

std::cout << std::fixed << std::setprecision(2) << myMortgage.monthlyPayments << std::endl;
cout << "Your monthly mortgage payment is " << myMortgage.monthlyPayments << " at an annual Interest rate of "<< endl;
cout << myMortgage.IntRate << "% for " << myMortgage.numYears << " years." << endl << endl;
}
}
return 0;

}
}
}