parsing name problem

This is a discussion on parsing name problem within the C++ Programming forums, part of the General Programming Boards category; I was reviewing my parse author function for my book inventory problem and discovered it did not work if only ...

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

    parsing name problem

    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="";
        }
           
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I spotted half the problem this morning:
    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="";//***** here should be middle_name doh 
            }           
        }
        else{
             first_name="";
             middle_name="";
             last_name="";
        }
           
    }
    Anyhow now it is accepting single names as input but when it displays the name it shows for instance:Johnathan 2569
    I can't figure out where the 2569 is coming from.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem parsing 16-bit Hex to ASCII
    By black_spot1984 in forum C Programming
    Replies: 12
    Last Post: 12-05-2006, 11:13 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. pointer problem
    By R.Stiltskin in forum C Programming
    Replies: 25
    Last Post: 10-20-2005, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21