Morgage Calculator???

The prompt is to create a morgage schedule for someone thinking of buying a new house. The inputs to determine a monthly schedule are the principal loan amount and the annual interest rate. 30 year loan. Calculate the monthly payment create a summary of the loan characteristics. Display the loan amount, the interest rate, the monthly payment, the total amount paid for the loan, the total amount of interest paid, and the ratio of amount paid over the prinicpal. Ask user to press any button to show the payment schedule. After printing the summary you can begin to make the schedule. The schedule should display the monthly payment, the amount paid in principal in that month, the amount paid in interest in that month, and the amount remaining in that principal (the amount paid in principle each month is deducted from the remaining principle). A month's interest amount is equal to monthly interest rate times the remaining principal. The monthly prinicpal is the difference between the monthly payment and the monthly interest paid. Remember to update the remaining principal after every month. After each year of the schedule has been printed display a message stating the end of that year. I have no idea where to start!!



#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double years = 30;
double mPayment = 0;
double principal = 0;
double rate = 0;

mPayment = (principal * (rate / 12) * pow((1 + rate / 12), (years * 12))) / (pow((1 + rate / 12), (years * 12)) - 1);
Break it down into parts. Don't try and do everything at once; do one part at a time, and test that it works before moving on to the next thing.

So, for this problem, the first part to do is to get the user's inputs. Prompt the user to input the things you need from them, and then read those in.

Test that that works, then move on to the next bit.
Hello bethmayweather,

I agree with MikeyBoy. If you are not going to plan your program before you actually write it then do it in small steps.

Although the "double" type is the best choice for floating point numbers in a program like this it may not work well because of the way it stores decimal numbers.

An example "0.03" stores as "0.029999999999999999", at least on VS2017. This may not be a problem when output to the screen as having only two decimal places, it would show as "0.03", but in a calculation it will produce results that are off a penny here and there and by the end of a long loop you could be off two or more dollars.

Just so you are aware.

Andy

Edit: tag.
Last edited on
Copy the assignment text into your C++ source code file. Turn each sentence into a comment.

Now go through the sentences and write code for each one. Here is a start to show you what I mean.
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
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double years = 30;
    double mPayment = 0;
    double principal = 0;
    double rate = 0;

    //The prompt is to create a morgage schedule for someone thinking of buying a new house.

    // The inputs to determine a monthly schedule are the principal
    // loan amount and the annual interest rate.  30 year loan.
    cout << "Enter loan Amount: ";
    cin >> principal;
    cout << "Enter annual interest rate (e.g., enter 4 for 4%): ";
    cin >> rate;
    rate = rate / 100;		// convert to percent
    
    // Calculate the monthly payment
    mPayment = (principal * (rate / 12) * pow((1 + rate / 12), (years * 12))) / (pow((1 + rate / 12), (years * 12)) - 1);

    // create a summary of the loan characteristics.  Display
    cout << "Loan Amount: $" << principal << '\n';    // the loan amount, 
    cout << "Interest rate: " << rate*100 << "%\n"; // the interest rate
    cout << "Monthly payment: $" << mPayment << '\n'; // the monthly payment

    double totalLoanPayment = mPayment * 12 * years;
    double totalInterestPayment = totalLoanPayment - principal;
    
    cout << "Total loan payment: $" << totalLoanPayment << '\n';   // the total amount paid for the loan
    cout << "Total Interest payment: $" << totalInterestPayment << '\n';// the total amount of interest paid,
    cout << "Ratio of total paid to principal: " << totalLoanPayment / principal << '\n'; // and the ratio of amount paid over the prinicpal.

    // Ask user to press any button to show the payment schedule.
    // After printing the summary you can begin to make the schedule.
    // The schedule should display the monthly payment, the amount paid in principal in that month, the amount paid in interest in that month, and the amount remaining in that principal (the amount paid in principle each month is deducted from the remaining principle).
    // A month's interest amount is equal to monthly interest rate times the remaining principal.
    // The monthly prinicpal is the difference between the monthly payment and the monthly interest paid.
    // Remember to update the remaining principal after every month.
    // 
 
}


