Finding max number in an array of a custom class

I have an assignment that has a file with a bunch of students info listed. I made a student class to handle all of this and another thing i need to do is find which student has the highest GPA. Normally this would be easy and i would set array[0] to max, then compare that to next element and switch to max if needed. Problem is GPA is a float, and idk how i can set studentPTR[0] to GPA. I will post only my main file here, but if you would like to see my header, or class implementation file let me know.

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 "student.h"
#include<fstream>
#include<iomanip>

int main(){

	string lastName;
	string firstName;
	float GPA;
	string emailAddress;
	char middleInitial;
	string concent;
	int year;
	char level;
	int N;
	int count = 0;
	Student *studentPtr;
	float max;

	//Header Information
	cout<< "\nStudent list before:"<<"\n\n";
	cout<< "-----------------------------------------------------------------------------"<<endl;
	cout<<setiosflags(ios::left);
	cout<<setw(14)<<"lastName"<< setw(11)<<"firstName"<< "    "<<"M"<<"   "<<"U"<<"   " << "Concent"<<"   "<<"Y"<<"   "<<"emailAddress"<<"\t\t"<<"GPA"<<endl;
	cout<< "-----------------------------------------------------------------------------"<<endl;

	//Read in the student.txt file
	ifstream myfile("student.txt");

	if(!myfile.is_open())
	{
		cout<<"Can't open file";
	}else{
		//read first element and set to size of student pointer matrix/ dynamically allocate memory
		myfile>>N;
		studentPtr = new Student[N];

	while(!myfile.eof()){

		//read in each element from the file and assign to each respective variable
		myfile >> lastName;
		myfile >> firstName;
		myfile >> middleInitial;
		myfile >> level;
		myfile >> concent;
		myfile >> year;
		myfile >> emailAddress;
		myfile >> GPA;

		//assign the content to elements in the studentPtr array
		studentPtr[count].set_lastName(lastName);
		studentPtr[count].set_firstName(firstName);
		studentPtr[count].set_middleInitial(middleInitial);
		studentPtr[count].set_level(level);
		studentPtr[count].set_concent(concent);
		studentPtr[count].set_year(year);
		studentPtr[count].set_emailAddress(emailAddress);
		studentPtr[count].set_GPA(GPA);

		//Output contents of student
		cout<<setiosflags(ios::left)<<setw(14)<<lastName <<setw(11)<< firstName << "    "
			<<middleInitial<< "   " << level << "   " << concent<< "   " << year<<"   "<< emailAddress<< "\t" << GPA<< endl;
	}
	myfile.close();

	//free up memory
	delete [] studentPtr;
	}

	system("pause");
	return 0;
}


can anyone point me in the right direction?

i would set array[0] to max
¿? ¿did you mean max = array[0];?
I don't see where is your max/max-element function.

1
2
3
4
5
6
7
8
		studentPtr[count].set_lastName(lastName);
		studentPtr[count].set_firstName(firstName);
		studentPtr[count].set_middleInitial(middleInitial);
		studentPtr[count].set_level(level);
		studentPtr[count].set_concent(concent);
		studentPtr[count].set_year(year);
		studentPtr[count].set_emailAddress(emailAddress);
		studentPtr[count].set_GPA(GPA);
Waste of time...
i didn't put it in yet cause everything i tried didn't work so i just removed it. and yes i meant max = array[0]. The part of code you selected is putting the info i read in into the array. what i need to do is find the max of just GPA. it would go after that part. Max should not go in the student class. I have tried things like max = studentPtr[0], but i cannot do it because studentPtr is part of the student class, and max needs to be a float.
1
2
3
float maximum_value = studentPtr[0].GPA;
student *maximum_element = studentPtr;
size_t maximum_index = 0;
You coul use just the pointer, but it may be more clear to use the value too. Just remember to update both.

You don't need setters/getters for everything. It's like making your members public, but wasting a lot of time doing it.
Topic archived. No new replies allowed.