Super confused plz help

Sorry for not being clear in my question. My problem is I am trying to make an amortization table which would depending on the input of the user will display the amount of the payment will go to principle and interest and decrease the balance over the years. For example if they input 30 years it would show how much of the payment would actually go to the balance. I am very confused how to do this. Please point me in the right direction.


Hello I am new to programming and need help with creating an amortization table. I have search previous forums and they are all a couple of years old in older versions of Visual Studio. I am now very confused any help or pointing me in the right direction will be greatly appreciated.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <math.h>
#include <string>
#include <iomanip>				
#include <fstream>		


using namespace std;

int main()
{
	int counter = 0;
	float month=1;
	float mortgageAmt, mortgageAmtB, loanLength;
	float totalLoan, totalInterest, totalPymts, mthlyPymt;
	float pymtYearly, interestRate, monthPaidInt, monthPrinciple, 
        newmthBalance;
	float mthBalance, balanceA, balanceB, decimalInterestRate;
	float loanLengthB, loanLengthC;
	float totalLoanB, totalInterestB, totalPymtsB, mthlyPymtB, 
        mthlyPymtC;
	float interestRateB, totalInterestC, totalPymtsC;
	float balanceC, balanceD, decimalInterestRateB;
	string reply;

	cout << "Welcome to Nadia's Mortgage Calculator!\n";
	cout << "Enter the amount you would like to borrow: \t $";
	cin >> mortgageAmt;
	cout << "Enter your interest rate: \t";
	cin >> interestRate;
	cout << "How many payments will you make each year?   ";
	cin >> pymtYearly;
	cout << "How long in years will your loan last:  ";
	cin >> loanLength;

	decimalInterestRate = (interestRate / 100) / 12;
	totalPymts = pymtYearly * loanLength;
	balanceA = mortgageAmt * (pow((1 + decimalInterestRate), totalPymts)) * decimalInterestRate;
	balanceB = (pow(1 + decimalInterestRate, totalPymts)) - 1;
	mthlyPymt = balanceA / balanceB;
	totalInterest = (mthlyPymt * totalPymts) - mortgageAmt;
	totalLoan = mortgageAmt + totalInterest;





	cout << "The total amount paid back will be \t $" << totalLoan << endl;
	cout << "Your total amount paid in interest will be \t $" << totalInterest << endl;
	cout << "The amount of payments you will make is \t  " << totalPymts << endl;
	cout << "Your monthly payment will be \t $" << mthlyPymt << endl;
	cout << "What else would you like to do?" << endl;
	cout << "Would you like to: change length of the loan (A), make an amortization table (B), or exit (C)." << endl;
	cin >> reply;
	if (reply == "A" || reply == "a")
	{
		cout << "How long in years will your loan last:  ";
		cin >> loanLengthB;

		totalPymtsB = pymtYearly * loanLengthB;
		balanceC = mortgageAmt * (pow((1 + decimalInterestRate), totalPymtsB)) * decimalInterestRate;
		balanceD = (pow(1 + decimalInterestRate, totalPymtsB)) - 1;
		mthlyPymtB = balanceC / balanceD;
		totalInterestB = (mthlyPymtB * totalPymtsB) - mortgageAmt;
		totalLoanB = mortgageAmt + totalInterestB;

		cout << "The total amount paid back will be \t $" << totalLoanB << endl;
		cout << "Your total amount paid in interest will be \t $" << totalInterestB << endl;
		cout << "The amount of payments you will make is \t  " << totalPymtsB << endl;
		cout << "Your monthly payment will be \t $" << mthlyPymtB << endl;
		cout << "Thank you for using my Mortgage Calculator!" << endl;

	}

	if (reply == "B" || reply == "b")
	{
		cout << "Enter the amount of the loan: \t $ " ;
		cin >> mortgageAmtB;
		cout << "What is the interest rate?  \t" ;
		cin >> interestRateB;
		cout << "How long in years will your loan last?  \t";
		cin >> loanLengthC;
		cout << "What is the monthly payment amount?  \t$ ";
		cin >> mthlyPymtC;


		cout << "\nPayment schedule for $"<<mortgageAmtB<< " loan at "<<interestRateB<< "% annual interest rate and monthly payment of $" << mthlyPymtC<<" for "<<loanLengthC <<" years.";
		cout << "\nMonth\tInterest Payment\tPrinciple Payment\t\tBalance";
		cout << "\n---------------------------------------------------------------------\n";

		decimalInterestRateB = (interestRateB / 100) / 12;
		monthPaidInt = mortgageAmtB * decimalInterestRateB;
		monthPrinciple = mthlyPymtC - monthPaidInt;
		mthBalance = mortgageAmtB - mthlyPymtC;
		newmthBalance = mthBalance - monthPrinciple;
		


		

		if (mortgageAmtB > mthlyPymtC)
		{
			
			totalPymtsC = 12 * loanLengthC; 
			monthPaidInt = mortgageAmtB * decimalInterestRateB;
			mortgageAmtB = mortgageAmtB + monthPaidInt - mthlyPymtC;
			totalInterestC = (mthlyPymtC * totalPymtsC) - mortgageAmtB;
			mthBalance = mortgageAmtB - mthlyPymtC;
			newmthBalance = mthBalance - monthPrinciple;
			

			for (int counter= 1; counter <= loanLengthC; counter-=mthlyPymtC)
			{
				cout << "\n\t" << counter << "\t" << monthPaidInt << " \t" << monthPrinciple << "\t" << (mthBalance - monthPrinciple);
			}
			

		}
		else {
			if (mthlyPymtC >= mortgageAmtB)

				monthPaidInt = mortgageAmtB * decimalInterestRateB;
			mthlyPymtC = newmthBalance + monthPaidInt;
			mortgageAmtB = mthlyPymtC - mthlyPymtC;


			
		}


		cout << "\n---------------------------------------------------------------------\n";
	}
	else {
		if (reply == "C" || reply == "c")
			cout << "Thank You for using my Calculator!!" << endl;
	}
}
Last edited on
if you actually ask a question you may have better answers.


