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?