Thank you for taking the time to help me! I am working on a program that keeps information about staff members, such as volunteers and employees. The problem I am having is that when I run the program (everything compiles fine), I enter a member, and then when I tell the program to display the members, the member list is not printed out. It prints the main menu prompt infinitely. Also, when I add a member and then compute the pay, the program displays "pay computed" then the main menu prompt, and repeats this pattern infinitely. Also, when I add a member and then search for the member, the program has a windows error and then terminates. So, I think something is wrong with my add function, or my StaffMemberParser function, which actually creates the member that is added to the list.
I have attached most of my files so that if you want to, you can run it and see what's going on for yourself. The last file is attached in the next post. I am also putting the file I think is the problem, which is StaffMemberParser.h, below. Can someone help me figure out why its not doing what I want it to?
Thank you very much in advance for any help you can provide!!
Code:
// StaffMemberParser.h
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
#include "StaffMember.h"
#include "FullTimeEmployee.h"
#include "HourlyEmployee.h"
#include "Volunteer.h"
// protections
#ifndef StaffMemberParser_H
#define StaffMemberParser_H
using namespace std;
class BadConversion : public std::runtime_error
{
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};
inline double convertToDouble(const std::string& s)
{
std::istringstream i(s);
double x;
if (!(i >> x))
throw BadConversion("convertToDouble(\"" + s + "\")");
return x;
}
inline int convertToInt(const std::string& s)
{
std::istringstream i(s);
int x;
if (!(i >> x))
throw BadConversion("convertToInt(\"" + s + "\")");
return x;
}
class StaffMemberParser
{
public:
static StaffMember * parseStringToMember(string lineToParse)
{
const char *ptr = lineToParse.c_str();
char field [100];
int n, i = 0, hoursWorked;
string type, firstName, lastName, employeeId;
double rate, bonus;
StaffMember * newMember;
while ( sscanf(ptr, "%31[^/]%n", field, &n) == 1 )
{
switch (i++)
{
case 0: type = field; break;
case 1: firstName = field; break;
case 2: lastName = field; break;
case 3: employeeId = field; break;
case 4:
if ( type == "Volunteer" )
{
newMember = new Volunteer(firstName, lastName, employeeId);
return newMember;
}
else if ( type == "HourlyEmployee" )
{
rate = convertToDouble(field);
}
else if ( type == "FullTimeEmployee" )
{
rate = convertToDouble(field);
}
break;
case 5:
if ( type == "Volunteer" )
{
// nothing to do
}
else if ( type == "HourlyEmployee" )
{
hoursWorked = convertToInt(field);
newMember = new HourlyEmployee(firstName, lastName, employeeId, rate, hoursWorked);
return newMember;
}
else if ( type == "FullTimeEmployee" )
{
bonus = convertToDouble(field);
newMember = new FullTimeEmployee(firstName, lastName, employeeId, rate, bonus);
return newMember;
}
break;
default: break;
}
ptr += n;
if ( *ptr != '/' )
{
break; // didn't find an expected delimiter
}
while ( *ptr == '/' )
{
++ptr; // skip the delimiter
}
}
}
};
#endif