Help with future value program and interest

I have to write a program for my computer science class to determine the future value of a given amount of money. I'm close I know it, but I'm currently stuck. My teacher also provided sample input and sample output that i should have, along with the formula that should be used. I also have to use the libraries provided (they're in the code already) Can you guys help? My current issues are getting the output in the right format and the program closes immediately after getting the result.

Sample input 1000 2 5 (1000 deposited for 2 years at an interest of 5%)
Sample output $1000 deposited for 2 years at 5.00% interest = 1102.50

My code so far:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
cout <<"Enter initial deposit:";
double deposit;
cin>>deposit;

cout<<"Enter number of years:";
double years;
cin>>years;

cout<<"Enter an interest rate:";
double rate;
cin>>rate;

double futurevalue=deposit*pow(1+rate/years,-1/years);

cout<<"Your future value is:" <<futurevalue <<endl;



return 0;
}


Thank you for any help
For the console closing:
http://www.cplusplus.com/forum/beginner/1988/

And isn't this the formula?
http://qrc.depaul.edu/StudyGuide2009/Notes/Savings%20Accounts/Compound%20Interest.htm
Seems like you have it wrong when you do:
double futurevalue=deposit*pow(1+rate/years,-1/years);
closed account (j86M4iN6)
The first thing I notice is that the interest formula you have is not right. It looks like you have a mixture of both compound and continuous interest going on which would produce an incorrect amount for either.

P(1+i)^n

is the formula for continuously compounding interest, which is what it would take to get your desired amount. P= principal i=interest rate and n = number of years.

1
2
int pause;
cin>>pause;


Would be enough to keep the window open for now, but there are tons of solutions stickied at the top of this forum. Hope this helps
Last edited on
Somehow forgot to include but the formula i was provided was A=X(1.0+R/100)^N
Since my formula for the code above is wrong, can someone show me how it should look?

A=Amount of money
X=initial deposit
R=rate
N=years

When I mentioned help with output, I mainly meant on getting the format right. basically with the input showed above, i want the output as shown with the $ signs and all that.
Last edited on
closed account (j86M4iN6)
1
2
double futurevalue=deposit*pow(1+(rate/100),years);


Something like that would do it. This also allows the user to enter the percentage rate as a whole number rather than a decimal. As far as the output I think this may help:

1
2
3
4
5
cout<<"$"<<deposit<<" deposited for "<<years<<" years at "<<rate<<"% interest = $";
	cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
	<<futurevalue<<endl;


The setprecision stuff is new to me so I'm not sure if it is the most effective way to do it, but it works. I just found it by typing "force two decimal places c++". Google is your friend.
Topic archived. No new replies allowed.