1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
double GetParam(string prompt, double min, double max)
{
double number;
cout << prompt;
while ((!(cin >> number)) || (number > max || number < min))
{
cin.clear();
cin.ignore();
cout << prompt;
}
return number;
}
void PrintHeader(void)
{
string month = "Month";
string principal = "Principal";
string interest = "Interest";
string balance = "Balance";
cout << month;
cout << setw(15) << principal;
cout << setw(20) << interest;
cout << setw(15) << balance;
cout << endl;
for (int i = 0; i < month.size(); i++)
{
cout << "_";
}
cout << setw(7);
for (int i = 0; i < principal.size(); i++)
{
cout << "_";
}
cout << setw(13);
for (int i = 0; i < interest.size(); i++)
{
cout << "_";
}
cout << setw(9);
for (int i = 0; i < balance.size(); i++)
{
cout << "_";
}
cout << endl;
}
void PrintMonthlyData(int month, double principal,
double interest, double balance)
{
double newbalance, newprincipal, newinterest = 0;
vector <double> newbal;
vector <double> newprinc;
vector <double> newinter;
balance = 0;
while (newinterest < interest * 100)
{
newbalance = (balance + principal) / (1 + (interest / 12));
newbal.push_back(newbalance);
newprincipal = newbalance - balance;
newprinc.push_back(newprincipal);
balance = newbalance;
newinterest = principal - newprincipal;
if (newinterest > interest * 100)
{
break;
}
newinter.push_back(newinterest);
month++;
}
int totalmonth = month;
for (int i = 0; i < totalmonth; i++)
{
cout << month;
month--;
cout << setw(15) << fixed << setprecision(2) << newprinc[i];
cout << setw(20) << setprecision(2) << newinter[i];
cout << setw(15) << setprecision(2) << newbal[i];
cout << endl;
}
return;
}
int main()
{
double monthpay, interestrate;
double year;
monthpay = GetParam("Please enter the monthly payment: ", 1, 100000); //Input monthly payment.
interestrate = GetParam("Please enter the interest rate: ", 0, 1); //Input the Interest Rate.
year = GetParam("Please enter the duration of the loan, in years: ", 1, 100); // Input duration of loan (years).
while (year != (int)year) //Checks to see if years is a integer.
{
year = GetParam("Please enter the duration of the loan, in years: ", 1, 100);
}
cout << endl;
PrintHeader();
PrintMonthlyData(0, monthpay, interestrate, 0);
return 0;
}
|