CSC 306 Introduction to Programming with C++

Creating a Loan Amortization Table

Chapter 5


Objectives

Important Definitions


Manipulators Functions for Formatting

We have already come across the member functions setf and precision for formatting output streams. Here is another one called width, which tells cout how many spaces to give the following output string. If the output takes less than the number of spaces in the width member function, the additional spaces are padded out.

// Use cout's member function "width" which sets the number of 
// spaces into which the next item of output will be fit.
// Note that it works for only the next item of output.
cout.width(4); 
cout << "4" << endl; // this will output the string "   4" and newline
cout.width(4);
cout << "36464" << endl; // this will output the string "36464" and newline
Using member functions like these are one way to set the output format. A manipulator function is another mechanism to accomplish this, although one calls them in a fashion that differs from what we have encountered so far.
Setting PrecisionSetting the Width
Member
Functions
 cout.setf(ios::fixed); 
 cout.precision(2);     // set 2 digits precision

 cout << "$" << 10.3 << endl;
 cout.setf(ios::fixed); 
 cout.width(10);

 cout << "output:" << 10.3 << endl;
Manipulator
Functions
 cout.setf(ios::fixed); 

 cout << "$" << setprecision(2) << 10.3 << endl;
 cout.setf(ios::fixed); 

 cout << "output:" << setw(10) << 10.3 << endl;
Output: The program will output the string "$10.30", which is the dollar sign, followed by the number 10.3 with 2 digits of precision:
$10.30
The program will output the string "output:", followed by 10-4=6 spaces, followed by the number 10.3:
output:       10.3

Both of these functions are described in the iomanip library, so to use them, you must use the include directive:
#include <iomanip>
For an example of formatting output, take a look at Neatify.cpp encountered in assignment 10.

Lab Specifics

A loan is essentially money borrowed in order to pay for a car or a house. The amount you get is called the principle, and you pay back the loan at a specific rate of interest (this is always given as an annual rate even when you make monthly payments). What makes repayment complicated is that though the fixed monthly payment amount is constant, some of the payment money pays off the interest and some pays back the principle. However, the monthly payment pays off the interest you owe for that month first, and whatever is left goes to pay off the principle. The amount you pay for interest and the principle change from month to month.

What you are to do

These labs must be done individually.

You are to create a program in a source file called YourLastName_L04.cpp that will calculate a monthly payment and a loan amortization table. This program will do the following:

  1. It will prompt the user to provide important information such as:
    • The initial amount of the loan (original principle)
      Your program should work for both positive and negative values for principle, as some people think of a loan in negative dollars and some think of a loan in positive dollars.
    • The annual interest rate
      Be sure to be very clear about how to enter the interest rate. i.e. 7% or 0.07??
    • How many years you will take to repay the loan
    • File name of the desired output file for the computed amortization table

  2. Given the amount of principle the user borrowed, the annual interest rate per year for years years, your program is to use them to compute the monthly payment using the following formula:

    Note that your program should round the payment value to the nearest penny---people do not pay a fraction of a penny. Make sure that your program stores the value of payment rounded correctly or else the resulting output will be wrong.

  3. Your program should write information to the output file; you asked the user for the name of that file at the start of the program. In this file, create a columnar table of the following information using formatted output, with a descriptive header in the first line(s) of output describing the contents of the column. The following columns must be output:
    • Payment number
    • Payment date (see note below)
    • Payment (this will not change from month to month)
    • Interest paid in this payment
    • Principle paid in this payment
    • Principle still owed (see note below)
    Notes The payment for a loan is due on the first of every month, so if the loan started on June 1, 2006, the first payment would be July 1, 2006. Your program can output the date in "mm/dd/yyyy" format (i.e. 01/02/2006 for January second, 2006)
    The interest that is paid in a given month is interest only on the principle that remained in the previous month, and the amount of principle will decrease over time.
    The payment for a given month consists of both interest and money for the principle. Suppose the payment is X and interest is Y for a given month. Exactly X-Y goes towards paying off the principle. The final payment should exactly equal the interest due plus the remaining principle. Because your program rounds the payment value, the final payment will probably need to be adjusted by no more than a small amount in order that your final payment is correct. This table is called a loan amortization table.

  4. After the amortization table has been created, the program should then write the following totals to the file using some readable formatting.
    • Total interest paid
    • Total amount paid

Be sure to: When you have completed your program and have it working to your satisfaction, drop the source code only (YourLastName_306L4.cpp) into the CSC306_L04 dropbox on the Academic server.
Back to Introduction to Computer Programming with C++ Homepage