Reading from file and placing its contents into an array of objects

Hello, I've been trying to read from a file and place its contents to a array of objects in a class and trying to sort them with QuickSort, MergeSort, etc. as an assignment. But my problem is how to actually read from the file and placing them into a dynamically sized array based on the txt file and into the main.

I have this for my class but I don't know where to begin.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 class Student
{
private:
  string firstName;
  string lastName;
  double GPA;
public:
Student() { firstName = ""; lastName = ""; GPA = 0.0; }
// Default Constructor
Student(string f, string l, double g) {firstName = f; lastName = l;}

void setFName(string f) {firstName = f;}
string getFName() { return firstName;}

void setLName(string l) {lastName = l;}
string getLName() {return lastName;}

void setGPA(double g) {GPA = g;}
double getGPA() { return GPA;}



};


TXT FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Andrew Koch 2.0
Landyn Adkins 2.6
Jakobe Carey 2.7
Troy Murray 2.9
Cullen Dyer 3.0
Zaire Murphy 2.2
Zaniyah Martinez 3.7
Nolan Lynch 0.6
Josh Harris 1.3
Alejandra Stevens 2.1
Reginald Graves 1.9
Raelynn Castro 3.8
Oscar Norman 1.1
Emerson Randolph 4.0
Mitchell Roman 3.0
Alessandro Huff 0.9
Clarissa Rocha 3.1
Pedro Acevedo 1.1
Katelyn Gilmore 1.9
Julianna Carroll 4.0


I also want to sort this array by using sort algorithms by their GPA based on the choice of the user. I have created a switch case menu, but again, need help how to pass this array of objects into the functions.
EX:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void heapSort(int arr[], int size)
{
    for (int index = size / 2 - 1; index >= 0; index--)
    {
        maxHeapify(arr, size, index); // already written but not included in this post
        
    }    
    
    for ( int index = size - 1; index >= 0; index--)
    {
        swap(&arr[0], &arr[index]);
        
        maxHeapify(arr, size, index);
        
        
    }    
    
}   
Last edited on
you need a file.
ifstream ifs;
and you need to open it.
ifs.open("path/filename");
and you should read from it
ifs >> data or use getline and break it apart. it acts pretty much like cin for text
probably into a vector, using push-back each time you get a line.

that means your class can't do the file stuff as you have it. you need a vector OF that class where that happens, OR you class needs to be some sort of self handling data structure itself like a linked list (do not do this, just saying that your class holds 1 student, so you need some way to hold ALL of them).

you could make a class of the vector and filename and all but its probably overkill. do you need that second tier of objects for any reason?

vector<Student> studs;
and in some loop where the file stuff happens..
ifs >> tmpstr;
tmpstud.setFName(tmpstr);

studs.push_back(tmpstud);

any of this ring a bell on your class notes or book or anything?
Last edited on
Not much, since my teacher at the time didn't do a good job explaining dynamic arrays or give good programming examples in class. Addressing to your suggestion, I do have the file and I have already opened the file in the program, just need how to pass the contents to the array of objects. If anything helps, here's a copy of the assignment that should explain my situation better: https://www.docdroid.net/PCb1ZBY/
Topic archived. No new replies allowed.