Mortgage Rate Calculator

Seem to have gotten myself in a bind

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 3698 Views - Last Post: 01 June 2009 - 06:19 PM Rate Topic: -----

#1 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Mortgage Rate Calculator

Posted 01 June 2009 - 12:50 AM

So I decided I would write a mortgage rate calculator using functions for practice, and I seem to have gotten myself into a bind... Everything works right now, and compiles and runs. However i need to use the variable effInt and totalMonths in later functions, and I don't quit know how to pass them to the new functions, I have not quite written the new functions yet because I don't know how to pass these two things into a different function to be used...
(I know, it's really messy, still in the development stage)

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

void monthsLoan(double, double);
void effectiveIntRate(double, double);

int main() {
char choice;
int const MONTHS = 12;
double payFreq;				  //Number of payments per year
double intRate;				  //Interest Rate
double effInt;				   //Effective interest rate
double noPay;				 //Number of payments
double mortgage;		  //Amount the mortgage is I.E. 100,000 no commas
double loanYrs;			  //Term of loan. I.E. 20 years 30 years
double totalMortgage;	//The final amount your mortgage will cost
double formula;  //(1 + (intRate/ payFreq))   The first part of the Effective Interest Rate Calculation
double formPt2;// (12/ payFreq)				   This is the second part of the formula for the Effective
									  // Interest Rate Calculation.

/***********************************************
The required formulas for producing the correct output  
effInt = pow(formula, formPt2) - 1;	 //Effective rate formula
noPay = (MONTHS * payFreq);		 //Number of payments over the life of your loan
**********************************************/
system("clear");
cout << "________--------```````Mortgage Rate Calculator```````--------________"<< endl;
cout << "\n\n\t   You will need to know the following information:" << endl;
cout << "1 ) The frequency of payments. (I.E. quarterly, semi-anually etc.)		" << endl;
cout << "2 ) The interest rate of the loan											   \n\n\n\n"  << endl;
cout << "Do you have those things in front of you? If so press Y" << endl;
cin >> choice;
	if(choice == 'Y' || choice == 'y') {
		cout << "Please enter the following information: " << endl;
		cout << "Interest Rate:" << endl;
		cin >> intRate;
		cout << "Your number of payments per year" << endl;
		cin >> payFreq; 
		cout << "How many years is your loan? (I.E. 30, 15)" << endl;
		cin >> loanYrs;
		cout << "How much is your Mortgage Loan? " << endl;
		cin >> mortgage;
		effectiveIntRate(intRate, payFreq);
		monthsLoan(payFreq, loanYrs);
	}
		else {
		cout << "Please grab those things and run the program again" << endl;
		return (-1);
		}		


return 0;
}


void effectiveIntRate(double x, double y) {
	double formula = (1 + (x/ y));
	double formPt2 = (12/ y);
	double effInt = pow(formula, formPt2) - 1;
	cout << "\nThe ""Effective Interest Rate"" is: % " << effInt << endl; 
}

void monthsLoan(double x, double y) {
	int totalMonths = (x * y);
	cout <<"Total amount of months that is in the loan:" << totalMonths << endl;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Mortgage Rate Calculator

#2 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 01:55 AM

You will need to change your functions to allow them to be passed
eg.. you currently have
void effectiveIntRate(double x, double y) {
    double formula = (1 + (x/ y));
    double formPt2 = (12/ y);
    double effInt = pow(formula, formPt2) - 1;
    cout << "\nThe ""Effective Interest Rate"" is: % " << effInt << endl;
}




in order to pass effInt you would need
void effectiveIntRate(double x, double y, double effInt) {
    double formula = (1 + (x/ y));
    double formPt2 = (12/ y);
    double effInt = pow(formula, formPt2) - 1;
    cout << "\nThe ""Effective Interest Rate"" is: % " << effInt << endl;
}


Hence when you call this function you would have something like effectiveIntRate( 20, 20 , 5);
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 10:15 AM

Ok, but how would I pass it back so other functions can call upon this variable?
Would it be something like...
double effectiveIntRate(double x, double y, double effInt) {
	double formula = (1 + (x/ y));
	double formPt2 = (12/ y);
	double effInt = pow(formula, formPt2) - 1;
	cout << "\nThe ""Effective Interest Rate"" is: % " << effInt << endl;
return effInt;
}


Becuase there are other functions that rely on the effInt, and I don't want effInt to be calculated in the main program, but instead have it calculated here. Maybe I could havea function call another function?
Was This Post Helpful? 0
  • +
  • -

#4 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 11:16 AM

You could pass it by reference so that whatever you do to it in the function will be done to it in main(). Is that what you wanted? Otherwise, I think you can call a function from another function.
Was This Post Helpful? 0
  • +
  • -

#5 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:05 PM

Yeah that's what I want to do, is call it by reference...Would that require the & operator or the * operator...I know one is the address of and the other is a pointer, however would I implement this into my function and main?

Something like this
void effectiveIntRate(double x, double y, double effInt) {
	double formula = (1 + (x/ y));
	double formPt2 = (12/ y);
	double &effInt = pow(formula, formPt2) - 1;
	cout << "\nThe ""Effective Interest Rate"" is: % " << &effInt << endl;


???????????????????

This post has been edited by IngeniousHax: 01 June 2009 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:25 PM

I think you would

void effectiveIntRate(double x, double y, double *effInt) {
	double formula = (1 + (x/ y));
	double formPt2 = (12/ y);
	double effInt = pow(formula, formPt2) - 1;
	cout << "\nThe ""Effective Interest Rate"" is: % " << effInt << endl;



Or you could use global variables...
Was This Post Helpful? 0
  • +
  • -

#7 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:29 PM

void effectiveIntRate(double x, double y, double *effInt) {
	double formula = (1 + (x/ y));
	double formPt2 = (12/ y);
	double effInt = pow(formula, formPt2) - 1;
	cout << "\nThe ""Effective Interest Rate"" is: %" << effInt << endl; 
}


gives the error::

error: declaration of ‘double effInt’ shadows a parameter


Same thing happens with an &

This post has been edited by IngeniousHax: 01 June 2009 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 jcmaster2   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:31 PM

Here are some code that may or may not help..

Computing future investment value:

double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int numOfYears)
{
  return investmentAmount * pow(1 + monthlyInterestRate, numOfYears * 12);
}


#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double periodicPayment(double L, double r, int m, int t);

double unpaidBalance(double R, double r, int m, int t, int k);

int main()
{
	double loanAmount, interestRate; 
	int numOfPayPerYear, loanYears;

	int numOfPaymentsMade;

	char response;

	cout << fixed << showpoint << setprecision(2);

	cout << "Enter (Y/y) to find the periodic payment and unpaid "
		 << "balance after certain payments: ";
	cin >> response;
	cout << endl;

	while (response == 'Y' || response == 'y')
	{
		cout << "Enter the loan amount: ";
		cin >> loanAmount;
		cout << endl;

		cout << "Enter the interest rate per year as a percentage: ";
		cin >> interestRate;
		cout << endl;

		cout << "Enter the number of payments per year: ";
		cin >> numOfPayPerYear;
		cout << endl;

		cout << "Enter the number of years for the loan: ";
		cin >> loanYears;
		cout << endl;

		double perioPayment = periodicPayment(loanAmount, interestRate, 
											 numOfPayPerYear, loanYears);
		cout << "The periodic payment is: "
			 << perioPayment << endl;
   
		cout << "Enter the number of payments made: ";
		cin >> numOfPaymentsMade;
		cout << endl;

		cout << "The unpaid balance after " << numOfPaymentsMade
			 << "payment(s) is : " 
			 << unpaidBalance(perioPayment, interestRate, 
						  numOfPayPerYear, loanYears, numOfPaymentsMade)
			 << endl;

		cout << "Enter (Y/y) to find the periodic payment and unpaid "
			 << "balance after certain payments: ";
		cin >> response;
		cout << endl;
	}

	return 0;
}

double periodicPayment(double L, double r, int m, int t)
{
	double i = r / ( 100 * m);

	return (L * i / ( 1 - pow(1 + i, -m * t)));
}

double unpaidBalance(double R, double r, int m,
					 int t, int k)
{
	double i = r / ( 100 * m);

	return R * ((1 - pow(1 + i, -(m * t - k))) / i);
}

Was This Post Helpful? 0
  • +
  • -

#9 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:42 PM

Ok, however, how would I return the effInt variable to be used in later function calls?
double effectiveIntRate(double x, double y, double *effInt) {
	double formula = (1 + (x/ y));
	double formPt2 = (12/ y);
	double effInt = pow(formula, formPt2) - 1;
	cout << "\nThe ""Effective Interest Rate"" is: %" << effInt << endl; 
retrun effInt = pow(formula, formPt2) - 1; //<--Like this?


Was This Post Helpful? 0
  • +
  • -

#10 jcmaster2   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:43 PM

How about this:

cout << "\n Interest rate entered as a %," 
		 << "\n example, six point two percent should be entered as 6.2";
	cout <<"\n Enter the interest rate:  ";
	cin >> interest;
 
	cout <<"\n Principal should be entered as a whole number";
	cout <<"\n Enter the principal:  ";
	cin >> principal;
  
	cout <<"\n Enter the term of mortgage as an integer and in years";
	cout <<"\n Enter whole number of years  ";
	cin >> term;

	float monthlyint, numerator, denominator, pmt, tot_int, temp_1, temp_2, power;
	monthlyint = (interest / 12) / 100;
	numerator = principal * monthlyint;
	temp_1 = 1 / (1 + monthlyint);
	temp_2 = static_cast<float>(term * 12);
	power = static_cast<float>(pow(temp_1, temp_2));
	denominator = 1 - power;
	pmt = numerator / denominator;
	tot_int = (pmt * (term * 12)) - principal;
		



OR

void AskForLoanInfo(float &r_interest, int &r_principal, int &r_term)
{
	cout <<"\n Interest rate entered as a %," << endl 
		<<" example, six point two percent should be entered as 6.2";
	cout <<"\n Enter the interest rate:  ";
	cin >> r_interest;
	cin.ignore();
	cout <<"\n Principal should be entered as an integer";
	cout <<"\n Enter the principal:  ";
	cin >> r_principal;
	cin.ignore();
	cout <<"\n Enter the term of mortgage as an integer and in years";
	cout <<"\n Enter the length:  ";
	cin >> r_term;
}


// overloaded function
// returns as string that includes the mortgage calculations of monthly payment and total interest
string MortCalc (float &intrest, int &principal, int &term)
{
	string s_result;
	stringstream ss_result;
	float monthlyint, numerator, denominator, pmt, tot_int, temp_1, temp_2, power;
	monthlyint = (intrest / 12) / 100;
	numerator = principal * monthlyint;
	temp_1 = 1 / (1 + monthlyint);
	temp_2 = term * static_cast<float>(12.0);
	power = pow(temp_1, temp_2);
	denominator = 1 - power;
	pmt = numerator / denominator;
	tot_int = (pmt * (term * 12)) - principal;
	
	// formating the string stream so all numbers show 2 decimal places
	ss_result.precision(2);
	ss_result.setf(ios::showpoint | ios::fixed);
	
	ss_result <<"\n The following has been returned as a string!" << endl 
		<<" With an interest rate of " <<intrest << "% and principal of $" 
		<< principal << endl <<" over " << term << " year(s), your monthly payment will be $"<< 
		pmt << endl <<" and the total interest on the loan is $" << tot_int 
		<< "." << endl;
	s_result = ss_result.str();
	return s_result;
}


// overloaded function
// void with no return
// uses address to pass mortgage calculations of monthly payment and total interest
void MortCalc (float &interest, int &principal, int &term, 
			   float &monthly_payment, float &tot_interest)
{
	float monthlyint, numerator, denominator, pmt, tot_int, temp_1, temp_2, power;
	monthlyint = (interest / 12) / 100;
	numerator = principal * monthlyint;
	temp_1 = 1 / (1 + monthlyint);
	temp_2 = term * static_cast<float>(12.0);
	power = pow(temp_1, temp_2);
	denominator = 1 - power;
	pmt = numerator / denominator;
	tot_int = (pmt * (term * 12)) - principal;
	
	//using address - monthly payment and total interest  
	monthly_payment = pmt;
	tot_interest = tot_int;

}

Was This Post Helpful? 0
  • +
  • -

#11 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:44 PM

View PostIngeniousHax, on 1 Jun, 2009 - 11:29 AM, said:

void effectiveIntRate(double x, double y, double *effInt) {
	double formula = (1 + (x/ y));
	double formPt2 = (12/ y);
	double effInt = pow(formula, formPt2) - 1;
	cout << "\nThe ""Effective Interest Rate"" is: %" << effInt << endl; 
}


gives the error::

error: declaration of ‘double effInt’ shadows a parameter


Same thing happens with an &


I didn't see
double effInt = pow(formula, formPt2) - 1;


Try just making that
effInt = pow(formula, formPt2, effInt) - 1;

Was This Post Helpful? 0
  • +
  • -

#12 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:47 PM

Says it wasn't declared in the scope.
Was This Post Helpful? 0
  • +
  • -

#13 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:48 PM

Alright now I'm home and at my compiler, so post what you have at the moment and I'll see if I can help.
Was This Post Helpful? 0
  • +
  • -

#14 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:52 PM

*effInt = pow(formula, formPt2) - 1;


Maybe? =D


nvm

This post has been edited by Mowgef: 01 June 2009 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#15 jcmaster2   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Mortgage Rate Calculator

Posted 01 June 2009 - 12:56 PM

Max...Do you want to use your program my program seems to calculate what you are asking for...?

:crazy:

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


void Header()
{
	cout <<"\n Chapter 3, Problem 27, Another Mortgage Calculator!";
	cout <<"\n\n User enters the interest rate, principal, and term of a mortgage loan.";
	cout <<"\n Program returns the monthly payment and the total interest for the loan.";
	cout <<endl <<endl;
}

void AskForLoanInfo(float &r_interest, int &r_principal, int &r_term)
{
	cout <<"\n Interest rate entered as a %," << endl 
		<<" example, six point two percent should be entered as 6.2";
	cout <<"\n Enter the interest rate:  ";
	cin >> r_interest;
	cin.ignore();
	cout <<"\n Principal should be entered as an integer";
	cout <<"\n Enter the principal:  ";
	cin >> r_principal;
	cin.ignore();
	cout <<"\n Enter the term of mortgage as an integer and in years";
	cout <<"\n Enter the length:  ";
	cin >> r_term;
}


// overloaded function
// returns as string that includes the mortgage calculations of monthly payment and total interest
string MortCalc (float &intrest, int &principal, int &term)
{
	string s_result;
	stringstream ss_result;
	float monthlyint, numerator, denominator, pmt, tot_int, temp_1, temp_2, power;
	monthlyint = (intrest / 12) / 100;
	numerator = principal * monthlyint;
	temp_1 = 1 / (1 + monthlyint);
	temp_2 = term * static_cast<float>(12.0);
	power = pow(temp_1, temp_2);
	denominator = 1 - power;
	pmt = numerator / denominator;
	tot_int = (pmt * (term * 12)) - principal;
	
	// formating the string stream so all numbers show 2 decimal places
	ss_result.precision(2);
	ss_result.setf(ios::showpoint | ios::fixed);
	
	ss_result <<"\n The following has been returned as a string!" << endl 
		<<" With an interest rate of " <<intrest << "% and principal of $" 
		<< principal << endl <<" over " << term << " year(s), your monthly payment will be $"<< 
		pmt << endl <<" and the total interest on the loan is $" << tot_int 
		<< "." << endl;
	s_result = ss_result.str();
	return s_result;
}


// overloaded function
// void with no return
// uses address to pass mortgage calculations of monthly payment and total interest
void MortCalc (float &interest, int &principal, int &term, 
			   float &monthly_payment, float &tot_interest)
{
	float monthlyint, numerator, denominator, pmt, tot_int, temp_1, temp_2, power;
	monthlyint = (interest / 12) / 100;
	numerator = principal * monthlyint;
	temp_1 = 1 / (1 + monthlyint);
	temp_2 = term * static_cast<float>(12.0);
	power = pow(temp_1, temp_2);
	denominator = 1 - power;
	pmt = numerator / denominator;
	tot_int = (pmt * (term * 12)) - principal;
	
	//using address - monthly payment and total interest  
	monthly_payment = pmt;
	tot_interest = tot_int;

}
#include <iostream>
#include <string>
#include <sstream>
#include "MortCalc.h"
using namespace std;

int main()
{
	Header();
	string more;
	do
	{
		float interest, monthly_payment, tot_interest;
		int principal, term;
		string result;
		AskForLoanInfo(interest, principal, term);

		// overloaded function call
		result = MortCalc (interest, principal, term); 
		cout << result << endl << endl;
		
		// overloaded function call
		MortCalc (interest, principal, term, monthly_payment, tot_interest);

		// write out from main per requirement
		cout.precision(2);
		cout.setf(ios::showpoint | ios::fixed);
	
		//outputing results a second time using passed values from overloaded functions
		cout <<"\n The monthly payment and total interest have been passed using addresses!";
		cout <<"\n With an interest rate of " <<interest << "% and principal of $" 
		<< principal << endl <<" over " << term << " year(s), your monthly payment will be $"<< 
		monthly_payment << endl <<" and the total interest on the loan is $" << 
		tot_interest << "." << endl;
		cout <<"\n Would you like to calculate another mortgage? (yes or no) ";
		cin >> more;
		cin.ignore();
	} while (more == "yes");
	cout <<"\n Good luck with your mortgage!" << endl << endl;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2