3 Replies - 705 Views - Last Post: 20 March 2016 - 08:30 AM Rate Topic: -----

#1 bbam19   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-March 16

Need help with mortgage formula code.

Posted 20 March 2016 - 07:49 AM

I am extremely new, as in only doing this in
university for just a few short weeks. I found that if i am extremely literal with my variable and constants it makes it a lot easier for me.

I am doing a mortgage project in which requires user inputs and then mortgage formulations using pow function, I am stuck directly in the center of my project trying to get the correct formula for my monthly payments. Can anyone tell me what i am doing wrong or if my complete and utter OCD of my code is what is screwing me up. I will attach my entire code below.



#include<iostream>
#include<string>
#include<iomanip>
#include <cmath>
using namespace std;


// Constants: 
const string BRDR = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
const int yearsOne = 15; // Loan years
const int yearsTwo = 30; // Loan Years
const int yearsThree = 40; // Loan Years




int main()
{


	cout << BRDR << endl;
	cout << "$" << setw(85) << "$";
	cout << "$" << "             Welcome to the mortgage information calculator 2016                    $";
	cout << "$" << setw(85) << "$";
	cout << "$" << "             Made exclusively for Dewey, Cheatum, and Howe bankers                  $";
	cout << "$" << setw(85) << "$";
	cout << "$" << "             by: Carrie Mabb                                                        $";
	cout << "$" << setw(85) << "$";
	cout << BRDR << endl;
	system("pause");
	system("cls");

	cout << "Please enter the name of the family purchasing the home: ";	
	string name;
	cin >> name;
	cout << "Please enter the home's price: ";
	int homePrice; // Home Price
	cin >> homePrice;
	cout << " Please enter the down payment percentage: ";
	int dwnPayment;
	cin >> dwnPayment;
	cout << " Please enter the interest rate as a percentage: ";
	float interest;
	cin >> interest;
	system("cls");


   float monthInterest = interest/12;
    int amntDown = homePrice * dwnPayment/100; // Down Payment Amount
	int amntFinanced = homePrice - amntDown; // Finaced Amount
	int loanMonthsFift = yearsOne * 12; // Loan term in months
	int loanMonthsThirt = yearsTwo * 12;// Loan term in months
	int loanMonthsFort = yearsThree * 12; // Loan term in months
	int mPaymentOne = (monthInterest*(homePrice - dwnPayment))*(pow((1 + monthInterest), loanMonthsFift) / pow((1 + monthInterest), loanMonthsFift) - 1); // Montly payment for 15 years
	int mPaymentTwo = (amntDown * monthInterest) / (1 - pow(1 + monthInterest, -loanMonthsThirt)); // Monthly payment for 30 years
	int mPaymentThree = (homePrice * interest) / 1 - pow(1 + interest, -loanMonthsFort); // Monthy Payment for 40 years
	int totalPaymentsOne = mPaymentOne * 12; // Total monthly Payments of 15 years
	int totalPaymentsTwo = mPaymentTwo * 12; // Total monthly Payments for 30 years
	int totalPaymentsThree = mPaymentThree * 12; // Total Montly Payments for 40 years

	


	cout << "Financing breakdown for the "<<name << " Family: " << endl;
	cout << " " << endl;
	cout << " " << endl;
	cout << "Price of home: $" << homePrice << endl;
	cout << "Amount down: $" << amntDown << endl;
	cout << "Amount financed: $" << amntFinanced<< endl;
	cout << " Annual interest rate: " << interest <<"%"<< endl;
	cout << "Monthly interest rate: " <<interest/12 << "%" << endl;
	cout << " " << endl;
	cout << " " << endl;
	cout << "15 year loan: " << endl;
	cout << "Monthly payment: $" <<mPaymentOne<< endl;
	cout << "Number of monthly payments: " << endl;
	cout << "Total Payments: $" << endl;
	cout << "Total interest paid on the loan: $" << endl;
	cout << " " << endl;
	cout << " " << endl;
	cout << "30 year loan: " << endl;
	cout << "Monthly payment: $" <<mPaymentTwo<< endl;
	cout << "Number of monthly payments: " << endl;
	cout << "Total Payments: $" << endl;
	cout << "Total interest paid on the loan: $" << endl;
	cout << " " << endl;
	cout << " " << endl;
	cout << "40 year loan: " << endl;
	cout << "Monthly payment: $"<< mPaymentThree<<endl;
	cout << "Number of monthly payments: " << endl;
	cout << "Total Payments: " << endl;
	cout << "Total interest paid on the loan: $" << endl;




	return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with mortgage formula code.

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Need help with mortgage formula code.

Posted 20 March 2016 - 07:55 AM

Integer math has no fractions. Because dwnPayment is an integer, dwnPayment/100 will result in a value of 0 if it is less than 100.

Try dwnPayment/100.0, which forces floating point math.
Was This Post Helpful? 0
  • +
  • -

#3 bbam19   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-March 16

Re: Need help with mortgage formula code.

Posted 20 March 2016 - 08:00 AM

View PostCTphpnwb, on 20 March 2016 - 07:55 AM, said:

Integer math has no fractions. Because dwnPayment is an integer, dwnPayment/100 will result in a value of 0 if it is less than 100.

Try dwnPayment/100.0, which forces floating point math.



See, that is not the problem I am having. I need to be able to run my code with a home price of $100,000 20% down and 4% interest and it gives me the correct values. It's the monthly payment that is giving me a run for my money, I keep either getting "0" or "66666" as the value in the 15 year calculations of my monthly payment.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Need help with mortgage formula code.

Posted 20 March 2016 - 08:30 AM

It's basically the same problem.
	double numerator =((monthInterest*(homePrice - dwnPayment))*(pow((1 + monthInterest), loanMonthsFift)));
	double denominator =(pow((1 + monthInterest), loanMonthsFift) - 1);
	cout << "Numerator: " << numerator << endl;
	cout << "Denominator: " << denominator << endl;
	cout << "Result: " << numerator/denominator << endl;

	cout << "numerator: "<< ((monthInterest*(homePrice - dwnPayment))*(pow((1 + monthInterest), loanMonthsFift))) << endl;
	cout << "denominator: " << (pow((1 + monthInterest), loanMonthsFift) - 1) << endl;
	double test =(monthInterest*(homePrice - dwnPayment))*(pow((1 + monthInterest), loanMonthsFift) / pow((1 + monthInterest), loanMonthsFift) - 1);
	cout << "test: "<< test << endl;



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1