I was reviewing my parse author function for my book inventory problem and discovered it did not work if only one name ie first name was entered. I revised the code but it doesn't seem to work correctly please have a look.
Code:
void Author::parse_name(string tauthor){
const string delims(" \t");
int begIdx,endIdx,tIdx=0;
//search for beginning of first word
begIdx=tauthor.find_first_not_of(delims,0);
//while beginning of word found
if (begIdx!=string::npos){
// end or word
endIdx=tauthor.find_first_of(delims,begIdx);
first_name=tauthor.substr(begIdx,(endIdx-begIdx));
begIdx=tauthor.find_first_not_of(delims,endIdx);
if (begIdx!=string::npos){
endIdx=tauthor.find_first_of(delims,begIdx);
tIdx=tauthor.find_first_not_of(delims,endIdx);
// if tIdx is at end of tauthor string only first,last are given
if (tIdx!=string::npos)
{
middle_name=tauthor.substr(begIdx,endIdx-begIdx);
//extract lastname
begIdx=tauthor.find_first_not_of(delims,endIdx);
endIdx=tauthor.length();
last_name=tauthor.substr(begIdx,endIdx-begIdx);
}
else
{
last_name=tauthor.substr(begIdx,endIdx-begIdx);
middle_name="";
}
}
else{
last_name="";
first_name="";
}
}
else{
first_name="";
middle_name="";
last_name="";
}
}