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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include<iostream>
#include <iomanip>
using namespace std;
//*****************************************************
// Function Prototypes
//*****************************************************
char getChoice();
char repeatProgram();
void intro();
void doInvestment();
void doBorrowing();
void investmentInput(double&, double&, double&);
void borrowingInput(double&, double&, double&);
void investmentOutput(double&, double&, double&);
void borrowingOutput(double&, double&, double&);
void investmentSummary(int&, double&, double&, double&);
void borrowingSummary(int&, double&, double&, double&);
//*****************************************************
// Main Program
//*****************************************************
int main()
{
// Variables
char userInput;
//Function calls
intro();
userInput = getChoice();
//Closing remarks
system("pause");
return 0;
}
//*****************************************************
// Function Definitons
//*****************************************************
void intro()
{
cout << " Welcome to the Bank of Kwantlen...again...yay" << endl;
}
char getChoice()
{
char repeat;
char userInput;
cout << " Would you like to invest (i) or borrow (b) from us?";
cin >> userInput;
if (userInput == 'i' || userInput == 'I')
doInvestment();
else if (userInput == 'b' || userInput == 'B')
doBorrowing();
return userInput;
}
void doInvestment()
{
double annualInvestment;
double returnRate;
double endGoal;
double endBalance;
int year;
investmentInput(annualInvestment, returnRate, endGoal);
investmentOutput(annualInvestment, returnRate, endGoal);
}
void investmentInput(double& annualInvestment, double& returnRate, double& endGoal)
{
cout << endl;
cout << " Enter the amount you want to invest annually: ";
cin >> annualInvestment;
cout << " Enter the yearly percentage rate of return (input as decimal): ";
cin >> returnRate;
cout << " Enter investment goal: ";
cin >> endGoal;
}
void investmentOutput(double& annualInvestment, double& returnRate, double& endGoal)
{
int year = 1;
double endBalance;
endBalance = (annualInvestment * returnRate) + annualInvestment;
double newBalance;
cout << "Year" << " Balance (Jan. 1)" << " Balance (Dec. 31)" << endl;
cout << year << " " << fixed << setprecision(2) << annualInvestment << " " << fixed << setprecision(2) << endBalance << endl;
while (endBalance <= endGoal)
{
newBalance = endBalance + annualInvestment;
endBalance = (newBalance * returnRate) + newBalance;
cout << year + 1 << " " << newBalance << " " << endBalance << endl;
year ++;
}
investmentSummary(year, endBalance, endGoal, annualInvestment);
}
void investmentSummary(int& year, double& endBalance, double& endGoal, double& annualInvestment)
{
double extraBalance;
double totalInvestment;
double totalReturn;
totalInvestment = annualInvestment * year;
totalReturn = endBalance - totalInvestment;
extraBalance = endBalance - endGoal;
cout << endl;
cout << "Number of years needed to achieve investment goal: " << year << endl;
cout << "Total investment: " << totalInvestment << endl;
cout << "Total accumulated return on investment: " << totalReturn << endl;
cout << "Final balance exceeds goal by: " << extraBalance << endl;
repeatProgram();
}
void doBorrowing()
{
double loanAmount;
double annualIntRate;
double annualPayment;
borrowingInput(loanAmount, annualIntRate, annualPayment);
borrowingOutput(loanAmount, annualIntRate, annualPayment);
}
void borrowingInput(double& loanAmount, double& annualIntRate, double& annualPayment)
{
cout << endl;
cout << "Enter the amount of the loan: ";
cin >> loanAmount;
cout << "Enter the annual interest rate (input as decimal): ";
cin >> annualIntRate;
cout << "Enter the annual payment amount: ";
cin >> annualPayment;
}
void borrowingOutput(double& loanAmount, double& annualIntRate, double& annualPayment)
{
int year = 1;
double endBalance;
endBalance = ((loanAmount * annualIntRate) + loanAmount) - annualPayment;
double newBalance;
cout << "Year" << " Balance (Jan. 1)" << " Balance (Dec. 31)" << endl;
cout << year << " " <<fixed << setprecision(2) << loanAmount << " " << fixed << setprecision(2) << endBalance << endl;
while (endBalance > 0)
{
newBalance = endBalance;
endBalance = ((newBalance * annualIntRate) + newBalance) - annualPayment;
cout << year + 1 << " " << newBalance << " " << endBalance << endl;
year ++;
}
borrowingSummary(year, loanAmount, annualPayment, endBalance);
}
void borrowingSummary(int& year, double& loanAmount, double& annualPayment, double& endBalance)
{
double totalPayments;
double totalInterestCharged;
totalPayments = (year * annualPayment);
totalInterestCharged = (totalPayments + endBalance) - loanAmount;
cout << endl;
cout << "Number of years needs to pay back loan: " << year << endl;
cout << "Total of payments: " << totalPayments << endl;
cout << "Total of interest charged: " << totalInterestCharged << endl;
cout << "Balance at end of period: " << endBalance << endl;
repeatProgram();
}
char repeatProgram()
{
char repeat;
cout << "Would you like to make another investment/loan calculation? (y/n) ";
cin >> repeat;
return repeat;
}
|