Return to the BobbyGriggs.com C++ Nook


Chapter 6 - Return on Savings
© 1998 by Bobby Griggs.



// Program name:  Savings return

// Purpose:  To compute the amount of money in an account with 

// compound interest over a period of years.  The user will input

// the number of periods per year that interest is to be compounded.

// The user will also input the number of years that the funds will

// be invested.



// Win32 console application



#include < iostream.h >

#include < iomanip.h >

#include < math.h >

#include "myprocs.h"



// variables in functions

// r - interest rate

// pd - number of compound periods per year

// y - number of years

// pr - principal

// a - final amount

// c - menu choice



int Menu();

double Principal_amount();

double Final_amount();

void Rate_and_periods(double &r,int &pd,int &y);

void Calculate(int c,double &pr,double &a,double r,int pd,int y);

void Display(int c,double pr,double a);



void main()

{

	int choice,periods,years;

	double rate,principal,amount;

	bool doagain = true;



	do

	{

		choice = Menu();

		switch (choice)

		{

		case 1 : 

			principal = Principal_amount();

			break;

		case 2 : amount = Final_amount();

		}

		Rate_and_periods(rate,periods,years);

		Calculate(choice,principal,amount,rate,periods,years);

		Display(choice,principal,amount);

		doagain = Again();

	}

	while (doagain);

}



int Menu()

{

	int c = 0;



	while ((c < 1) || (c > 2))

	{

		cout << "Savings Program" << endl << endl;

		cout << "1 ... Compute amount returned on deposit" << endl;

		cout << "2 ... Compute amount to deposit for desired return";

		cout << endl << endl;

		cout << "Enter your choice ... ";

		cin >> c;

		cout << endl;

	}

	return c;

}



double Principal_amount()

{

	double pr;



	cout << "Enter amount of principal ... ";

	cin >> pr;

	cout << endl;

	return pr;

}



double Final_amount()

{

	double a;



	cout << "Enter final amount desired ... ";

	cin >> a;

	cout << endl;

	return a;

}



void Rate_and_periods(double &r,int &pd,int &y)

{

	cout << "Enter interest rate ... ";

	cin >> r;

	cout << endl;

	cout << "Enter compounded periods per year ... ";

	cin >> pd;

	cout << endl;

	cout << "Enter number of years for investment ... ";

	cin >> y;

	cout << endl;

	return;

}



void Calculate(int c,double &pr,double &a,double r,int pd,int y)

{

	switch(c)

	{

	case 1 :

		a = pr * pow(1 + (r / pd),pd * y);

		break;

	case 2 :

		pr = a / pow(1 + (r / pd),pd * y);

	}

	return;

}



void Display(int c,double pr,double a)

{

	cout << setiosflags(ios::fixed) << setprecision(2);



	switch(c)

	{

	case 1 :

		cout << "The final amount with interest is $" << a << "!";

		break;

	case 2 :

		cout << "You will need to deposit $" << pr << "!";

	}

	cout << endl << endl;

	return;

}