SOURCE FILE: ex0519.cpp
//
// Example 5.19. This program computes the amount of money in
// the bank after a certain number of years. It also computes
// how many years it would take to arrive at a given amount.
// The bank compounds interest annually.
//
#include <iostream.h>
#include <math.h>
#include "money.h"
money ReadPrincipal(void);
float ReadRate(void);
int main(void)
{
int menu(void); // prototypes
void FindAmount(void);
void FindYears(void);
void error(int choice);
int choice; // menu choice
choice = menu();
switch (choice)
{
case 1: FindAmount();
break;
case 2: FindYears();
break;
default: error(choice);
}
return 0;
}
//
// This function prints a menu of choices, reads and
// returns the user's integer choice, 1 or 2; all other
// choices are in error.
// Pre: none
// Post: Function has returned user's choice from the menu.
//
int menu(void)
{
int choice; // menu choice
cout << "This program makes savings computations for an\n";
cout << "account with interest compounded annually. It will\n";
cout << "ask you the one-time deposit amount and the annual\n";
cout << "interest rate. You have two choices: Given a number\n";
cout << "of years, the program will return the final amount;\n";
cout << "given a final amount, the program will tell the\n";
cout << "number of years you must leave the money in the bank.\n\n";
cout << "Choose from the following:\n\n";
cout << "1. Given a number of years, calculate amount\n";
cout << "2. Given a desired amount, calculate years\n\n";
cout << "Your choice: ";
cin >> choice;
return choice;
}
//
// This function obtains principal, interest rate, and years of
// deposit; determines if an input error occurs; and if
// not, calculates and prints final amount.
// amount = principal * (1 + rate)^years
// Pre: none
// Post: If no errors, final amount has been displayed.
//
void FindAmount(void)
{
int ReadYears(void); // prototype
float rate; // interest rate
int years; // number of years in bank
money principal, // object for original deposit
ZeroDollars; // object representing $0.00
principal = ReadPrincipal();
rate = ReadRate();
years = ReadYears();
if (principal.LessThan(ZeroDollars) ||
principal.EqualTo(ZeroDollars) ||
(rate <= 0) ||
(years <= 0) ) //error
cout << "\nInvalid input. Run program again.\n";
else
{
principal.MultiplyBy(pow(1 + rate, years));
cout << "\nYou will have $";
principal.display();
cout << " in the bank.\n";
}
}
//
// This function obtains principal, interest rate, and amount
// desired; determines if an input error occurs; and if
// not, calculates and prints number of years needed.
// years = smallest integer greater than or equal to
// log(amount/principal)/log(1 + rate)
// Pre: none
// Post: The number of years needed or an error message
// has been displayed.
//
void FindYears(void)
{
money ReadAmount(void); // prototype
float rate; // interest rate
int years; // number of years in bank
money amount, // final amount in bank
principal, // original deposit
ZeroDollars; // object representing $0.00
principal = ReadPrincipal();
rate = ReadRate();
amount = ReadAmount();
if ((!principal.LessThan(amount)) ||
principal.EqualTo(ZeroDollars) ||
principal.LessThan(ZeroDollars) ||
(rate <= 0)) //error
cout << "\nInvalid input. Run program again\n";
else
{
years = ceil(log(amount.quotient(principal))
/ log(1 + rate));
cout << "\nYou will need the account for "
<< years << " years.\n";
}
}
//
// This function reads and returns principal
// Pre: none
// Post: The money object for the principal was returned.
//
money ReadPrincipal(void)
{
double deposit; // principal as a double
cout << "\nGive the original deposit: ";
cin >> deposit;
money principal(deposit); // original deposit object
return principal;
}
//
// This function reads and returns interest rate, -1 in
// case of error
// Pre: none
// Post: Floating point interest rate has been returned, -1
// in case of error.
//
float ReadRate(void)
{
float rate; // interest rate
cout << "Give the interest rate as a decimal number\n";
cout << " For example, for 6.25% type 0.0625: ";
cin >> rate;
if ((rate <= 0.0) || (rate >= 0.2)) // error
rate = -1;
return rate;
}
//
// This function reads and returns years of deposit
// Pre: none
// Post: Years of deposit have been returned.
//
int ReadYears(void)
{
int years; // number of years in bank
cout << "Number of years to leave in bank: ";
cin >> years;
return years;
}
//
// This function reads and returns the amount desired
// Pre: none
// Post: The money object for the amount desired was returned.
//
money ReadAmount(void)
{
double desired; // amount desired
cout << "Give the amount desired: ";
cin >> desired;
money amount(desired); // object for amount desired
return amount;
}
//
// This function prints an error message for an invalid
// menu choice
// Pre: choice has an invalid value.
// Post: Error message is displayed.
//
void error(int choice)
{
cout << choice << " is an invalid choice. Please run again.\n";
}