Adivce on populating a datastructure with textfile
Hello everyone!
I'm devloping an address book and I hope it will do the following:
-Add new entry
-Delete entry
-Display all entries
-Sort entries based on lastname
If time permits to also search entries.
Here is what I have so far:
Code:
//Person.h
....
....
....
friend ostream& operator<<(ostream& os, const Person& p);
private:
string name;
string phone;
string city;
string state;
string address;
int zip;
};
Code:
//Person.cpp
...
...
...
//overloading ostream for pretty formatting
ostream& operator<<(ostream& os, const Person& p)
{
os << p.getName() <<" " <<p.getAddress() <<" "
<< p.getCity() <<" " <<p.getState() << " "
<< p.getZip() <<" " <<p.getPhone() << "\n";
return os;
}
In my main function I have a sample program to do a messy input into a text file:
Code:
#include "Person.h"
#include <fstream>
void main()
{
Person p("Cory Sanchez","814-591-0187",
"2217 Bond street","Brockway","PA",15824);
Person c;
c.setName("Alisa Haney");
c.setPhone("814-268-1207");
c.setCity("Brockway");
c.setState("PA");
c.setZip(15821);
c.setAddress("2217 Bond Street Ext");
cout <<"Name: " << p.getName() << endl;
cout <<"Phone: " << p.getPhone() << endl;
cout <<"City: " << p.getCity() << endl;
cout <<"State: " << p.getState() << endl;
cout <<"Zip: " << p.getZip() << endl;
cout << p;
ofstream myfile;
myfile.open ("C:\\example.txt");
myfile << c;
myfile << p;
myfile.close();
}
This will output the following to a text file named example.txt:
Code:
Alisa Haney 2217 Bond Street Ext Brockway PA 15821 814-268-1207
Cory Sanchez 2217 Bond street Brockway PA 15824 814-591-0187
Now what I want to do is read that text file and populate a data structure of some kid, a map or a set or any other data structure (i'm not sure which would be best yet for sorting/adding/deleting).
Should I break up my text file with pipes? like:
Cory| Sanchez | 2217 Bond Street | Brockway | PA| 15824 | 814-268-1027 '\n';
That way I read until I hit a '|' then place whatever I read into the proper data member, like name? By writing this it makes me think I need to break the data member 'name' into fristName and lastName
So once I read the first line, each object will have the all the data to mess around with.
Am I designing my class correctly so when it comes time I will be able to implement my desired operations on the data?
EDIT: I also am going to need to add a menu, but im' not sure where it would be placed. Its not related to Person so I wouldn't put it in a Person class, but the menu doesn't have any data members, its just going to be a simple function with some switch statements.
Thanks!