1 /****************************************************************************
2 * Author: Ryan Flannery
3 * Date: 2 July 2008
4 * File: lab3-task1.cpp
5 * Description:
6 * This program computes the amount of interest a credit card will
7 * accumulate after N years, with an interest rate of R, and a balance
8 * of B.
9 * The values of N, R, and B are supplied by the user.
10 ****************************************************************************/
11 #include <iostream>
12 #include <iomanip>
13 #include <cmath>
14
15 using namespace std;
16
17 int main()
18 {
19 int numYears;
20 float amountCharged, interestRate;
21
22 ///////////////////////////////////////////////////////////////////////////
23 // Step 1. Get the necessary inputs from the user
24 ///////////////////////////////////////////////////////////////////////////
25 cout << "Enter the amount charged: ";
26 cin >> amountCharged;
27
28 cout << "Enter the interest rate as a decimal (E.g. for 6\% enter 0.06): ";
29 cin >> interestRate;
30
31 cout << "Enter the number of years before you'll start paying off: ";
32 cin >> numYears;
33
34 ///////////////////////////////////////////////////////////////////////////
35 // Step 2. Setup Table Header
36 ///////////////////////////////////////////////////////////////////////////
37
38 // The following instructs the "cout" stream to behave differently for
39 // float/double values. Specifically...
40 // 1. "setprecision(2)"
41 // All float/double variables should be displayed with 2 places after
42 // the decimal point (so "4" would be displayed "4.00"). This is
43 // preferred for monetary values.
44 // 2. "fixed"
45 // For float/double variables, use "fixed" notation (1.234) rather
46 // than "scientific" notation (1234e-3).
47 // Nothing is actually output here. Note that this only has to be done
48 // once, and all float/double variables that are output afterwards have
49 // these settings in effect.
50 cout << setprecision(2) << fixed;
51
52 // Output the table header
53 cout << endl
54 << setw(10) << right << "Year"
55 << setw(15) << right << "Amount Owed" << endl
56 << setw(10) << right << "----"
57 << setw(15) << right << "-----------" << endl;
58
59 ///////////////////////////////////////////////////////////////////////////
60 // Step 3. Output the Table Body
61 ///////////////////////////////////////////////////////////////////////////
62 for (int year = 1; year <= numYears; year++)
63 {
64 // compute the amount owed for this year
65 float amountOwed = amountCharged * pow(1.0 + interestRate, year);
66
67 // output this row of the table
68 cout << setw(10) << right << year
69 << setw(15) << right << amountOwed << endl;
70 }
71
72 ///////////////////////////////////////////////////////////////////////////
73 // Step 4. Output the final amount owed
74 ///////////////////////////////////////////////////////////////////////////
75 cout << endl
76 << "After " << numYears << " year(s) you'll owe a total of $"
77 << amountCharged * pow(1.0 + interestRate, numYears) << endl;
78
79 return 0;
80 }