I am working on an addressbook program, which reads data from a csv file of the following format
“last name”, “first name”, “nickname”, “email1”, “email2”, “phone1”, “phone2”,“address”, “website”, “birthday”, “notes”
I have read the file using getline in the following way:
if(!input.fail())
{
cout<<"File opened"<<endl;
while(!input.eof())
{
getline(input,list) ;
contactlist.push_back(list);
token=con.tokenize(list); // NOT SURE IF I'm doing this right..am I?
}
}
and I’m using the tokenize member function of one of my classes contact, which looks like this
// member function reads in a string and tokenizes it
vector<string>Contact::tokenize(string line)
{
int x = 0, y = 0,i=0;
string token;
vector<string>tokens;
while(x < line.length() && y < line.length())
{
x = line.find_first_not_of(",", y);
if(x >=0 && x < line.length())
{
y = line.find_first_of(",", x);
token = line.substr(x, y-x);
tokens.push_back(token);
i++;
}
}
}
I now need to read the tokenized vector into a private vector member variable of another class and also need to read them into individual private variable of first name,lastname …note of class Contact.How do I read them into private vector member variable of a classtype and how would I call them in the member functions that would do evaluations such sorting adding a contact using the vectors.
and in total I have 2 header files Contact and addressbook with their respective implementation files and a main.
Also If you happen to have a clear concept accessing vectors of vectors/vectors within a vector like I have contactlist and token here in main
First you should separate your tokenize function from the contact class. It’s not in the responsibility of a contact to read a csv row. So extract this method to a new tokenizer class, just write a free tokenize function or use a solution like boost tokenizer.
With the generated tokens you can create a contact instance or pass it to another class.