First, I want to preface this discussion with "I KNOW THERE IS AN EASIER WAY BESIDES EXCEPTION HANDLING!!" However, my assignment REQUIRES me to use exception handling in some way in my Mortgage calculator.
My code works!!....sort of....
It allows me to input the principal with a comma and it ignores the comma (eg. 200,000).
Then it allows me to input the number of years to be financed.
After that it returns -1.00 output with the rest of my code.
Output reads:
***************************
Welcome to ABC Bank!
How much are you wanting to finance?
200,000
Do you want a 15 or 30 year loan?
15
-1.00
Your monthly mortgage payment is -1.00 at an annual Interest rate of
3.28% for 15.00 years.
Process returned 0 (0x0) execution time : 7.644 s
Press any key to continue.
************************************
My question is where is the -1.00 coming from and how to I get the mortgage to calculate properly.
I have been writing and re-writing this for over 3 days now to figure out where my mistake is. My Professor is not answering my cries for help and neither are any of my classmates. This project is due TODAY!!!!!
I'm sitting on the edge of "screw it! Just turn it in broken!"
Please advise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#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
{
cin >> 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);
}
std::istringstream ins(Principal); // Converts the string into a number without punctuation
ins >> retValue;
return retValue;
}
};
cin >> myMortgage.Principal;
if(!cin)
{
cin.clear(); //Clears the error flags
cin.ignore(1024, '\n'); //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;
}
|
Thanks - Mustang