Thread: Parsing a string

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    14

    Parsing a string

    Hello all,
    I need help in trying to parse a string as part of my assignment. My function I have will receive a string and the values are each separated with forward slash (/) delimiters. The format of the string will be either of the following:

    type/firstName/lastName/employeeId (for volunteer)
    type/firstName/lastName/emloyeeId/rate/bonus (for fulltime employee)
    type/firstName/lastName/emloyeeId/rate/hoursWorked (for hourly employee)



    When I receive the string the user will enter something like:

    HourlyEmployee/John/Smith/000002/25.25/10
    Here is how my function code looks so far:

    Code:
    #include <string>
    #include "StaffMember.h"
    using namespace std;
    #ifndef STAFFMEMBERPARSER_H
    
    #define STAFFMEMBERPARSER_H
    
    class StaffMemberParser
    {
     
        public:
        static StaffMember* parseStringToMember(string lineToParse)
        {
        string stype, sfirstname, slastname, semployeeId, srate, shours; //I initialize the data here
        int pos, pos2, pos3, pos4, pos5, pos6;                           // because it would give me an error otherwise. 
        
            pos = lineToParse.find("/"); 
            stype = lineToParse.substr(0,pos);
            
            pos2 = lineToParse.find("/", pos+1); 
            sfirstname = lineToParse.substr(pos+1,pos2);
            
            pos3 = lineToParse.find("/", pos2+1); 
            slastname = lineToParse.substr(pos2+1,pos3);
            
            pos4 = lineToParse.find("/", pos3+1); 
            semployeeId = lineToParse.substr(pos3+1,pos4);
         
            if(stype == "Volunteer")
            {
               
            }
         
            if(stype == "FullTimeEmployee")
            {
            //pos5 = lineToParse.find("/"); 
           // srate = lineToParse.substr(pos4+1,pos5);
            }
            
            if(stype == "HourlyEmployee")
            {
           // pos6 = lineToParse.find("/"); 
            //shours = lineToParse.substr(pos5+1);
            }
            
            cout << "Type\t\t" << stype << endl;
            cout << "Firstname\t\t" << sfirstname << endl;
            cout << "Lastname\t\t" << slastname << endl;
            cout << "EmployeeId\t\t" << semployeeId << endl;
            cout << "Rate\t\t" << srate << endl;
            cout << "Hours\t\t" << shours << endl;
    
        }
    };
    #endif
    At the end of the function I'll test it with cout to make sure it parsed the values correctly.

    So when I test it, I enter "HourlyEmployee/John/Smith/000002/25.25/10" as the string.

    And the output I get is this:
    Code:
    Type            HourlyEmployee                                                                                                                                                       
    Firstname               John/Smith/000002/2                                                                                                                                          
    Lastname                Smith/000002/25.25/10                                                                                                                                        
    EmployeeId              000002/25.25/10                                                                                                                                              
    Rate                                                                                                                                                                                 
    Hours
    It parses the Type correctly , but I can't figure out why it wont work with the rest. Any ideas?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read up on substr string::substr - C++ Reference

    The second param is length you are passing index instead.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    14
    Quote Originally Posted by stahta01 View Post
    Read up on substr string::substr - C++ Reference

    The second param is length you are passing index instead.

    Tim S.
    So my first parameter is right? How would I get the length up to the next delimiter?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    length is almost pos2-pos1 you will likely need to subtract 1.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    14
    Quote Originally Posted by stahta01 View Post
    length is almost pos2-pos1 you will likely need to subtract 1.
    Thank you.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You really should consider using a stringstream to parse that string, and why the static member function in a separate class? Where are you returning the value you promised (the Staffmember*)? This "function" really looks like it should be a member of the StaffMember class instead of in it's own class. Also note that in C++ not everything must be in some random class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing a string
    By sigur47 in forum C Programming
    Replies: 4
    Last Post: 05-06-2012, 04:30 AM
  2. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 PM
  3. String Parsing
    By Shaun32887 in forum C++ Programming
    Replies: 11
    Last Post: 07-02-2008, 02:15 PM
  4. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  5. String parsing
    By Cbuild in forum C Programming
    Replies: 5
    Last Post: 05-03-2004, 05:15 PM

Tags for this Thread