Compound Interest in a for loop.

Hello all, I am new at programming, in my first semester at school. I am trying to crate a function that takes the user inputted investment amount, months they are investing, and the rate and return what the investment will be in (number of months).

I did it using pow, but i wanted to also try to create it with a for loop.
the values are 10,000. rate is 3.0, months is 12. when i use the pow, i get 10,304.12, which is the correct answer.

When i run it with my for loop i get 10300.00. I can't figure out what i am doing wrong on this.

Thanks for any help you can provide.

1
2
3
4
5
6
7
8
9
10
11
12
double returnFutureInvestment(int invest, double months, double monthRate)
{
   double futureAmount, x;
   //futureAmount = (invest * pow((1 + monthRate), months));
   for( x=months ; x>0; x--)
   {
       invest += (invest * monthRate);//(principle *rate) + principle

   }
   futureAmount = invest;
   return futureAmount;
}


my entire code is here if its needed.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int getInvest();
double getMonths();
double getAnnualRate();
double monthlyRate(double);
double returnFutureInvestment(int, double, double);

//In the main function there is a do while loop with a nested if statement. The do while
// will  continue until the investment is 0. The nested if statement will allow it to continue
// with the rest of the program as long as the first investment is not 0.
int main()
{
    double invest, months;
    double yearRate, monthRate, futureAmount;
    do
      {
         invest = getInvest();
         if (invest != 0)
         {
            yearRate = getAnnualRate();
            months = getMonths();
            monthRate = monthlyRate(yearRate);
            futureAmount = returnFutureInvestment(invest, months, monthRate);
            cout << "After " << months << " your investment of $" << invest << " will be worth $" 
<< fixed << setprecision(2) << futureAmount << endl << endl;

         }
      }while (invest !=0);


    return 0;
}

//this function asks the user for the investment amount. if it is zero or greater
//than 10000, it will return the investment amount. if it is not, then it will continue
//to ask for a correct investment amount.
int getInvest()
{
   int invest;
   cout << "Enter the amount of the investment: ";
   cin >> invest;
   while (invest < 10000 && invest != 0)
   {
      cout << "The investment needs to be greater than 10,000" << endl;
      cout << "Enter the amount of the investment: ";
      cin >> invest;
   }
   return invest;
}

//This function asks for the number of months invested. it will return values that are negative.
double getMonths()
{
   int months;
   cout << "Enter the amount of months you want to invest: ";
   cin >> months;
   while (months < 0)
   {
      cout << "The number of months can not be a negative" << endl;
      cout << "Enter the amount of months you want to invest: ";
      cin >> months;
   }
   return months;
}

//This function asks for the annual rate. it will not accept a negative.
double getAnnualRate()
{
   double rate;
   cout << "Enter the annual interest rate: ";
   cin >> rate;
   while (rate < 0)
   {
      cout << "The annual interest rate can not be a negative" << endl;
      cout << "Enter the annual interest rate: ";
      cin >> rate;
   }
   return rate;
}

//This calculates the monthly rate from the annual rate. I could have done it in the main
//but i had already started making a function for it, oh well.
double monthlyRate(double rate)
{
   rate = (rate/12)/100;
   return rate;
}

//This function takes the investment, months, and the monthly rate and calculates
//the future investment amount.
double returnFutureInvestment(int invest, double months, double monthRate)
{
   double futureAmount, x;
   futureAmount = (invest * pow((1 + monthRate), months));
  /* for( x=months ; x>0; x--)
   {
       invest += (invest * monthRate);//(principle *rate) + principle

   }
   futureAmount = invest;
   */
   return futureAmount;
}
closed account (48T7M4Gy)
double future = invest;

for(int i = 0; i < months; ++i}
future += future * monthRate;

return future;

Last edited on
Thanks, I had just figured it out.

1
2
3
4
5
6
7
8
9
10
11
12
double returnFutureInvestment(int invest, double months, double monthRate)
{
   double futureAmount, x, interest;
   interest = 1;
   for (x = months; x > 0; x--)
   {
      interest *= (1 + monthRate);
   }
   futureAmount = invest * interest;
  
   return futureAmount;
}
closed account (48T7M4Gy)
Just out of interest why are you counting the months in reverse?
You know, no real reason. I'm my head I said I needed to count down as I do the exponents. I know I could use
for (x =1;x < months; x++)
To do the same thing
You can simplify your function, if you want:
1
2
3
4
5
6
7
8
9
10
double returnFutureInvestment(int invest, double months, double monthRate)
{
   double interest = 1;
   for (; months > 0; months--)
   {
      interest *= (1 + monthRate);
   }
  
   return invest * interest;
}


What's more, you could consider declaring your variables only when you have a value to assign them, to reduce errors.
For example, you declare "invest" as "double"
line 18: double invest, months;
but you pass it into a "int" argument:
line 11: double returnFutureInvestment(int, double, double);
line 28: futureAmount = returnFutureInvestment(invest, months, monthRate);

I see what you mean, i didn't realize that you could return the equation to store it in the variable without the variable name in the function. that makes it a little easier.

Line 18 should actually be int, but when i was trouble shooting the program eariler, i had changed a bunch of things to see if it was a simple error with trying to multiple ints and getting a double. I went back and fixed it.

Thanks
Topic archived. No new replies allowed.