Hello bethmayweather,

I wish I was not interrupted yesterday and had more time to draft my response. After seeing dhayden's response I started thinking.

You big block of characters does convey the point, but hard to read. In a case like that I will do something like this:

1)	The prompt is to create a mortgage schedule for someone thinking of buying a new house.

2)	The inputs to determine a monthly schedule are the principal loan amount and the annual interest rate. For a 30-year loan.

3)	Calculate the monthly payment create a summary of the loan characteristics.

4)	Display the loan amount,
          a)	the interest rate,
          b)	the monthly payment,
          c)	the total amount paid for the loan,
          d)	the total amount of interest paid,
          e)	and the ratio of amount paid over the principal.

5)	Ask user to press any button to show the payment schedule.

6)	After printing the summary you can begin to make the schedule.
7)	The schedule should display the
          a)	monthly payment,
          b)	the amount paid in principal in that month,
          c)	the amount paid in interest in that month, and the amount remaining in that principal
          d)	(the amount paid in principle each month is deducted from the remaining principle).

8)	A month's interest amount is equal to monthly interest rate times the remaining principal.

9)	The monthly principal is the difference between the monthly payment and the monthly interest paid.
          a)	Remember to update the remaining principal after every month.

10)	After each year of the schedule has been printed display a message stating the end of that year.

11)	I have no idea where to start!!


Using dhayden's suggestion you could copy this, not sure how the indenting will work, into a shell of a program and then fill in the code as directed.

Thinking about the program you may need some more variables than you have right now.

Even if it is not quite right work up some code and post it. You may find answers showing a better way to do something.

Hope that helps,

Andy
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double years = 30.0;
double mPayment = 0.0;
double principal = 0.0;
double monthlyInterestRate = 0.0;
double annualInterestRate = 0.0;
double ratio = 0.0;
double paymentSchedule = 0.0;
double loanAmount = 0.0;
double totalCost = 0.0;

cout << "Enter in principal" << endl;
cin >> principal;

cout << "Enter in annual interest rate" << endl;
cin >> annualInterestRate;

int monthlyInterestRate = annualInterestRate / 12.0;
double ratio = ((totalCost - loanAmount) / (loanAmount)) * 100;


double totalLoanPayment = mPayment * 12 * years;
double totalInterestPayment = totalLoanPayment - principal;

mPayment = (monthlyInterestRate / (1 - pow(1 + monthlyInterestRate, -360.0))) * principal;

// summary of loan characteristics
cout << "loan amount $";
cin >> loanAmount;

cout << "interest rate $";
cin >> annualInterestRate;

cout << "monthly payment $";
cin >> mPayment;

cout << "total cost $";
cin >> totalCost;

cout << "ratio paid over principal $%";
cin >> ratio;

cout << "Press any button to display the payment schedule" << endl;
cin >> paymentSchedule;

for (int i = 0; i < 360; ++i);




This is what I have now, not sure how to finish it.
Hello bethmayweather,

I will cover the high points here:

On lines 25 - 28 you are trying to redefine variables that you have already defined in lines 9 - 17. And on line 25 you are trying to change "monthlyInterestRate" from a "double" to an "int". Even if this would work you would not like the results when "monthlyInterestRate" ends up with a value of (0) zero.

From lines 33 - 49 you send the output to the screen and follow by asking for input that you already have. I think what you want is:
1
2
3
4
5
std::cout << "loan amount $";
std::cout << loanAmount;
// Or

std::cout << "loan amount $" << loanAmount;


