The mortgage program extends the payment program example presented in the previous chapter. Review the comments in payment.cpp before proceeding with this extended version. The mortgage program calculates a monthly mortgage payment based on the principal (i.e., the amount borrowed), the annual percentage rate (APR), and the loan duration in years. It then prints a complete amortization table that lists each payment:
P | R | N | payment | |
---|---|---|---|---|
Case 1 | 100000 | 8% = 0.08 | 30 | 733.76 | Case 2 | 100000 | 4% = 0.04 | 15 | 739.69 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int years; double principal; double apr; cerr << "Please enter the principal: "; // prompt for the amount to borrow cin >> principal; // cerr "behaves" like cin, explained below cerr << "Please enter the APR: "; // prompt for annual percentage rate cin >> apr; cerr << "Please enter the number of years: "; // prompt for the loan length in years cin >> years; double R = apr / 12; // convert APR to periodic interest rate int N = years * 12; // convert number of years to periods double payment = principal * R / (1 - pow(1 + R, -N)); // monthly payment cout.setf(ios::fixed); // fixed point output See fixed cout.precision(2); // two places after the "." See precision cout << "My payment is: " << payment << endl; // print the monthly payment double balance = principal; // define and initialize table variables double total_interest = 0; double total_principal = 0; for (int i = 1; i <= N; i++) // loop prints one line for each payment { double to_interest = balance * R; // payment amount applied to interest double to_principal = payment - to_interest; // payment amount applied to interest principal total_interest += to_interest; // total interest over the life of the loan total_principal += to_principal; // total principal paid over the life of the loan balance -= (payment - to_interest); // outstanding principle after a payment cout << setw(3) << i << ": " << setw(10) << balance << // prints table rows setw(10) << to_interest << setw(10) << to_principal << endl; } cout << endl << setw(25) << total_interest << setw(10) << // prints totals following the table total_principal << endl; return 0; }
Using the first test case, the for loop produces 360 output lines, which scroll off the console window. Depending on how you run the program, 360 lines of output may also be too many lines to allow you to scroll back to the top. If we run the program from a command line prompt, it is possible to redirect the output to a file:
C:\>mortgage > amortization.txt
Used this way, > is called the file redirection operator. We can imagine the redirection operator acting like a funnel, catching the program's output and funneling or sending it to a file. If amortization.txt doesn't exist, the operation creates and fills it with the program's output; if the file exists, the operation discards its previous contents before filling it.
However, if the program uses cout for all output operations, the redirection operator redirects all output to the file, including the three prompts at the beginning of the program. In that case, the user doesn't see the prompts and doesn't know the program is waiting for input. We solve the problem by separating the output into two streams: one stream (the payment, table, and totals) is sent to cout and is redirected to the file. However, the program sends the prompts cerr. The file redirection operator only redirects cout to the file, so the prompts, sent to cerr, still display on the console.
Please enter the principal: 100000 Please enter the APR: 0.08 Please enter the number of years: 30 My payment is: 733.76 1: 99932.90 666.67 67.10 2: 99865.36 666.22 67.55 3: 99797.36 665.77 68.00 4: 99728.91 665.32 68.45 5: 99660.01 664.86 68.91 6: 99590.64 664.40 69.36 . . . 355: 3596.57 28.68 705.09 356: 2886.79 23.98 709.79 357: 2172.27 19.25 714.52 358: 1452.98 14.48 719.28 359: 728.91 9.69 724.08 360: -0.00 4.86 728.91 164155.25 100000.00