line 112: for (int counter= 1; counter <= loanLengthC; counter-=mthlyPymtC)
please explain in simple words what that loop is supposed to do.
Hello jumbee2,

As ne555 has pointed out you should ask a better question or point out where you are having a problem.

Looking over your code you should use <cmath> not "math.h". Go to the top left and where it says "Reference" and open that link in a new tab. In the right frame you will see a list of "<c?????>" header files with their C header file replacement. I have found the Reference section to be quite helpful along with https://en.cppreference.com/w/

"double" is the preferred floating point number type and not float. The "float" has less precision and may not store some numbers as well. This may not be the best example, but "4.1" is stored in a "double" as "4.09999999999999964473", but when you only print two decimal places it rounds up to "4.10" which is what you started with.

After that I give you credit for naming your variables with good names. Although some of those variables may need to be initialized when they are defined. I am not sure yet which ones until I can compile the program and see how it runs. I may get warnings or errors about uninitialized variables.

As ne555 has pointed out with line 112 it is very possible that you may have an endless loop with what you have as "counter" may never become large enough to exceed "loanLengthC".

When I load the program and see how it compiles and runs I will know more.

If you have any test data, i.e., known input and what it should produce it would be helpful.

Hope that helps,

Andy
Andy,

Thank you so much for that explanation and tips it was incredibly helpful and helped me understand.
ne555,

Honestly that is where my problem is. I know that is wrong. I was trying to create a loop which depends on the input for loanLengthC. That would show how much of the payment was going to interest and principle and the remaining balance. However I am not even sure if a loop is what I should be using. Any pointers that you can give would be really helpful.
Hello jumbee2,

You are welcome.

Now that I have had a change to run the program I have noticed some problems.

When I mentioned "cmath" it got by me to mention "fstream". The "f" here stands for file and the header file is for working with input and output files. Neither of which you are using in your program, so it is extra code that you do not need for now.

I would replace "fstream" with "iomanip". It is of greater use to you in this program. The line of code that you will nee is: std::cout << std::fixed << std::showpoint << std::setprecision(2);. The "std::fixed" tells the "std::cout" to use a decimal number and not scientific notation. the "std::showpoint" says to print ".00" is it should come up and "std::setprecision(2)" says to print two decimal places. This line only needs to be done once and will affect every "std::cout" and "std::cin" that comes after it until you change something, so I usually put it at the top of a program, but anywhere before the first "std::cout" that prints a floating point number will work.

The two else tend to through the rhythm off when reading. Everything else you do is to press enter and put the opening { on the next line and when the closing } is in the same column with the code indented it makes easier to read and follow. This is worth a look if you have not seen it https://en.wikipedia.org/wiki/Indentation_style#Styles Of the styles presented I prefer the Allman style and as you read through you may find that there is more than one name for this style.

