Need help with calling/defining functions

This program consists of three functions which will ultimately calculate an area. The first function requests the user to enter a few numbers and then calls the second function which calculates the final area using the entered values from the main function. The second one will then call the last function to retrieve the value of pi to calculate the final area. I'm so confused on where to place the function calls..please 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
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
#include <iostream>
#include <iomanip>

using namespace std;

//declare Simpson function
void Simpson(double);

int main ()
{
    int a,b,n,deltax;
    double deltaxdiv;
    
    //prompt user for a,b, and n values
    
    cout<<"Please enter a value for a: ";
    cin>>a;
    
    cout<<"Please enter a value for b: ";
    cin>>b;
    
    cout<<"Please enter a positive value for n: ";
    cin>>n;
    
    if (n<0)
    {
        cout<<"The value you entered is not positive, please enter again: ";
        cin>>n;
    }
    //display the entered numbers
    cout<<"The numbers you've entered are: "<<a<<" "<<b<<" "<<n<<" ";
    
    //calculate for simpsons delta x
    deltax=b-(a/n);
    
    deltaxdiv=deltax/3;
    
    cout<<"\ndelta x is: "<<deltaxdiv<<endl;
    
    //call Simpson function
    Simpson(deltaxdiv);
    
    cin.get();
    return 0;
}
//following is the Simpson function

{
    double Simpson(int area);
    int x,y,z,sum1,sum2,sum3,area,total;
    
    sum1=0;
    sum2=0;
    sum3=0;
    total=0;
    
    
    for (x=1; x<n; x=x+2)
    {
        sum1=sum1+(1/1*(x/2)^5)*4;
    }
    
    for (y=1; y<n; y=y+2)
    {
        sum2=sum2+(1/1*(y/2)^5)*2;
    }
    
    for (z=1; z<2; z=z+2)
    {
        sum3=sum3+(1/1*(z/2)^5);
    }
    
    total=sum1+sum2+sum3;
    
    area=pi*(deltaxdiv*total);
    
    cout<<"the final area is: "<<area<<endl;
    
    //call Pi function
    double findPi(pi);
    
    return;
}
                  
    //following is the Pi function
    {
        double findPi(double pi);
        
        double indval1,indval2,sumofiterations,pi;
        indval1=0;
        indval2=0;
        
        
        for (int i = 1; i <250; i= i + 4)
        {
            indval1 =indval1+ (1.0 / i);
        }
        
        for (int j = 3; j <250; j= j + 4)
        {
            indval2 =indval2+ (1.0/j);
        }
        
        //calculate each set of iterations
        sumofiterations=indval1-indval2;
        
        //multiply sum by 4 to receive pi
        pi=4*sumofiterations;
        
        //print out answer
        cout<<" "<<fixed<<setprecision(6)<<pi<<endl;
        
        return;
    }
Here's how you'd do it to calculate the area of a circle (also uses pi), you can edit this accordingly.

The main thing to bear in mind is that the complier needs to have seen at least the declaration of any function that is being called from within another function and so if you switch your order of function declarations around you'd have to forward declare some of them as required:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

double return_pi()
{
    return 3.14;
}
double calc_area (double rad)
{
    return_pi(); //not strictly reqd, could go to next line directly
    return return_pi()*rad*rad;
}
double get_rad ()
{
    std::cout<<"Enter radius: \n";
    double rad;
    std::cin >> rad;
    return calc_area(rad);
}
int main()
{
    double rad = get_rad();
    std::cout << rad << '\n';
}



This:

1
2
3
4
5
6
//following is the Simpson function

{
    double Simpson(int area);
    // ...
}


isn't how you define a function. I strongly suggest you go back to your textbook, and look at what it actually says for how to define a function. Or, alternatively, look at the tutorial on this site:

http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.