How to bubble sort data from a file?

I'm trying to write a program that will print the data of 10 records from a file and then, sort them descending to ascending by year.

Here are the records:
Name Year Tuition
David 2011 1582.38
Sylvester 2012 728.82
Ben 1992 0
Brandon 1995 500.25
Riley 1997 845.19
Mark 2009 700.05
Roberts 2002 450.87
Butler 2005 920.78
Steve 2000 1000.00
Joe 1994 1200.15

Here is my code so far:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
string line;
ifstream myfile ("bubblesort.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;

}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}

How do I go about arranging it?
Have a class:
1
2
3
4
5
6
class Student
{
    string name;
    unsigned int year;
    unsigned float tuition;
}


As you read each line, allocate new object and populate the object from each line. Once you have all objects allocated, you can then sort based on year field in each object.

And de-allocate objects before exiting the program.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.