You are using a version without Ads of this website. Please, consider donating:

### Compound Interest program - 1 of 2 posts

I am going to need to put this in two posts, the assignment first, the code second. I am getting errors that I cannot use pow as a function, what am I doing wrong? I am not even sure if I have the calculations right.

Also, I am not allowed to use one letter variables so how to turn for example the letter M into choiceM?

Directions and requirements:
Program #2
Write a program to compare the return from a yearly compounded interest calculation (i.e. figured and added to the balance annually) to interest that is compounded monthly, weekly, or daily.

The program should:
• Use double precision floating point variables, where floating point variables are used.
• Let the user choose from a menu which comparison to make (compounded monthly,
weekly, or daily). A fourth menu choice, exit the program, should be included on the menu.
• Ask the user for a beginning balance, an annual interest rate, and a term in years.
o Error check each of the above items, one at a time, as they are entered. All of
them must be at least 1. Additionally, annual interest cannot be more than 50%,
and the term should not be more than 100 years. For each item, re-prompt the user
until a valid value is entered. Error messages should tell the user what values are valid.
• Display the total return computed with annual compounding versus interest compounded monthly or weekly or daily, formatted as shown in the Sample Run.
o The total compounded returns must be calculated using nested loops, NOT
banking interest formulas. You will compute the interest and add it to the balance each time you loop.
o Calculate annual interest by adding annual interest to the running balance every year.
o Calculate monthly interest by adding monthly interest (use 1/12th of the annual interest rate) to the running balance every month for 12 months per year.
o Calculate weekly interest by adding weekly interest (use 1/52nd of the annual
interest rate) to the running balance every week for 52 weeks per year.
o Calculate daily interest by adding daily interest (use 1/365th of the annual interest rate) to the running balance every day for 365 days per year.
• Loop back to the menu, and continue until the user chooses EXIT from the menu.
o The program should ONLY exit when the user chooses EXIT from the menu.
o Do NOT ask the user if s/he wishes to run the program again – just redisplay the menu each time until EXIT is chosen.

In addition to main, your program must contain a minimum of four additional functions with parameters and/or return values. You should try to use functions to reduce code duplication, when the code is similar in multiple places.

HINT: When you consider how to break this program into functions, think about how
program 1 was divided up:
• At least one function to read input
• At least one function to do calculations
• At least one function to display output
Use of global variables is NOT allowed. The functions should use parameters to pass required data to each function. Remember to pass all input parameters by VALUE, and pass all output parameters by REFERENCE. Display dollar figures to 2 decimal places and the interest rate to 3 decimal places.

Sample Run

When the user presses a key to continue, the program will loop and re-display the menu. The program will exit when the user chooses EXIT from the menu.

This program demonstrates the benefits of compounding interest.

Compare Annual Compounding to:
M - Monthly Compounding
W - Weekly Compounding
D - Daily Compounding
E - Exit Program
Enter choice from Menu above: w

Enter an annual interest rate: 5.5
Enter a term in whole years: 5
------------------------------------------------
RESULTS
------------------------------------------------
Beginning Balance: 5555.55
Return after 5 years at 5.500% interest annually:

Return Ending Balance
Compounded Yearly: \$ 1705.33 \$ 7260.88
Compounded Weekly: \$ 1757.44 \$ 7312.99
------------------------------------------------
Press any key to continue . . .

When the user presses a key to continue, the program will loop and re-display the menu. The program will exit when the user chooses EXIT from the menu.

