Sort and Read file
Jan 21, 2014 at 12:44am UTC
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.
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>
using namespace std;
ifstream theFile( "A1.txt");
string firstName, lastName, address;
int ssn, age;
cout << "ERROR" << endl;
theFile >> firstName;
cout << firstName<< endl;
theFile >> ssn;
cout << ssn<< endl;
theFile >> age;
cout << age<< endl;
cout <<address << endl;
The format of the document
3930 4th Blvd, Yourcity, NJ 88710
36 Jon Havey Court, Middle, MO 66222
Jan 21, 2014 at 11:00am UTC
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:
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
Topic archived. No new replies allowed.