Calculating future value question

Jun 17, 2011 at 12:10am
I am new to C++ and worked hours on this today. I need to calculate the future value. Can anyone check this out and see what I am doing wrong? For some reason my pow will not light up so I am not being sure it is being recognized. Any help is greatly appreciated. Thanks!

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

int main()
{
//Payment
cout << " Enter payment amount: ";
double payment;
cin >> payment;

//ENTER INTEREST RATE
cout << " Enter an interest rate: ";
double rate;
cin >> rate;

//ENTER PERIODS
cout << "Enter number of payment periods: ";
double periods;
cin >> periods;

//CALCULATE

double futurevalue = payment * pow(1 + rate / periods, - 1 / periods);

//DISPLAY RESULTS
cout << "Your future value is: " << futurevalue << endl;

return 0;
}
Jun 17, 2011 at 1:23am
Here's how I learned simple intrest in high school:

A=P(1+i)n

where:

A = amount at maturity
P = Principle invested
i = intrest rate (as a decimal)
n = compounding periods

so I must ask what this is:

double futurevalue = payment * pow(1 + rate / periods, - 1 / periods);

note: Use code tags please

What you have is:

A = P(1+i/n)-1/n

or something along those lines.

Simply create an extra variable (bad practice, but it's a small program) double i;, define it as i = 1 + (i/100); and in the parameters for pow, use pow (i, periods);

It works, from your code, here's what I got:

 Enter payment amount: 1000
 Enter an interest rate: 4
 Enter number of payment periods: 5
 Your future value is: 889.09


from my edits:

 Enter payment amount: 1000
 Enter an interest rate: 4
 Enter number of payment periods: 5
 Your future value is: 1216.65


The final proper value, if you I do the math:

A = P(1+i)n

A=?
P=1000
i=(4/100)
n=5

A = 1000(1+0.04)5
A = 1000(1.04)5
A = 1000(1.216652902)
A = 1216.652902

Therefore, the amount at maturity will be $1216.65.
Last edited on Jun 17, 2011 at 1:23am
Jun 17, 2011 at 1:35am
Thanks for the help, I just tried to run it but came up with an error and 2 warnings. never had these before :/ any idea?

cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header

cpp(2) : warning C4627: '#include <cmath>': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header

cpp(31) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
Jun 17, 2011 at 1:50am
Are you using Visual Studio? Try adding #include "stdafx.h" to your code (you might have to create an empty header file called "stdafx.h" and place it in the same directory as you "main.cpp"). You might also be able to turn pecompiled headers off (I don't use VS, so I don't know) and avoid this all together.
Jun 17, 2011 at 2:00am
Ok, took care of that problem. Pain in the butt. It compiled and ran fine and I got the same future value you got (1216.65). I entered my books example though and I was slightly off...

books definition of future value

futurevalue = payment * ((1 + interest rate) ^ numberofpaymentperiods) - 1 / numberofpaymentperiods

The books example is

P=1000
i=3.04
N=48
which equals...
A=4130.72

the value I am coming up with is 4209.99. Any ideas?
Jun 17, 2011 at 2:20am
Their math (or my math) is wrong.

*looks through high school math notes, that's right, I still have them nearly two years later*

I have no idea where they got that one. I'm trying to figure out why they would multiply by (-1/numberofpaymentperiods). It simply (to me) makes no sense.

What book are you using?

Jun 17, 2011 at 3:01am
I am using "Introduction to Porgramming with C++" by daniel liang 2nd ed. I did some further testing as well and saw what you said is right. Guess its a book error? Thanks for all your help
Topic archived. No new replies allowed.