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.
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);
}
}
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?
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/