Sort and Read file

Hi I am trying read in a file and sort it with user number SSN number, but I have no idea how to do it. I also need to separate the first name and last name with the comma. Another problem tends to show up when I compile the program. There is an extra line between age and the address, and it only occurs to the first person's information. Any help will be appreciated.

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


int main()
{
    ifstream theFile("A1.txt");

    string firstName, lastName, address;
    char ch;
    int ssn, age;

    if(!theFile)
    {
        cout << "ERROR" << endl;
    }

    else
    {
        while(!theFile.eof())
        {
            theFile >> firstName;
            cout << firstName<< endl;
            theFile >> ssn;
            cout << ssn<< endl;
            theFile >> age;
            cout << age<< endl;
            while(getline(theFile,address))
            {
                cout <<address << endl;
            }

        }
        theFile.close();
    }
    return 0;
}


The format of the document
Matthews,Jocob
459237148
19
3930 4th Blvd, Yourcity, NJ 88710

Garfield,Kitty
891772436
24
36 Jon Havey Court, Middle, MO 66222

If you want to sort multiple entries you need a container such as an array, vector, list etc.
In the standard library there is a sort function:

http://www.cplusplus.com/reference/algorithm/sort/?kw=sort

Better put ther required variables into a struct or class

There is an extra line between age and the address
This proble occurs due to the fact that the operator>> leaves and end of line in the stream. getline() takes it as an empty line.

line 31: are you sure you want to read the address until the end of file?
line 23: there might be other errors. So better use something like while(theFile.good())
Topic archived. No new replies allowed.