I have been working for hours trying to get my mortgage calculator program to work. The purpose of the program is to allow user input of the loan amount, interest rate, and term in years, and then display the mortgage payment amount. I also have to list the loan balance and interest paid over the term of the loan. I am fine with the calculation of the mortgage payment amount. The problem is that I am unable to get the loan balance for each payment and the interest paid for each payment to calculate correctly. Also, I'm unsure how to stop when the loan balance is zero. Any assistance with what I am doing wrong in my calculations and how to stop when the loan balance is zero would be greatly appreciated!

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

using namespace std;

int main()
	//local variables
	float LOAN_AMOUNT=0; //loan amt
	float INT_RATE=0;	 //interest rate
	int TERM_YEARS=0;	 //loan term in years
	char quit;			 //determines if user wants to quit
		//Variable declarations
		float RATE;					 //converts interest rate into decimal format 
		float monthlyInterestRate;	 //monthly interest rate
		int length_in_months = 12;	 //number of months for a given period
		int numberOfPayments;		 //total number of payments
		float monthlyPaymentAmt;	 //monthly payment amount
		int monthly_payments_counter;//counter that keeps track of the payment number
		float loanBalance;			 //loan balance
		float monthlyIntPymt;		 //monthly interest payment
		int divide_list = 0;		 //displays a partial list for the user
		char list_more;				 //enables the user to continue viewing a partial list
		float monthlyPrincipalPymt;	 //monthly principal payment

        //output message asking for loan amt
		cout<<"Input the total loan amount:\n";

		//input data

		//output message asking for interest rate
		cout<<"Input the annual interest rate:\n";

		//input data
		//output message asking for term in years of loan
		cout<<"Input the length of the loan in years:\n";

		//input data

		//rate in decimal format
		RATE = INT_RATE / 100; 

		//number of payments
		numberOfPayments = TERM_YEARS * length_in_months; 
		//monthly interest rate
		monthlyInterestRate = RATE / length_in_months; 

		//monthly payments
		monthlyPaymentAmt = (LOAN_AMOUNT*
			pow(monthlyInterestRate + 1, numberOfPayments)
			* monthlyInterestRate)/(pow(monthlyInterestRate + 1, 
			numberOfPayments) - 1); //pow returns x raised to the power of y

		//output results
		cout<<fixed<<setprecision(2)//cout writes text to the screen
		cout<<"Your monthly payment for "
			<<TERM_YEARS <<" years\n";
		cout<<"for an Interest Rate of "
			<<INT_RATE <<"%\n"; 
		cout<<"on a Loan Amount of $"
			<<LOAN_AMOUNT <<" is $"<<monthlyPaymentAmt<<" a month\n";
		cout<<"Payment Number\tLoan Balance\tInterest Paid\n";

		//List the payment number, loan balance, and interest paid 
		//for each period
		for (monthly_payments_counter=1; monthly_payments_counter<=numberOfPayments; ++monthly_payments_counter)
			monthlyIntPymt = loanBalance * monthlyInterestRate;
			monthlyPrincipalPymt = monthlyPaymentAmt - (loanBalance * INT_RATE);
			loanBalance = loanBalance - monthlyPrincipalPymt;

			//displays a partial list and allows the user either continue viewing 
			//the list, enter a new loan, or exit the program
			if (divide_list == 12)
				cout <<"Type 'c' to continue viewing the list, "
					 <<"'n' to enter a new loan, or 'e' to exit: ";
				cin >> list_more;

				if ((list_more == 'c') || (list_more == 'C'))
					divide_list = 0;
				else if ((list_more == 'n') || (list_more == 'N'))
				else if ((list_more == 'e') || (list_more == 'E'))
					return 0;
			//displays the results on the screen for each payment number
		//user can continue in a loop or quit
		cout<<"Enter C to continue, Q to quit >";

		//user input

	while (((quit!= 'q') && (quit!= 'Q')) || ((quit == 'c') && (quit == 'C')));

	cout<<"Thanks for using the calculator!\n";

	return 0;
13 Years
Discussion Span
Last Post by Dave Sinkula

You use loanBalance before it is initialized:

monthlyIntPymt = loanBalance * monthlyInterestRate;
monthlyPrincipalPymt = monthlyPaymentAmt - (loanBalance * INT_RATE);
loanBalance = loanBalance - monthlyPrincipalPymt;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.