Interest calculator

Ok, trying to make a compound interest calculator that reads the input from a file then computes the total at the end. I'm having problems with my forumla as it is not displaying the right amount of compound interest.

My question is how can I keep track of the input and adding it into the total to compute interest. I thought I was doing it with the year++, but I guess not.

Thanks for taking a look at it. I'm new to programming and hate to be the newbie begging for help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
cout << setprecision(2)<<showpoint<<fixed;
double rate, amount,invest;
int year = 1;
cout << "Enter your interest rate " <<endl;
cin >> rate;
cout << "Enter amount invested "<<endl;
while (cin >> invest)                                                       
    {
    amount = invest * pow(1.0 + rate,year);     
    year++;
    }
double total=amount+invest;
cout<<total<<endl;       
return 0;
}


Edit: Just realised I had the year declared twice. I just made it
int year = 1

My total I'm getting is 251.13
Last edited on
This doesn't really make sense to me.

1
2
3
4
5
while (cin >> invest)                                                       
    {
    amount = invest * pow(1.0 + rate,year);     
    year++;
    }


So each year, you're reading in a new value into invest, and assigning a new value to amount. Can you be more specific as to what you want your program to accomplish, or explain some of the reasoning behind your code?

If you want to calculate interest it should probably look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
...
double interest = 100.00;
double rate = 1.12; // 12% interest rate
int years;

cout << "Enter the amount of years that " << interest << " has been in your savings account, at an annual interest rate of 12%: ";
getline (cin, years, '\n');

for (int i = 0; i < years; i++)
    interest *= rate;

cout << years << " years later, you now have: $" << interest << endl;
...

Last edited on
The program is designed for an assignment that will read from a file

Interest rate
Deposit
Deposit
Deposit
Deposit

etc...

So I'm trying to make my program around that input.
First asking the user for interest rate.
Then asking for the deposit.
So you want to calculate the interest on a specific deposit, then each year, add another deposit and continue calculating the interest?

1
2
total = (total + recentdeposit) * interestrate;
// is this what you want to do? 
Topic archived. No new replies allowed.