So my assignment is to go a little farther in this Mortgage calculator. by the way thank you for all the help thus far. I am trying to get this right, but can seem to get the math right.

Here is the assignment:
Write the program as a procedural C++ program. Calculate and display the mortgage payment amount using the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage as input by the user. Then, list the loan balance and interest paid for each payment over the term of the loan. On longer-term loans, the list will scroll off the screen. Do not allow the list to scroll off the screen, but rather display a partial list and then allow the user to continue the list. Allow the user to loop back and enter new data or quit. Insert comments in the program to document the program.

I have the code built, it compiles, but half way through the list it shows "0", and i dont know where i am messing that up. Can you please help!!

Thanks

#include <math.h>
#include <iostream>

using namespace std;

int main () {
    // defines varibles
    double principle = 0.0;
    double interest = 0.0;
    double years = 0.0;
	float total = 0.0;
    char quit;
	
    do {
		do {
        // user input
        cout << "Enter interest rate:";
        cin >> interest;
        cout << endl;
		if (! cin) {
			cout << "\nInvalid Interest...Please enter valid interest rate ";
			interest = 0;
			cin.clear();
			cin.ignore(512, '\n');
		}
		
		cout << "You entered: " << interest << "%" << endl;
	}while (interest < 0.001);
		
		do {
        cout << "Enter Total years: ";
        cin >> years;
        cout << endl;
			if (! cin) {
				cout << "\nInvalid Years...Please enter valid years ";
				years = 0;
				cin.clear();
				cin.ignore(512, '\n');
			}
			
			cout << "You entered: " << years << " years" << endl;
	}while (years < 0.001);
		
		do {
        cout << "Enter Loan amount: $";
        cin >> principle;
        cout << endl;
        cout<<"\n";
		if (! cin) {
			cout << "\nInvalid loan amount...Please enter valid loan amount ";
			principle = 0;
			cin.clear();
			cin.ignore(512, '\n');
		}
		
		cout << "You entered: $" << principle << endl;
	}while (principle < 0.001);
		
		double monthlyInterest = (interest / 100) / 12;
		int term = years * 12;
		
		total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term)); //total for mortgage

		
		
        //double total = principle*(interest/1200)/(1-pow(1+(interest/1200),-1*years*12)); // amoritization formula
        double loanAmnt = principle;
        cout << "For a $"<<loanAmnt<<" loan your payment is $" <<total<<endl<<endl;
		
			
        double monthlyInt =  principle * (interest/1200);
        double principalPaid = total - monthlyInt;
        double balance = principle-total;
        for (double i=0; i<years*12;i++)
        {
            double monthlyInt=balance*(interest/1200);
            double principalPaid=total-monthlyInt;
            if (balance<1.00)
                balance=0.0;
            //CHANGED
            else
                balance -= total ;
            //CHANGED
            cout <<" New loan balance for month "<<i+1<<" is "<<balance<<endl<<endl;
            cout <<" Your interest paid for this month is " <<monthlyInt<<endl<<endl;
            cout <<" Your Principal paid for this month is " <<principalPaid<<endl;
            cout << "\n";
			cout << "To continue please press enter button.";
            getchar();                   // pauses the program and allows you to view the output
			
        }
		
        // user can continue in a loop or quit
        //output
        cout<<"\n";
        cout<<"To continue press C then enter.\n";
        cout<<"If you wish to quit press Q then enter.\n";
		
        //user input
        cin>>quit;
        cout<<"\n";
    }
    while((quit!='q') && (quit!='Q')) ;
	
    cout<<"Thank you for trying my simple mortgage calculator\n";
	
    return 0 ;
}

Recommended Answers

All 2 Replies

Not maybe much of a C++ question, but rather a business issue. You have used a wrong formula in two places.

On line 73 and 82 change your balane calculations into

double balance = principle-principalPaid; //Line73

balance -= principalPaid; //Line 82

and you should be right. Also you have the months wrong. Since you already deducted the principalPaid in line 72 once, you need to start from month 1+1 on.

Thus change on line 78:

for (double i=1; i<years*12;i++)

Also, for the sake of correctness..

if (balance<1.00)
                balance=0.0;
            //CHANGED
            else
                balance -= principalPaid ;

The order is wrong as you need to calculate first the balance and THEN check if it's close to 0. Thus write:

balance -= principalPaid;
            if (balance<1.00)
                balance=0.0;

and you should get round to 0 on month 12.

Also just an additional idea if you are up to it. Since it is rather difficult to find your way in this code.. if you know a little bit about functions, you should use them and wrap your code within them. Thus you will be better able to understand your own code and be able to easily modify it. A good function could be looking like this:

double LoanBalanceLeft(double interest, int monthsleft, double principal)
{
//Here you write your formuals and calculate balance and return it at the end of the function
return balance 
}

And after having collected all user variables you could call that function e.g. int loan = LoanBalanceLeft(10, 3, 200000) and the function will return the correct balance amount left as loan will hold the value.

You may also make another function that returns the interest to pay. Something like

double MonthlyInterestToPay(double interest, double LoanBalance, int monthsleft)



Then your main could look like this at the end.

[code=C++]

int main(void)
{
GetUserData();

for (int month=1; month<=totalmonths;c++)
   {
   std::cout << "Balance: " << LoanBalanceLeft(double interest, principal, totalmonths, month) << " left at month "<< monthstotal-monthsleft << std::endl;

   std::cout << "Interest to pay: " << MonthlyInterestToPay(interest, LoanBalance, totalmonths, month) << std::endl;
   }

UserQuit();
return 0;
}

Cheers,
poliet

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.