Thread: parsing author name for book inventory prog

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    parsing author name for book inventory prog

    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;
    }
    Last edited by curlious; 09-27-2003 at 11:04 AM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I figured it out here is the working function:
    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{
             first_name="";
             middle_name="";
             last_name="";
        }
           
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-15-2005, 02:38 PM