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 |
| Setting Precision | Setting 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> |
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:
| 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.
|
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name
// Lab #4: Loan Amortization Tables
/* Put what your program does here */