The for loop has several problems:

First you define "counter" as an int and in the middle section compare it to a "double". This produced a warning, but it will not stop the code from compiling or the program from running, but there may be a time when it does not work properly. For now what I did is: static_cast<double>(counter) <= loanLengthC. It did eliminate the warning, but still it may not be the best solution. There are still some things I want to try.

Next there is the <= loanLengthC. Wrong variable. You need the variable that contains the number of total months in the loan not the number of years.

In the third section counter-=mthlyPymtC should be counter++ by subtracting "mthlyPymtC" you can end up with a negative number which makes the whole for loop an endless loop because "counter" will never become greater than "loanLengthC".

The other part I noticed is you start your program by entering information like "mortgageAmt", "interestRate", "pymtYearly" and "loanLength". Then in the if statement for "B" you ask for the same information again and use different variables for the information that you already have.

My point is that in both "A" and "B" you should be using information that you already have and nor creating duplicate variables that you do not need.

Right now I am working on changing "B" to use the variables where information was first entered.

As I learn more I will let you know what I find and also show you how you can make the for loop print something that looks nicer.

Hope that helps,

Andy
Why does the user have to enter new loan data to make an amortization table?

The thing with an amortization table is that the amount of principal and interest changes each month. So for each month you must compute the interest paid and new balance. Right now your code assumes that they stay the same.
Andy,

Thank you so much for breaking everything down so clearly that even a novice like me totally understood. I am updating the code as you suggest and will give an update once done.

Update.

I did what you suggested and it helped a lot thank you. However I have no clue on how to make the loan amount decrease over time. I appreciate any help.
Last edited on
Hello jumbee2,

It is a good subject "Super confused" because I am super confused at times. When I first took a quick look at your variable names I noticed that you did a good job of naming the variables until I started working with the program and discovered that names ending with "B", "C", etc. my first thought was duplicate variables, but I did see a use of a second set in the if statements because this would allow you to keep information in the original variables in case you need them later.

To make things a little easier to understand I rearranged the beginning of the program and added some to the "cout" statements that you can either use or not. At least it gives you an idea of what you could do and some help of how to do it in the future. I refer to the "setw()"s.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>	// <--- Changed from "<fstream>" that is not needed.	
#include <cmath>
#include <string>

//using namespace std;  // <--- Best not to use.

