Functions

I am working on a homework assignment and could use some guidance. I cannot get the program to go into the getFutureValue function.

This is what I'm asked to do:
Write a program that prompts the user to enter the accounts present value, monthly interest rate, and number of months that the money will be left in the account. The program should pass these values to a function named futureValue that returns the future value of the account after the specified number of months. The program should display the accounts future value.

Thanks in advance for any help.


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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

      // Global Constants 
      double presentValue, futureValue, rate, numberMonths;
      
      // Function prototypes
      double getFutureValue(double);
      
int main()
{
      
       cout << "Enter the current amount of money in your savings account: ";
       cin >> presentValue;
       
       // Input validation
       while (presentValue < 1)
          {
             //Error message
             cout << "\nThe amount of money must be greater than 0."
                  << " \nPlease re-enter the current amount of money in"
                  << " your account: ";
             cin >> presentValue;
          }
          
       cout << "\nEnter the monthly interest as a decimal for your account: ";
       cin >> rate;
       
        // Input validation
       while (rate < 0)
          {
             //Error message
             cout << "\nThe interest rate must be greater than 0."
             << " \nPlease re-enter the monthly interest rate: ";
             cin >> rate;
          }
       
       cout << "\nEnter the number of months that the money will be left "
            << "in the account: ";
       cin >> numberMonths;
       
         // Input validation
       while (numberMonths < -1)
          {
             //Error message
             cout << "\nThe number of months must be greater than -1."
                  << " \nPlease re-enter the number of months the money "
                  << "will be left in the account: ";
             cin >> numberMonths;
          }

   system("pause");         
    return 0;

}

double getFutureValue(double futureValue)

{

       futureValue = (presentValue * pow((1 + rate),numberMonths));
       
       cout << fixed << showpoint << setprecision(2);
       cout << "The future value of the account will"
            << " be " << futureValue << endl;

   system("pause");         
    return 0;
}
For starters, getFutureValue should not be doing any I/O. You should not be sending anything to cout in that function. The instruction state pretty clearly that your function should just do the computation and return the results. Your function does not do this (you always return 0)



That said... to get your program to go into your function, you just call it:

1
2
3
4
5
6
int main()
{
    ...

    // This is how you call the function:
    double output = getFutureValue( input );


You have to give the function some 'input', which will fill in the 'futureValue' parameter in the function. Then whatever getFutureValue returns gets assigned to the 'output' variable.
I made some changes but I'm still having errors. I tried the output = you talked about above with my own variable names but that didn't work either. My door doesn't show any examples like that. Here is my modified 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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

      // Global Constants 
      double presentValue, futureValue, rate, numberMonths;
      
      // Function protocols
      double getFutureValue(double);
      

int main()
{ /*begin main*/
      
       cout << "Enter the current amount of money in your savings account: ";
       cin >> presentValue;
       
       // Input validation
       while (presentValue < 1)
          {
             //Error message
             cout << "\nThe amount of money must be greater than 0."
                  << " \nPlease re-enter the current amount of money in"
                  << " your account: ";
             cin >> presentValue;
          }
          
       cout << "\nEnter the monthly interest as a decimal for your account: ";
       cin >> rate;
       
        // Input validation
       while (rate < 0)
          {
             //Error message
             cout << "\nThe interest rate must be greater than 0."
             << " \nPlease re-enter the monthly interest rate: ";
             cin >> rate;
          }
       
       cout << "\nEnter the number of months that the money will be left "
            << "in the account: ";
       cin >> numberMonths;
       
         // Input validation
       while (numberMonths < -1)
          {
             //Error message
             cout << "\nThe number of months must be greater than -1."
                  << " \nPlease re-enter the number of months the money "
                  << "will be left in the account: ";
             cin >> numberMonths;
             
          }
          
       cout << fixed << showpoint << setprecision(2);
       cout << "The future value of the account will"
            << " be " << futureValue << endl;

   system("pause");         
	return 0;

} /*end main*/

double getFutureValue(presentValue, rate, numberMonths)

{

        futureValue = (presentValue * pow((1 + rate),numberMonths));
     
	return 0;
}
Take time to block out your program like this:
Then replace the comments with code

Write a program that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    //prompts the user to enter:
    // the accounts present value
    // monthly interest rate
    // number of months that the money will be left in the account.
    // The program should pass these values to a function named futureValue
    // The program should display the accounts future value
}

//a function named futureValue

double futureValue (double present, double interest, double months) 
{
  double futureValue = 0.0;
  // calculate here and 
  // returns the future value of the account after the specified number of months.
}
Last edited on
Topic archived. No new replies allowed.