Wednesday, January 4, 2017

Programming Challenge 6.10 - Future Value

/* Future Value - Suppose you have a certain amount of money in a
   savings account that earns compound monthly interest, and you
   want to calculate the amount that you will have after a specific
   number of months. The formula, which is known as the future value
   formula, is:
  
   * F = P x (1 + i)^t
  
   The terms are as follows:

   * F is the future value of the account after the specified
     time period.
   * P is the present value of the account.
   * i is the monthly interest.
   * t is the number of months.

   This program uses a function named futureValue to perform this
   calculation. It prompts the user to enter the account's present
   value, monthly interest rate, and the number of months that the
   money will be left in the account. The program displays the
   account's future value. */

/* Prototype: Future Value */
double futureValue (double, double, int);

#include "Utility.h"

int main()
{
   /* Constants: Begin, Try again, Quit */
   const int BEGIN = 1,
      AGAIN = 2,
      QUIT = 3;

   /* Variable: Selection */
   int selection = 0;

   /* Variables: Present value, Monthly interest rate, Future value */
   double presentVal = 0.0,
      monthlyIntRate = 0.0,
      futureVal = 0.0;

   /* Variable: Number of months */
   int numMonths = 0;

   /* Display: Introduction */
   cout << "\t\tAshikaga Bank - Personal Financal Planner\n\n"
      << "Valued customer,\n\n"
      << "Suppose you have a certain amount of money on your savings\n"
      << "account that earns compound monthly interest, and you wish\n"
      << "to know the amount of money you will have after a specific\n"
      << "number of months.\n\n"
      << "For instance:\n"
      << "If you have $10,000 in your savings account, after 120\n"
      << "months, with a monthly interest rate of 9%, your account's\n"
      << "future value would be $24,513.57\n\n"
      << "This program calculates the so called future value, or amount\n"
      << "of money you will have in your savings account in the future.\n"
      << "It also allows you to experiment with the following values to\n"
      << "get different results:\n\n"
      << "* Present Value: The amount of money currently on your account\n"
      << "* Monthly Interest Rate: For instance 9.0%\n"
      << "* The number of months the money will be left alone to earn\n"
      << "  monthly compound interest, 120 months would be 10 years.\n\n";

   do
   {
   /* Display: Menu
      Get: User selection */
   cout << "1. Begin\n"
        << "2. Try again\n"
        << "3. Quit.\n";
   cin >> selection;
 
      /* Validate: Menu selection */
      while (selection < 1 || selection > 3)
      {
         cout << "Please select a valid menu item: ";
         cin >> selection;
      }

      /* As long as the user does not wish to quit the program,
         he or she is asked to enter the present value, monthly
         interest rate, and number of months */
      if (selection != QUIT)
      {
         cout << "\nStep 1: Enter the present value of your\n"
            << "savings account: $ ";
         cin >> presentVal;

         /* Validate: Input */
         while (presentVal <= 0)
         {
            cout << "\nValued customer, you seem to have entered a negative\n"
               << "number as the present value. Only positive values are\n"
               << "accepted.\n"
               << "Please repeat your input: $ ";
            cin >> presentVal;
         }

         cout << "\nStep 2: Now enter the monthly interest rate that\n"
            << "applies to your savings plan (ex: 9.0%): % ";
         cin >> monthlyIntRate;

         /* Validate: Input */
         while (monthlyIntRate <= 0 || monthlyIntRate > 100)
         {
            cout << "\nValued customer, the monthly interest rate can\n"
               << "not be lower than 1, and not greater than 100%.\n"
               << "Please repeat your input: % ";
            cin >> monthlyIntRate;
         }

         cout << "\nStep 3: The last thing you need to enter is the number\n"
            << "of months your money should earn interest. (ex: 120): ";
         cin >> numMonths;

         /* Validate: Input */
         while (numMonths <= 0)
         {
            cout << "\nValued customer, you have to enter a positive value "
               << "for months.\n"
               << "Please repeat your input: ";
            cin >> numMonths;
         }

         monthlyIntRate /= 100;

         /* Call: futureValue */
         futureVal = futureValue(presentVal, monthlyIntRate, numMonths);

         /* Set up: Numeric output formattingi */
         cout << fixed << showpoint << setprecision(2);

         /* Display: Future value */
         cout << "\nFuture Value Calculator - Result\n\n"
            << "Valued customer,\n\n"
            << "According to the information you provided, you currently\n"
            << "have $ " << presentVal << " in your account. "
            << "After a period of " << numMonths
            << "\nmonths, at a monthly interest rate of " << (monthlyIntRate * 100)
            << "% your future\n"
            << "account value will be $ " << futureVal << "\n\n";
      }

      if (selection == QUIT)
      {
         cout << "\nValued customer,\n\n"
              << "Thank you for your trust in our services.\n"
              << "We wish you a successful future, and hope\n"
              << "that you will return soon.\n"
              << "Your Ashikaga Bank Team\n\n";
      }
   } while (selection != QUIT);

    pauseSystem();
    return 0;
}

/* **********************************************************
   Defintion: futureValue

   This function accepts the account's present value, monthly
   interest rate, and number of months as arguments, it then
   calculates and returns the future value of the account,
   after the specified number of months.
   ********************************************************** */

double futureValue(double presentVal, double monthlyIntRate,
                   int numMonths)
{
   /* Variable: Future value */
   double futureVal = 0.0;

   /* Calculation: The future value of the savings account */
   futureVal = presentVal * pow(1.0 + (monthlyIntRate / 12), numMonths);

   /* Return: The future value */
   return futureVal;
}

No comments:

Post a Comment