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
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int getInvest();
double getMonths();
double getAnnualRate();
double monthlyRate(double);
double returnFutureInvestment(int, double, double);
//In the main function there is a do while loop with a nested if statement. The do while
// will continue until the investment is 0. The nested if statement will allow it to continue
// with the rest of the program as long as the first investment is not 0.
int main()
{
double invest, months;
double yearRate, monthRate, futureAmount;
do
{
invest = getInvest();
if (invest != 0)
{
yearRate = getAnnualRate();
months = getMonths();
monthRate = monthlyRate(yearRate);
futureAmount = returnFutureInvestment(invest, months, monthRate);
cout << "After " << months << " your investment of $" << invest << " will be worth $"
<< fixed << setprecision(2) << futureAmount << endl << endl;
}
}while (invest !=0);
return 0;
}
//this function asks the user for the investment amount. if it is zero or greater
//than 10000, it will return the investment amount. if it is not, then it will continue
//to ask for a correct investment amount.
int getInvest()
{
int invest;
cout << "Enter the amount of the investment: ";
cin >> invest;
while (invest < 10000 && invest != 0)
{
cout << "The investment needs to be greater than 10,000" << endl;
cout << "Enter the amount of the investment: ";
cin >> invest;
}
return invest;
}
//This function asks for the number of months invested. it will return values that are negative.
double getMonths()
{
int months;
cout << "Enter the amount of months you want to invest: ";
cin >> months;
while (months < 0)
{
cout << "The number of months can not be a negative" << endl;
cout << "Enter the amount of months you want to invest: ";
cin >> months;
}
return months;
}
//This function asks for the annual rate. it will not accept a negative.
double getAnnualRate()
{
double rate;
cout << "Enter the annual interest rate: ";
cin >> rate;
while (rate < 0)
{
cout << "The annual interest rate can not be a negative" << endl;
cout << "Enter the annual interest rate: ";
cin >> rate;
}
return rate;
}
//This calculates the monthly rate from the annual rate. I could have done it in the main
//but i had already started making a function for it, oh well.
double monthlyRate(double rate)
{
rate = (rate/12)/100;
return rate;
}
//This function takes the investment, months, and the monthly rate and calculates
//the future investment amount.
double returnFutureInvestment(int invest, double months, double monthRate)
{
double futureAmount, x;
futureAmount = (invest * pow((1 + monthRate), months));
/* for( x=months ; x>0; x--)
{
invest += (invest * monthRate);//(principle *rate) + principle
}
futureAmount = invest;
*/
return futureAmount;
}
|