The start of your program works better as:
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
#include <iostream>
#include <cmath>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	double years = 30.0;
	double mPayment = 0.0;
	double principal = 0.0;
	double monthlyInterestRate = 0.0;
	double annualInterestRate = 0.0;
	double ratio = 0.0;
	double paymentSchedule = 0.0;
	double loanAmount = 0.0;
	double totalCost = 0.0;

	std::cout << "Enter in principal" << std::endl;
	std::cin >> principal;

	std::cout << "Enter in annual interest rate(4% = 4 4.5% = 4.5) ";  // <--- Not using the "endl" puts the input on the same line.
	std::cin >> annualInterestRate;

	monthlyInterestRate = annualInterestRate / 12.0;
	ratio = ((totalCost - loanAmount) / (loanAmount)) * 100;
	double totalLoanPayment = mPayment * 12 * years;
	double totalInterestPayment = totalLoanPayment - principal;

	mPayment = (monthlyInterestRate / (1 - pow(1 + monthlyInterestRate, -360.0))) * principal;

Line 23 should be followed with annualInterestRate /= 100.0;. This will give you the actual decimal number that you will need later. Do not relay on the user to figure this and enter it correctly.

On lines 25 and 26 I remover the "int" and "double" because these variables are already defined. I found that lines 27 and 28 do need the type because they are new variables.

The only part I am not sure about is the last line. The actual calculation looks close, but not quite right.

The last bit of code is what you need to work on and get right before moving on.

Lines 33 - 49 if you fix them they can be of some help.

Hope that helps for now,

Andy
Hello bethmayweather,

My apologies I thought I had said something about using code tags, but must have been thinking about something else.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Working with what you started with I did rearrange and make some changes to your variable definitions.

1
2
3
4
5
6
7
8
9
10
constexpr double YEARS{ 30.0 };
double principal{ 100000.0 };
double annualInterestRate{ 4.5 };

double mPayment{};
double monthlyInterestRate = 0.0;
double ratio = 0.0;
double paymentSchedule = 0.0;
//double loanAmount = 0.0; // <--- Same as "principal" Not needed.
double totalCost = 0.0, totalLoanPayment{}, totalInterestPayment{};

The first line I made a constant because this should not be changed. At least for now. Maybe in the future you may need to change this.

Lines 2 and 3 are designed to bypass the input so that you can get right to checking the rest of the program with out having to type the same thing every time the program runs. To go along with you can do the input this way:
1
2
3
//std::cout << "\n Enter in principal: ";
//std::cin >> principal;
std::cout << "\n Enter in principal: " << principal << '\n';

When you finished testing and debugging uncomment lines 1 and 2 and comment line 3. Then if you ever need it it is there for you.

You have these lines in your last code:
1
2
3
4
5
int monthlyInterestRate = annualInterestRate / 12.0;
double ratio = ((totalCost - loanAmount) / (loanAmount)) * 100;

double totalLoanPayment = mPayment * 12 * years;
double totalInterestPayment = totalLoanPayment - principal;

Lines 1 and 2 are already defined and just need to be used not redefined.

Lines 4 and 5 are OK if you define the variable hers, but since all your other variables are at the top of main I would put the definition there. That is the bold type in the above code.

You defined a variable "totalCost" but never give it a value. This would be done after you give "monthlyInterestRate" its value because you will need it to figure "totalCost". I ended up at this web sight to get the formula for this https://www.1728.org/loanfrm4.htm That was a lot of fun (not) it took me over an hour to find the mistake I made when entering the formula.

In the end with everything that you input or calculate I came up with this:

 Enter in principal: 100000.00

 Enter in annual interest rate: 4.50

         LOAN SUMMARY
---------------------------------
 Loan amount     $ 100000.00
 Interest rate          4.50%
 Total Interest  $  82406.71
 Monthly payment $    506.69
 Total cost      $ 182406.71

 Ratio paid over principal 82.41%

 Press Enter to display the payment schedule


I admit that I put a little time into setting up the output here so that the decimal points all line up. If you are interested I will help you set up the output this way.

When you get all your variables to match these numbers and can display them on the screen then you will be ready for the for loop to display the schedule.

Hope that helps,

Andy
Topic archived. No new replies allowed.