Annuity calculator.

The holder of an IRA can deposit money to the account each year. The amount is usually withdrawn when the depositor is age 65. However, it can be withdrawn when the depositor is age 59.5 without incurring a penalty. A bank wishes to show its customers the difference between what they will have in their accounts at age 65 and what they will have in their accounts at age 59.5, if equal annual payments are made to the account. Write a program to do this.
The program should prompt for the annual deposit (which should not exceed $2,000) the annual interest rate, and the customer’s age when the account was opened.
Use a function to obtain each input datum.
The function that obtains the interest rate should allow the user to enter the interest rate as a percent (for example, 4.7) but should return the decimal equivalent of the entered percent (for example 0.047).
The amount in an IRA after n years of equal annual deposits of R dollars at an annual interest rate of r is given by the following formula.
A= R((1 + r) n -1/r)
To calculate the value of an IRA when the customer is 65, first subtract the customer’s age at the time the account was opened from 65. This gives the value of n to use in the formula. The values of R and r are obtained from the user. To calculate the value of an IRA when the customer is 59.5, proceed in the same manner, but subtract the customer’s age from 59.5 to obtain the value of n.
The program should output the value of the IRA at age 65, the value of the IRA at age 59.5 and the difference between these values.
My problem is, my code gives off weird number when calculating the annuity;
-1.#IND

this is my code.
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
#include <iostream>
#include <cmath>
using namespace std;

void annualdeposite(double a);
void interestrate(double b);
double age(double c);
void annuity(); //n=years, r=interest,
double globalintrest;
double years;
double deposit;
double results1;
double results2;
double rate;
double customer65;
double customer59;
double difference;

void main()
{//variables 
	cout<<"Individual Retirement Account"<<endl;
	annualdeposite(deposit);
	interestrate(rate);
	age(years);
	
	customer65=65-years; //subtracting the year of 65
	customer59=59.5-years;
	annuity(); //calling annuity

	system ("Pause");

}
void annualdeposite(double a) //annual
{
cout<<"Enter the annual deposit: $";
cin>>a;
deposit=a;
while (deposit > 2000)
{cout<<"ERROR: This amount is exceeding $2000. Please re-enter: $";
cin>>deposit;
}
}

void interestrate(double b) //interest function 
{
cout<<"Enter the INTEREST RATE: %";
cin>>b;
globalintrest= b*0.01;
cout<<"Decimal Equivalent:"<<globalintrest;
}

double age(double c) //age function. 
{
cout<<"\nEnter the customer's age when the account was opened: ";
cin>>c;
years=c;
return years;
}

void annuity() //annuity function
{   
	results1=deposit*((pow((1+globalintrest),customer65)-1)/globalintrest);
	cout<<"The IRA at age 65: $"<<results1<<endl;
	 results1;

	results2=deposit*((pow((1+globalintrest),customer59)-1)/globalintrest);
	cout<<"The IRA at age 59.9: $"<<results2<<endl;
	 results2;

	 difference=results1-results2;
	 cout<<"The difference between the values: $"<<difference<<endl;
	 difference;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
             // 0↓
    customer65=years-65; 
    result1=annuity(deposit, rate, customer65);
                //     ↑       ↑       ↑
                //     0       0      -65

//...
                     //   0↓           0↓       -65↓
double annuity(double& dollars, double& r, double& n) //annuity function
{

    double results1=dollars*((pow((1+r),n)-1)/r);
    //result1 = 0*(1^-65 - 1) / 0 = 0*(0) / 0 = 0/0 ??? 
Last edited on
Line 10: main must return int.

annualdeposite(), interestrate() and age() should all take references to a double instead of just a double. Right now they all set their parameter but that doesn't change the value that was passed in.

Similarly there's no reason for annuity() to take references since it isn't changing the values.

Line 24. You want to know how many years are between now and when the customer turns 65. That's 65-years, not years-65.

The function that obtains the interest rate should allow the user to enter the interest rate as a percent (for example, 4.7) but should return the decimal equivalent of the entered percent (for example 0.047).
You aren't doing this. Your interestrate() function returns 4.7.

A great way to diagnose problems like this is to add temporary code to print out some of the values. In this case, I added this at the top of annuity():
cout << dollars << " dollars " << r << "% " << n << "years\n";
That showed that both the interest rate r and the value of years were wrong.

Consider structuring things a little differently. Separate output from computation and have functions return their values rather than taking reference parameters. That way main() could look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int
main()
{                               // variables
    double futureValue = 0;

    cout << "Individual Retirement Account" << endl;
    double deposit = annualDeposit();
    double rate = interestRate();
    double years = age();

    futureValue = annuity(deposit, rate, 65-years);     // calling annuity
    cout << "At age 65, annuity is worth $" << futureValue << '\n';

    futureValue = annuity(deposit, rate, 59.5-years);
    cout << "At age 59.5, annuity is worth $" << futureValue << '\n';

    system("Pause");
    return 0;
}

Last edited on
Solved guys; you were a big help!
Topic archived. No new replies allowed.