int main()
{
	int counter{}, totalPymts;
	//double month{}; // <--- Not used.

	double
		mortgageAmt{ 100000.00 },
		interestRate{ 4.5 }, loanLength{ 30 },
		pymtYearly{ 12 };
	double
		totalLoan{},
		totalInterest{},
		mthlyPymt{},
		decimalInterestRate{};
	double
		monthPaidInt{},
		monthPrinciple{},
		newmthBalance{},
		mthBalance{};

	// <--- Moved to if statements for "B".
	//double mortgageAmtB{}, interestRateB{}, loanLengthB{}, decimalInterestRateB{};
	//double totalLoanB{}, totalInterestB{}, totalPymtsB{}, mthlyPymtB{};

	 // <--- Also moved to if statements for "B", but not really needed.
	//double totalInterestC{}, totalPymtsC{};
	//double loanLengthC{}, mthlyPymtC{};

	double balanceA{}, balanceB{}, balanceC{}, balanceD{}; // <--- Not sure yet what these are used for.

	char reply{'B'}; // <--- Should be a "char" not a "string", but the string will work.

	constexpr size_t WIDTH{ 44 };  // <--- Added.

	std::cout << "\n Welcome to Nadia's Mortgage Calculator!\n";

	std::cout << "\n" << std::setw(WIDTH) << "Enter the amount you would like to borrow:" << " $" << mortgageAmt;
	//std::cin >> mortgageAmt;
	std::cout << "\n" << std::setw(WIDTH) << "Enter your interest rate:" << ' '/*std::setw(6)*/ << interestRate << '%';
	//std::cin >> interestRate;
	std::cout << "\n" << std::setw(WIDTH) << " How many payments will you make each year?:" << std::setw(3) << pymtYearly;
	//std::cin >> pymtYearly;
	std::cout << "\n" << std::setw(WIDTH) << " How long in years will your loan last:" << std::setw(3) << loanLength;
	//std::cin >> loanLength;

	decimalInterestRate = (interestRate / 100.0) / pymtYearly;
	totalPymts = static_cast<int>(pymtYearly * loanLength);
	balanceA = mortgageAmt * (pow((1 + decimalInterestRate), totalPymts)) * decimalInterestRate;
	balanceB = (pow(1 + decimalInterestRate, totalPymts)) - 1;
	mthlyPymt = balanceA / balanceB;
	totalInterest = (mthlyPymt * totalPymts) - mortgageAmt;
	totalLoan = mortgageAmt + totalInterest;

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Added.

	std::cout << "\n\n" << std::setw(WIDTH) << "The total amount paid back will be:" << " $" << totalLoan << std::endl;
	std::cout << std::setw(WIDTH) << "Your total amount paid in interest will be:" << " $" <<totalInterest << std::endl;
	std::cout << std::setw(WIDTH) << "The amount of payments you will make is:" << ' ' <<  totalPymts << std::endl;
	std::cout << std::setw(WIDTH) << "Your monthly payment will be:" << " $" << mthlyPymt << std::endl;
	std::cout << "\n What else would you like to do?" << std::endl;
	std::cout << "\n Would you like to:\n (A) change length of the loan \n (B) make an amortization table\n (C) exit" << std::endl;
	std::cout << " Enter choice: ";
	//std::cin >> reply;  

For a little research and some help I have been using this site https://www.calculator.net/amortization-calculator.html?cloanamount=100000&cloanterm=30&cinterestrate=4.5&printit=0&x=81&y=20 This is for a loan amount of 100,000.00 at 4.5% for 30 years. I can not say if it is the best or the worst, but it is something to go by to check your work and every little bit helps.

The comments in the code should help to understand what is happening.

I moved "totalPymts" from a "double" to the "int" because you only need the whole number here and it works out better in the long run. This did mean I had to change the line totalPymts = pymtYearly * loanLength; to totalPymts = static_cast<int>(pymtYearly * loanLength); because you are trying to store a "double" into an "int" and the compiler gives a warning about possible data loss because it will drop the decimal portion of the "double" and only store the whole number. Either way works because the result of the calculation is a whole number, but the second example would be more proper.

I changed "reply" from a "string" to a "char" because you are using only one letter and there is no need to use a string for this. FWIW, for what this variable is used for I tend to use "choice" for the name as it is a better description of what it is used for. Whether you use "choice" or "replay" it is up to you, but I would suggest that you be consistent in its use. When writing code I feel it helps when you use certain words for the same task like "choice" when you are making a menu choice.

Two things about the way the "doubles" are done.

First; by putting each name on a different line it helps a bit to understand what you have.

Second; you will notice that some of these variables are initialized with a number. This is a little trick you can use so that you do not have to enter information every time the program runs and you will always be using the same data so you can see if your calculations are correct. This along with changing the "cout" statements as I did and putting a comment on the "cin" statements saves you some time. You will see I did the same thing with "reply" and the "cin" statement used for it.

The other variables have a set of empty{}s which initializes the variables to "0.0" (for "double"s), zero (for "int"s), and '\' (for "char"s). "std::string"s are empty to start with and do not need initialized unless there is something you want in the string.

The next two sections I left there commented out, but eventually they should be removed.

Line 40 I added to help with the "setw()" on the input and output. Use if you want or if you are not ready for this yet you can do something different.

Lines 44 - 70 give you an idea of what you can do to format the input and output to something that looks nicer.

Line 61. Is the real reason for including "<iomanip>". "std::fixed" tells the "cout" statements to print a "decimal" number not a "scientific" number. The "std::showpoint" is used to print ".00" should it occur. The "std::setprecision(2)", the most important part, says to print only 2 decimal places. This along with "std::setw()" help to align the decimal point of each row in the same column. In another program you might need to change the "2" to a "3" or "4" or even larger depending on the need.

The next part is the if statement for "B". I need to change a few things before I go over it to show you what I have done. Based on dhayden's post what you need to do is put the calculations inside the for loop before the cout statement. Also if you want to keep track of totals you will need to code for that before the cout statement. That part I have not worked on yet. Still trying to get the loop to work correctly.

There is a start for you for now.

Hope that helps,

Andy
Hello jumbee2,

I started to ask this until I got into something else and lost mey thought.

In the if statement for "B" is there a reason for entering the information there?

My first thought is that yo should be using what has already been entered.

Andy
Topic archived. No new replies allowed.