c++ beginner

closed account (jL8qM4Gy)
Hello all! I am new to C++ and I was given an assignment to calculate the future value of an ordinary annuity in C++. I am very confused on how to do this. I have done some research on annuities but its very confusing. Can anyone give me some pointers?
and what excactly is your problem?
closed account (jL8qM4Gy)
This is what I have so far :

#include <iostream>
#include <string>
using namespace std;
int main () {
float n, r, i;
// Future value of an ordinary annuity
float OrdinaryAnnuity = ((1 + i)*n -1 /i);
cout << "Please enter the payment amount $";
cin >> r;
cout << "Please enter the interest rate: ";
cin >> i;
cout << "Please enter the number of payments: ";
cin >> n;
cout << "The future value of the annuity is" <<OrdinaryAnnuity<<endl;
}

Idk what I am doing I am looking at my text book but I don't really understand.
Last edited on
you try to calculate OrdinaryAnnuity, before you set i and n with the users input.
that won't work
You do it wrong, by calculating OrdinaryAnnuity before you read the data.
The value of uninitialized variables (n, r, i) is garbage, or zero, depending on the phase of the Moon. So you get a garbage response.

Solution: move the line float OrdinaryAnnuity = ((1 + i)*n -1 /i); after the last cin.

Or you could be more straightforward and just smack the calculation into the final cout.
cout << "The future value of the annuity is" << ((1 + i)*n -1 /i) <<endl;

Moral: use variables after you read them.
Also: you don't use your variable r in calculating the formula.
Last edited on
kam,
you are just one step away, listen to Darkmasters comment,
calculating your formula before you asked for inputs is not going to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
    float n, r, i;
    float OrdinaryAnnuity;

// Future value of an ordinary annuity

    cout << "Please enter the payment amount $";
    cin >> r;
    cout << "Please enter the interest rate: ";
    cin >> i;
    cout << "Please enter the number of payments: ";
    cin >> n;

    OrdinaryAnnuity = ((1 + i)*n -1 /i);
    cout << "The future value of the annuity is" <<OrdinaryAnnuity<<endl;
}

simply move down your equation to after you enter the inputs.

Oops! Sorry while we all might be posting at the same time, Catfish2 already commented on the calculation happening before the input stage. I didnt mean to butt in.
Last edited on
closed account (jL8qM4Gy)
Thank you all. for some reason it still isn't calculating right.

I know the calculation for it mathematically is the payment [((1 + interest)*number of periods - 1) / interest].. I can't figure out how to write it in C++ i guess.
You know what kam,
after looking at your problem a little more, i noticed that you didnt use the variable r in the equation.

i think the equation is OrdinaryAnnuity = r *[((1 + i)^n - 1) / i]


you should be able to figure out from your book what else you might need to do the equation.
Last edited on
@ kam5985: you misplace a pair of parentheses!
1
2
3
OrdinaryAnnuity = ((1 + i)*n -1 /i);
// should be
OrdinaryAnnuity = ((1 + i)*n -1) /i;

closed account (jL8qM4Gy)
I know the calculation for it mathematically is the

payment [((1 + interest)*number of periods - 1) / interest]
r=payment
i=interest
n=number of periods

.. I can't figure out how to write it in C++ i guess.
closed account (jL8qM4Gy)
actually its not multiplied by number of periods
its raised to the number of periods
--how do I do this? i know it should be "pow" n... but it won't let me...ahhh
Last edited on
you need to #include <math.h>
to your program,
then you can do things like
pow((1 + i),n )


so your equation should be like this:

OrdinaryAnnuity = r*( pow((1 + i),n) -1 /i);
Last edited on
And if your compiler can't find math.h, use cmath.
#include <cmath>
closed account (jL8qM4Gy)
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main ()
{
double r, i, n, p;
double OrdinaryAnnuity;
// Future value of an ordinary annuity
cout << "Please enter the payment amount per period $";
cin >> r;
cout << "Please enter number of periods per year: ";
cin >> p;
cout << "Please enter the number of years: ";
cin >> n;
cout << "Please enter the state annual interest rate (in decimal form): ";
cin >> i;
OrdinaryAnnuity = r*((pow (1+(i / p), (n * p))-1 ) /(i / p));
cout << "The future value of the annuity is " <<OrdinaryAnnuity<<endl;
}[
This is what I came up with now. The problem is it's still not calculating it right.

My question is how should I input the rate to show as a percentage?


say the interest rate is 7%...I would have to enter .07 in order for the program to calculate right. I would like to make it so all you have to do is enter in the 7.

Last edited on
on line right after cin >> i;
you can put

i = i/100;


At this point you should have all the tools you need to complete your assignment,
now its all a matter of getting your formula correct,
this should be a reachable goal for you if you just go by trial and error.
work out on paper what you have for the formula, and play around with the variables until you know what the output should be like. thats most of the job of programming right there, code->compile->test->retry.

you've got the formula and i'm glad you figured out how to incorporate the POW function. you are almost done
Last edited on
kam,

Q)-say the interest rate is 7%...I would have to enter .07 in order for the program to calculate right. I would like to make it so all you have to do is enter in the 7.
A)- Enter 7 and divide by 100.

for further help , try this link:
http://www.jiskha.com/search/index.cgi?query=%245%2C000+compounded+semiannually+at+6%+for+5+years

btw, it'll be a lot easier if you make a plan first.
plan what will go into MAIN()
and plan what functions you'll need
plan what inputs and outputs you'll need
plan how you want to handle exceptions and errors.
and plan how you want the user to respond - is this a single shot, once through program or multiple time use?
and lastly, how you plan to test.

oh, and plan your variables in advance so they don't look like i,j,n,m, x,y,
but rather like
INT i_Rate, i_Num_years
Float f_Calc_Val
DOUBLE d_Annual_Inst

Once you have a good design, then you can add your formulas
THEN you can begin to wrap that with C++ code.


closed account (jL8qM4Gy)
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main () 
{
	float pmt, rate, years, per;
	float OrdinaryAnnuity;
	string go_on = "yes";
	// Future value of an ordinary annuity
	cout <<"Calculate Future Value of Ordinary Annuity" << endl;
	cout << endl;
	while (go_on == "yes") {
		cout << "Please enter the payment amount per period $";
		cin >> pmt;
		cout << "Please enter number of periods per year: ";
		cin >> per;
		cout << "Please enter the number of years: ";
		cin >> years;
		cout << "Please enter the state annual interest rate: ";
		cin >> rate;
		rate= (rate / 100);
		OrdinaryAnnuity = pmt*((pow (1+(rate / per), (years * per))-1 ) /(rate / per));
		cout << "The future value of the annuity is " <<OrdinaryAnnuity<< endl;
		cout << "Continue (\"yes\" or \"no\")? ";
		cin >> go_on;
		cout << endl;
	}
}


This is my newest code. I think I am getting really close. The only problem I am getting is it's not producing the exact amount still.
The only problem I am getting is it's not producing the exact amount still.

Then it can only be that you made mistakes in the formula again, because your code doesn't have language mistakes that I can see.

Or if the problem has to do with accuracy, use double instead of float.
Be advised, because of the way both double and float store real numbers, you can never get 100% accuracy.
http://en.wikipedia.org/wiki/Floating-point#Accuracy_problems
kam,
you changed the formula and added another parameter to the program (years and per), i just noticed it, and this is why you're getting confused.

I looked up the formula for FutureValue Ordinary Annuity and there are really 3 parameters needed.

C = Cash flow per period
i = interest rate
n = number of payments

FVOrdinaryAnnuity = C * [ ( pow(1+i,n) -1) / i ]
remember this? this is what you were putting up for us to look at, and its pretty close to what the other textbooks are saying, just refined it a little so it works in C/C++ language.

when you introduced the 'years' variable and the 'per' variable you really didnt change anything. relative to your original equation, years*per = number of payments.
and with that proof, here is my version of your program which only uses 'number of payments'

the equation was from: http://www.investopedia.com/articles/03/101503.asp#axzz2CrmpdaTV

i'm hoping that this at least solves your original problem:
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
#include <iostream>
#include<cmath>
using namespace std;

int main()
{
    double n, C, i;
    double FVOrdinaryAnnuity;
    double PVOrdinaryAnnuity;

// Future value of an ordinary annuity

    cout << "Please enter the payment amount $";
    cin >> C;  //base amount
    cout << "Please enter the interest rate: ";
    cin >> i;
    cout << "Please enter the number of payments: ";
    cin >> n;
    i =(i / 100);  //convert to percent
    
    
    FVOrdinaryAnnuity = C * ( (       pow(  (1 + i),  n)   -1   )   /i     );
    PVOrdinaryAnnuity = C * ( ( 1 - pow(  (1 + i), -n)          )   /i    );

    cout << "The future value of the annuity is" << FVOrdinaryAnnuity<<endl;
    cout << "The present value of the annuity is" << PVOrdinaryAnnuity<<endl;
    return 0;
}


I threw in Present Value ordinary Annuity just in case you wanted to see anther calculation translated into your program. give this a try and see if this gets you closer to the right anwser.
I'll let you figure out how to do the equation with years*per variables.

Last edited on
This is a great example of the pitfalls of trying to write code on the fly.

OP, when you don't fully understand the subject for which you are programming, in this case finance, then first write pseudocode to organize your thoughts about the problem.

I KNOW that if you had pseudocoded your problem you would not have tried to compute the future value before the user inputted the needed parameters, as Darkmaster pointed out.

I also believe that if you had also created test data by hand and tested your program with it, you would have discovered your misplaced parenthesis error right away.

If you are still having difficulty after the posts to help you, then go back to square one and pseudocode your problem. Then you can get your program correct.

I am teaching myself C++ and am far from an expert. My very first intro to computer science course was as a freshman in 1980 and we used BASIC. The professor taught us pseudocode. As I try to write programs for more complex problems, I really see how ESSENTIAL pseudocoding is. I am surprised it is not emphasized more.

I decided to give an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
Problem: compute the future value of an ordinary annuity.

The user inputs the needed parameters.
The needed parameters are: 
  1) the number of compounding periods
  2) the length of the compounding period
  3) the interest rate per compounding period
  4) the annuity amount
The future value is computed using the formula 
  Future value = annuity amount times [(the interest rate + 1)^number of    
  periods - 1] divided by the interest rate.
Display the future value.
The user indicates if another computation is to be made.


If you want bonus points, add filters to make sure the interest rate is not negative, the compounding period is at least daily, the number of periods is not negative, the annuity amount is not negative.

N.B. There is a difference between an ordinary annuity, (payment made at end of the period like a salary), and an annuity due, (payment made end beginning of the period like rent).
Last edited on
Topic archived. No new replies allowed.