Here is the code for the above program:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137`` ``````#include #include #include #include #include using namespace std; void getChoice(char& choice); void getNumbers(double& beginBalance, double& rate, int& years); void getCalculations(char choice, double beginBalance, double rate, int years, double calcYInt, double totalYAmt, double calcMInt, double totalMAmt, double calcWInt, double totalWAmt, double calcDInt, double totalDAmt, char M, char W, char D); void getResults(double beginBalance, double rate, int years, double calcYInt, double totalYAmt, double calcMInt, double totalMAmt, double calcWInt, double totalWAmt, double calcDInt, double totalDAmt); int main() { char choice; double beginBalance; double rate; int years; double calcYInt; double totalYAmt; double calcMInt; double totalMAmt; double calcWInt; double totalWAmt; double calcDInt; double totalDAmt; char M; char W; char D; cout << "This program demonstrates the benefits of compounding interest." << endl << endl; do { getChoice(choice); getNumbers(beginBalance, rate, years); getCalculations(choice, beginBalance, rate, years, calcYInt, totalYAmt, calcMInt, totalMAmt, calcWInt, totalWAmt, calcDInt, totalDAmt, M, W, D); getResults(beginBalance, rate, years, calcYInt, totalYAmt, calcMInt, totalMAmt, calcWInt, totalWAmt, calcDInt, totalDAmt); } while (choice != 'E'); cin.get(); return 0; } void getChoice(char& choice) { cout << "Compare Annual Compounding to:" << endl; cout << setw(3) << " " << "M - Monthly Compounding" << endl; cout << setw(3) << " " << "W - Weekly Compounding" << endl; cout << setw(3) << " " << "D - Daily Compounding" << endl; cout << setw(3) << " " << "E - Exit Program" << endl << endl; cout << "Enter Choice from menu above: "; cin >> choice; choice = toupper(choice); cout << endl; switch (choice) { //start switch plan case 'M': cout << "M - Monthly Compounding" << endl << endl; break; case 'W': cout << "W - Weekly Compounding" << endl << endl; break; case 'D': cout << "D - Daily Compounding" << endl << endl; break; case 'E': cout << "E - Exit Program" << endl << endl; exit(0); default: cout << "Please enter a valid menu choice from above: "; cin >> choice; break; } //end switch plan } void getNumbers(double& beginBalance, double& rate, int& years) { cout << "Enter your beginning balance: "; cin >> beginBalance; cout << "Enter an annual interest rate: "; cin >> rate; cout << "Enter the years with a whole number: "; cin >> years; } void getCalculations(char choice, double beginBalance, double rate, int years, double& calcYInt, double& totalYAmt, double& calcMInt, double& totalMAmt, double& calcWInt, double& totalWAmt, double& calcDInt, double& totalDAmt, char M, char W, char D) { double pow; calcYInt = totalYAmt - beginBalance; totalYAmt = beginBalance * pow (1 + rate/100), years; while (choice == M) { calcMInt = totalYAmt - beginBalance; totalMAmt = beginBalance * pow (1 + (rate/100) * 1/12), years; } while (choice == W) { calcWInt = totalYAmt - beginBalance; totalWAmt = beginBalance * pow (1 + (rate/100) * 1/52), years; } while (choice == D) { rate = (rate / 100) * .0027397260; calcDInt = totalYAmt - beginBalance; totalDAmt = beginBalance * pow (1 + (rate/100) * 1/365), years; } } void getResults(double beginBalance, double rate, int years, double calcYInt, double totalYAmt, double calcMInt, double totalMAmt, double calcWInt, double totalWAmt, double calcDInt, double totalDAmt) { cout << "------------------------------------------------" << endl; cout << setw(28) << "RESULTS" << endl; cout << "------------------------------------------------" << endl; cout << "Beginning Balance: " << beginBalance; cout << "Return after" << years << "years at" << rate << "% interest annually" << endl << endl; cout << setw(28) << "Return" << setw(14) << "Ending Balance" << endl; cout << "Compounded Yearly: \$ " << calcYInt << " \$ " << totalYAmt << endl; cout << "Compounded Monthly: \$ " << calcMInt << " \$ " << totalMAmt << endl; cout << "Compounded Weekly: \$ " << calcWInt << " \$ " << totalWAmt << endl; cout << "Compounded Daily: \$ " << calcDInt << " \$ " << totalDAmt << endl; } ``````
The pow function takes 2 arguments - x to the power of y.

Also, consider the use of a bool variable to help control the redisplay the display of the menu.

Edit:

I would have separate functions for the weekly, daily & monthly calculations - and call these from within the switch. This will save the repeated logic in the getCalculations function - and might cut down on the number of args to functions.

HTH - GOOD LUCK !!!!
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: