/************************************************************************
* *
* Program: FinancialPlanner *
* *
* Purpose: This program will help you plan for retirement *
* *
************************************************************************/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
// Get the user's menu choice
cout << "Welcome to the Financial Planner" << endl << endl;
cout << "Work now  Play later!" << endl << endl;
cout << "Choose one:" << endl;
cout << "(1) Solve for future value" << endl;
cout << "(2) Solve for monthly payment" << endl;
cout << "(3) Solve for number of years" << endl;
cout << endl << "Enter your menu choice (1, 2, or 3): " << flush;
int Choice;
cin >> Choice;
// Validate menu choice
if (Choice < 1  Choice > 3) {
cerr << endl << "Jeez! Can't you do anything right?!" << endl;
cerr << "Exiting..." << endl;
exit(1);
}
// Get the known quantities
double PV;
double FV;
double t;
double r;
// Get the future value
if (Choice != 1) {
cout << "Enter the future value: " << flush;
cin >> FV;
}
// Get the monthly payment
if (Choice != 2) {
cout << "Enter the monthly payment: " << flush;
cin >> PV;
PV *= 12;
}
// Get the number of years
if (Choice != 3) {
cout << "Enter the number of years: " << flush;
cin >> t;
}
// Get the interest rate
cout << "Enter the interest rate (as percent): " << flush;
cin >> r;
r /= 100;
// Solve for the missing quantity
switch (Choice) {
// Solve for future value
case 1:
if (r == 0)
FV = PV * t;
else
FV = PV * (pow(1 + r, t)  1)/r;
cout << endl << "The future value is $" << FV << endl;
break;
// Solve for present value
case 2:
if (r == 0)
PV = FV / t;
else
PV = FV / (pow(1 + r, t)  1) * r;
PV /= 12;
cout << endl << "The monthly payment is $" << PV << endl;
break;
// Solve for number of years
case 3: {
if (r == 0)
t = FV / PV;
else
t = log(1 + r*(FV/PV)) / log(1 + r);
int months = round(12*t);
int years = months / 12;
months %= 12;
cout << endl << "The time required is " << years << " years ";
cout << "and " << months << " months" << endl;
break;
}
// This should never happen
default:
cerr << endl << "Houston, we have a problem" << endl;
break;
}
return 0;
}
