Errors fix

Hi! I am nearly done with this code that needs to read from a text file. However, I keep getting a few errors and I am not sure how to fix them. The errors read as follows: 'AgeAve': undeclared identifier, 'DisplayName':illegal use of type 'void','THREE<n>::DisplayName' looks like a function definition, but nthere is no parameter list; skipping apparent body, 'THREE<n>::DisplayName':initialization of a function.

The text file contains 5 president's names and their age. I need to compute and return their age average and display the name of presidents whose age are above average. Thank you for any tips!

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

template<int n>
class THREE
{
private:
	struct PRESIDENT
	{
		string name;
		int age;
	};
	person a[n];
public:
	THREE();
	void copydata();
	float ComputeAgeAve();

	float AgeAve=ComputeAgeAve();
	void DisplayName(float AgeAve);
	~THREE(){}
};

int main()
{
	THREE<5> p;
	p.copydata();
	float AgeAve;
	p.DisplayName(AgeAve);
	system("pause");
	return 0;
}

template<int n>
void THREE::copydata(string fname)
{
	fstream in;
	in.open("data.txt", ios::in);
	for(int i =0; i < n; i++)
	{
		in >> a[i].name >> a[i].age;
	}
}

template<int n>
float THREE<n>::ComputeAgeAve()
{
	int total =0;
	AgeAve = 0;
	for(int i = 0; i < n; i++)
	{
		total += a[i].age;
	}
	AgeAve = total / n;
	return AgeAve;
}

template<int n>
void THREE<n>::DisplayName(AgeAve)
{
	for(int i = 0; i < n; i++)
	{
		if(a[i].age > AgeAve)
		{
			cout << "Names above age average: " << a[i].name << " ";
		}
	}
	cout << endl;
}
Line 31: You are let setting AgeAve to a value to start off with.
example)
 
  float AgeAve = 0.0;


Line 62: You are not declaring what type of data AgeAve is in your parameter list.
ok thank you!
In addition to @Parasin-

line 16: What is a "person"? You need to define the type before you can use it.
line 38: Missing the template parameter, and inexplicably the function signature has changed to include a string parameter.
lines 44, 55, 66: As a consequence of line 16, "a" does not exist.

Also, include the <cstdlib> for system if you must use it.

You've got errors on multiples of 11 which is kinda spooky :)
Thank you so much! I have made some changes and this is what I have now but I'm still getting errors

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

template<int n>
class THREE
{
private:
	struct PRESIDENT
	{
		string name;
		int age;
	};
	PRESIDENT a[n];
public:
	THREE();
	void copydata();
	float ComputeAgeAve();
	void DisplayName(float AgeAve);
	~THREE(){}
};

int main()
{
	THREE<5> p;
	p.copydata();
	float AgeAve=0.0;
	p.DisplayName(AgeAve);
	system("pause");
	return 0;
}

template<int n>
void THREE<n>::copydata(string fname)
{
	fstream in;
	in.open("data.txt", ios::in);
	for(int i =0; i < n; i++)
	{
		in >> a[i].name >> a[i].age;
	}
}

template<int n>
float THREE<n>::ComputeAgeAve()
{
	int total =0;
	AgeAve = 0;
	for(int i = 0; i < n; i++)
	{
		total += a[i].age;
	}
	AgeAve = total / 5.;
	return AgeAve;
}

template<int n>
void THREE<n>::DisplayName(float AgeAve)
{
	for(int i = 0; i < n; i++)
	{
		if(a[i].age > AgeAve)
		{
			cout << "Names above age average: " << a[i].name << " ";
		}
	}
	cout << endl;
}
What errors are you getting?

If you don't #include <cstdlib>, you'll probably get an error when you call system() in line 31

In member function ComputeAgeAve(), you haven't declared AgeAve before using it. Perhaps change line 50 to float AgeAve = 0; instead of just AgeAve = 0;

Most importantly, you've left out the implementation for your THREE constructor.
Topic archived. No new replies allowed.