Struggling with the last two functions

I'm struggling with the last two functions of this program. I'm unsure of what to do for these last two functions.

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



struct personType
{
	string student;
	int age;
	double gpa;
	char sex;
 };
 
void loadStudents(personType students[], int &numStudents);
void averageAge(personType students[], int );
void over20(personType students[], int );
void highestGpa(personType students[], int );

const int MAXSTUDENTS = 20;
int main()
{
	personType students[MAXSTUDENTS];
	int numStudents;
	
	//read student information into the array
	loadStudents(students, numStudents);
	
	//Print the average age
	averageAge(students, numStudents);
	
	//Print the student with the highest gpa
	highestGpa(students, numStudents);
	
	//Print the number of students older than 20
	over20(students, numStudents);
	
	//print the names of the female students
	//femaleStudents(students, numStudents);
	
	//find a student
	//findStudent(students, numStudents);
	
	system("pause");
	return 0;
}

void loadStudents(personType students[], int &numStudents)
{
	 cout <<"Entering loadcourses"<<endl;
     ifstream infile;
     infile.open("Students.dat");
     cout <<"Entering while loop"<<endl;
	 numStudents = 0;
     while(!infile.eof() )
     
     infile >> students[numStudents].student >> students[numStudents].age >> students[numStudents].gpa >> students[numStudents].sex;
         numStudents++;
}

void averageAge(personType students[], int numStudents)
{
	int array_sum;
	double average;
	cout << "Printing the average age..." << endl;
	array_sum = 0;
	for (int i = 0; i < numStudents; i++)
	array_sum = array_sum + students[i].age;
	average = (double)array_sum / numStudents;
	cout << array_sum << endl;
	cout << average << endl;
}

void highestGpa(personType students[], int numStudents)
{
	cout << "Printing the highest GPA..." << endl;
	int array_max;
	array_max = students[0].gpa;
	for (int i = 1; i < numStudents; i++)
	if (students[i].gpa > array_max)
	{
		array_max = students[i].gpa;
	}
	cout << array_max << endl;
}

void over20(personType students[], int numStudents)
{
	cout << "Printing ages over 20" << endl;
	int ctr;
	ctr = 0;
	for (int i = 0; i < numStudents; i++)
	if (students[i].age > 20)
	{
		ctr++;
	}
	cout << ctr << endl;
}

void femaleStudents(personType students[], int numStudents)
{
	
}

void findStudent(personType students[], int numStudents)
{
	
}


The functions I'm referring to are void femaleStudents and void findStudent
Last edited on
How does "age is over 20" differ from "gender is female"?
Age and sex both attributes of a personType.

Find whom?
If you are told to find a specific student, then should you know what to look for?
For void findStudent the user types the name of a student, and the program will print out their information.
It's a DAT file called
Students.dat
. Consisting of these data:

Church 23 3.1 M
Tucker 19 2.9 M
Caboose 17 2.1 M
Washington 24 3.9 M
Sarge 52 2.7 M
Simmons 23 3.7 F
Grif 20 3.0 F
Donut 22 2.4 F
Lopez 30 2.4 F
O'Malley 31 1.9 F
Your attempt at reading the file is broken in many ways.

1. eof doesn't mean what you think it means.
2. You don't pass the max size of the array to the function.
3. You don't have braces around the necessary parts of your loop.

1
2
3
4
5
6
7
8
9
10
11
12
void loadStudents(personType students[], int &numStudents, int maxStudents)
{
    cout <<"Entering loadcourses"<<endl;
    ifstream infile("Students.dat");
    cout <<"Entering while loop"<<endl;
    numStudents = 0;
    while ( numStudents < maxStudents &&
            infile >> students[numStudents].student >> students[numStudents].age 
                   >> students[numStudents].gpa >> students[numStudents].sex ) {
        numStudents++;
    }
}

Topic archived. No new replies allowed.