Functions - Variables

UPDATE: Thank you to user long double main for helping previously to this topic on Segmentation Fault!

Hi there,

I'm taking an introductory course in Computer Programming using C++. I am writing a program where the inputs are for payments, rates, and number of weeks to calculate the future value of a deposit.

My question is when you run this program, you input P for the payment itself, R for the interest rate, and N for the number of weeks you are making this payment. However, when the program tells you the Future Value, fv, for it you get 0. Am I doing something wrong with the variables?

The example output I am using is P = 100, R = 9.25, and N = 50.
The Future Value, or fv, should = $5233.57

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  #include <iostream>
#include <cmath>

using namespace std ;

/* PROTOTYPES */

void PrintDirections() ;

double GetPmt() ;

double GetRate() ;

int GetNumWeeks() ;

double Compute_FV(double P, double R, int N) ;

void Report_FV(double P, double R, int N, double fv) ;

/* ********************************** */
/*              MAIN                  */
/* ********************************** */

int main ()
{
  double P, R, fv;
  int N;

  PrintDirections();

  GetPmt();

  GetRate();

  GetNumWeeks();

  Compute_FV(P, R, N);

  Report_FV(P, R, N, fv);

  return 0;
}



/* ********************************** */
/*          PRINT DIRECTIONS          */
/* ********************************** */
void PrintDirections()
{
  cout << endl;
  cout << "This program will calculate the future value of N\n";
  cout << "weekkly payments of P dollars at annual interest\n";
  cout << "rate R." << endl;
  cout << endl;
  cout << "You will be prompted to enter P, R, and N (in that\n";
  cout << "order) and then the future value will be\n";
  cout << "calculated and written to the screen." << endl;
}



/* ********************************* */
/*              GETPMT               */
/* ********************************* */
double GetPmt()
{
  double P;
  cout << endl;
  cout << "You must now enter the amount of the payment P in\n";
  cout << "decimal form (for example: 54.55). Do *not*\n";
  cout << "include a dollar sign ($). Do *not* include any\n";
  cout << "commas." << endl;
  cout << endl;
  cout << "Enter P, the amount of each weekly payment here ===> P = ";
  cin >> P;

  if (P>0) return P;
  else cout << "Cannot recognize value. Try again." << endl;
}



/* ********************************* */
/*              GETRATE              */
/* ********************************* */
double GetRate()
{
  double R;
  cout << endl;
  cout << "Now you must enter the annual interest rate in\n";
  cout << "decimal form (for example: 8.125). Do *not*\n";
  cout << "include a percent sign (%). Do *not* include any\n";
  cout << "commas." << endl;
  cout << endl;
  cout << "Enter R, the annual interest rate here ===> R = ";
  cin >> R;

  if (R>0)
    {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(3);
    return R;
    }
  else
    {
    cout << "Cannot recognize value. Try again." << endl;
    }
}



/* ******************************** */
/*              GETNUMWEEKS         */
/* ******************************** */
int GetNumWeeks()
{
  double N;
  cout << endl;
  cout << "Now you must enter the number of weekly payments\n";
  cout << "as a whole positive integer (for example: 45). Do *not*\n";
  cout << "include a decimal point or any commas." << endl;
  cout << endl;
  cout << "Enter N, the amount of weekly payments here ===> N = ";
  cin >> N;
  if (N>0) return N;
  else cout << "Cannot recognize value. Try again." << endl;
}



/* ******************************** */
/*            COMPUTE_FV            */
/* ******************************** */
double Compute_FV(double P, double R, int N)
{
  double r = (R/5200) + 1;
  double fv = ( P * ((pow(r,N)) - 1) * r ) / (r - 1);

  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);
  return fv;
}


/* ******************************** */
/*           REPORT_FV              */
/* ******************************** */
void Report_FV(double P, double R, int N, double fv)
{
  cout << endl;
  cout << "Here is your answer:" << endl;
  cout << endl;
  cout << "The future value of " << N << " weekly payments of\n";
  cout << "$" << P << " at " << R << "% is $" << fv << "." << endl;
  cout << endl;
}


/* ******************************** */
/*              END                 */
/* ******************************** */
Last edited on
66
67
68
double GetPmt(void)
{
  double P = GetPmt();

What are you trying to do here?
The first thing GetPmt will do is call GetPmt, which will in turn call itself again, and so on and so forth all the way until your program runs out of stack space, at which point you get that segmentation fault error.

You have the same problem with your GetRate and GetNumWeeks functions.
From the looks of it, you can just delete the recursive call and leave the variable uninitialized when you declare it (or set it to 0 if that bothers you), and it'll be fine.
Thanks for your help!! It worked and I appreciate it!
Topic archived. No new replies allowed.