Error in Main Function

Apr 20, 2017 at 1:28pm
For my code, I have an error in line 33 and i'm not sure why, can anyone point out what's wrong?

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
#include <iostream>  
#include <cmath> 
#include <math.h>
#include <fstream>
#include <iomanip>

using namespace std;  

int main(void)  
{
	const char filepath[] = "C:\\CoutputFiles\\File6.txt";
	const char ErrorMessage[] = "Can't open file ";
	fstream OutStream(filepath, ios::out);
	if (OutStream.fail())
	{
		cerr << ErrorMessage << filepath;
		exit(-1);
	}

	int i;
	long double a, b, d, n, I = 0, J = 0, A, K = 0, E = 0;
	cout << " x^4 " << endl;
	cout << "Enter Lower limit " << "\n";
	cin >> a;
	cout << "Enter Upper Limit " << "\n";
	cin >> b;
	cout << "Enter the number of intervals : " << "\n";
	cin >> n;
	OutStream << n << ", ";
	d = (b - a) / n;

	double f(double(x))
	{
		return (x*x*x*x);
	}

	double g(double(x))
	{
		return (4 * x*x*x);
	}

	double h(double(x))
	{
		return (12 * x*x);
	}

	for (i = 1; i<n; i++)
	{
		if ((i % 2) != 0)
		{
			I = I + f(a + (i*d));
		}
	}

	for (i = 2; i<n - 1; i++)
	{
		if ((i % 2) == 0)
		{
			J = J + f(a + (i*d));
		}
	}

	A = (d / 3)*(f(a) + (4 * I) + (2 * J) + f(b));

	cout << "The Value of integral under the entered limits is : " << "\n";
	cout << A << "\n";

	E = ((b - a)*d*d*d*d * 6 / 180);

	cout << "The Total Error is : " << "\n";
	cout << E << "\n";
	OutStream << E << ", ";
	system("pause");
	return 0;
}
Apr 20, 2017 at 1:56pm
You're defining a function, f, inside the definition of another function, main.
Not allowed. (Actually you can if you're using lambdas but that's different.)
Put them above (and outside of) main, or below (and outside of it). In the latter case, you'll need some prototypes at the top to keep your compiler happy. In a simple example like this, I'd just define them at the top of the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double f(double(x))
{
	return (x*x*x*x);
}

double g(double(x))
{
	return (4 * x*x*x);
}

double h(double(x))
{
	return (12 * x*x);
}

int main /* etc. */


Apr 20, 2017 at 2:04pm
Thank you!
Topic archived. No new replies allowed.