Output in alphabetical order

Hello, I am trying to print out these names from a data file in alphabetical order with the scores attached to the names. Can someone be of some assistance and write some code please?

struct person
string firstName, lastName;
int score;

int main()

ifstream info1, info2;

cerr << "Error: info1 could not be opened" << endl;

info2.open("info2.txt"); // opens the file
if(!data2) // file couldn't be opened
cerr << "Error: info2 could not be opened" << endl;

person rec1;
data1 >> rec1.firstName >> rec1.lastName >> rec1.score;
while (!data1.eof())
cout << rec1.lastName << ", " << rec1.firstName << " " <<rec1.score << endl;
data1 >> rec1.firstName >> rec1.lastName >> rec1.score;

person rec2;
data2 >> rec2.firstName >> rec2.lastName >> rec2.score;
while (!data2.eof())
cout << rec2.lastName << ", " << rec2.firstNamee << " " << rec2.score << endl;
data2 >> rec2.firstName >> rec2.lastName >> rec2.score;

return 0;

Therefore, I want it to output like this:

Bradley, Jenifer 90
David, Larry 100
Zee, Betty 70
Last edited on
Why are you reading from two files? Isn't the data all in one file?

If that's the case, then you will need an array (or vector) of person records.
You will need to read the data file adding each record to the array until you reach end of file.
You will then need to sort the array.

Not going to write the code for you. We don't do homework. If you run into problems, post back and we will try and help.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.

Yes, I have to read the data from two files. I will try your method. Thanks.
Topic archived. No new replies allowed.