# Amortization Table

• 11-03-2008
jap2010
Amortization Table
Hello,

I have written code to calculate monthly payment for a loan. Now, I have to display an amortization table, which I don't know how to do. I have posted what I have written so far. Any help will be greatly appreciated. Thanks!

Code:

#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>
using namespace std;

int main ()

{
//Stores variables
int loan_amount;
double interest_rate;
int loan_length;
double monthly_payment;

//Prompts for and reads loan amount
cout << "Enter the amount of the loan: ";
cin >> loan_amount;
cout << endl;

//Prompts for and reads interest rate
cout << "Enter the Annual Interest Rate: ";
cin >> interest_rate;
cout << endl;

//Prompts for and reads loan length
cout << "Please enter the length of the loan in years: ";
cin >> loan_length;
cout << endl;

//Calculate monthly payment
interest_rate = interest_rate / 1200;
monthly_payment = (interest_rate + (interest_rate/(pow (1+interest_rate, 12*loan_length)-1))) * loan_amount;

//Format output
cout << setprecision(2) << fixed << showpoint;

//Display monthly payment
cout << "The monthly payment is " << monthly_payment << endl << endl;

//Display amortization table

}

• 11-03-2008
Angus
I'm not sure exactly what your problem is. I'm not certain how to display an amortization table either, but that problem is a matter of accounting, not computer programming.
If your problem is not knowing how to format your output, that's something else. I don't know if IOstreams is good for formatted output (although IMO IOstreams isn't good for anything, so I don't use it) so I'd use the ANSI C *printf() family of function. Whatever you decide to do, you should probably not mix your use of IOstreams and *printf()s, which means you should take out all your couts and replace them w/printf()s.
• 11-03-2008
BobS0327
Quote:

Now, I have to display an amortization table, which I don't know how to do
Here is the pseudo code to display your amortization table. This is based on the code that you posted:

Quote:

LOOP THE FOLLOWING CODE FOR TOTAL MONTHS OF LOAN

MONTHLY INTEREST PAID EQUAL TO AMOUNT OF LOAN TIMES INTEREST RATE
MONTHLY PRINCIPAL PAID EQUAL TO MONTHLY PAYMENT MINUS MONTHLY INTEREST PAID
LOAN BALANCE EQUAL TO LOAN AMOUNT MINUS MONTHLY PRINCIPAL PAID
IF LOAN BALANCE LESS THAN ZERO THEN LOAN BALANCE EQUAL ZERO
LOAN AMOUNT = LOAN BALANCE
PRINT THE LOAN BALANCE AND PRINT THE MONTHLY INTEREST PAID
• 11-03-2008
Raigne
iostreams are very useful and are very good for a lot of things. Recommending that someone not use C++, and instead use C is very odd i think. There is nothing wrong with iostreams and in the majority of cases have the same performance, so really it is preference.

Also, I do not see why it is bad to mix iostreams with printf() or whatever. They are places where one may make more since than the other.
One bonus to iostreams is that they can be extended to do a vast amount of things, while printf() and all similar are not quite so simple to extend. Take for example making a fwrite() function that outputs a particular class. You either to to convert your class to something that fwrite can understand or re-make the fwrite() function. While with iostream you can overload the operator<<() and operator>>() to be able to handle all iostreams. Whether it be file/console input/output.

I will stop ranting, but why recommend something that is more complicated, and has no apparent advantages over the C++ version.
• 11-04-2008
Angus
Quote:

Originally Posted by Raigne
iostreams are very useful and are very good for a lot of things. Recommending that someone not use C++, and instead use C is very odd i think. There is nothing wrong with iostreams and in the majority of cases have the same performance, so really it is preference.

Also, I do not see why it is bad to mix iostreams with printf() or whatever. They are places where one may make more since than the other.

*printf() buffers, and so does iostreams. If you write to both, you might find that when the descriptors flush, your data is out of order.

Quote:

I will stop ranting, but why recommend something that is more complicated, and has no apparent advantages over the C++ version.
One problem I had is that when developing a CGI was if I sent something to cerr each character showed up as its own entry in the error log. This may not have been iostreams's fault, (although, it did make me wonder about its performance) but it made it unsuitable for this application. I don't remember exactly what other problems I had, but I found it unpredictable and frustrating. Iostreams might be a simpler to use for simple applications, but I found that I was just as happy using ANSI C.