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 your beginning balance: 5555.55
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:
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <cmath>

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.