I have wrote a string that will extract first, middle, and last names from a string but it doesn't seem to work here is some code:
Code:
void Author::parse_name(string tauthor){
const string delims(" \t");
int begIdx,endIdx,tIdx;
//search for beginning of first word
begIdx=tauthor.find_first_not_of(delims, begIdx);
//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)
last_name=tauthor.substr(begIdx,endIdx-begIdx);
else
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{
first_name="";
middle_name="";
last_name="";
}
}
the constructor for the Author class
Code:
Author::Author(string tauthor){
parse_name(tauthor);
cout << first_name<<endl;
cout << last_name<<endl;
cout << middle_name<<endl;
}
finally my little main test
Code:
#include <iostream>
using std::cout;
using std::cin;
#include <stdlib.h>
#include "book.h"
#include "author.h"
int main(int argc, char *argv[])
{
Book Fiction("The Once and Future King");
cout << Fiction;
cin >> Fiction;
cout << Fiction;
Author SomeGuy("Jon Monroe Stevens");
system("PAUSE");
return 0;
}