Hello, and thank you for taking the time to help me with this! 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 members, and then when I tell the program to display the members, nothing prints out. It just prompts for the next input. Is this an error in my main function, or in my StaffMemberParser function, which is called from the main function and used to add a member to the list? I am including both my main function, called Assignment7.cpp, and the StaffMemberParser.h file which I think could be the problem.
Can someone help me figure out why its not doing what I want it to?
Here is my code:
// Assignment #: 7
// Name:
// EmailAddress:
// Description: It displays a menu of choices
// (add volunteers, full time employees, or hourly employees,
// compute their pay, search a member, list members,
// quit, display menu) to a user.
// Then it performs the chosen task. It will keep asking a user
// to enter the next choice until the choice of 'Q' (Quit) is
// entered.
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "StaffMember.h"
#include "StaffMemberParser.h"
using namespace std;
void printMenu();
int main()
{
char input1;
string inputInfo;
bool operation;
vector<StaffMember *> memberList;
printMenu(); // print out menu
do
{
cout << "What action would you like to perform?" << endl;
cin >> input1;
switch (input1)
{
case 'A': //Add Member
cout << "Please enter a member information to add:\n";
cin >> inputInfo;
StaffMemberParser::parseStringToMember(inputInfo);
break;
case 'C': //Compute Pay
for (int j=0; j < memberList.size(); j++)
{
memberList.at(j)->computePay();
}
cout << "pay computed\n";
break;
case 'D': //Search for Member
cout << "Please enter a memberID to search:\n";
cin >> inputInfo;
operation = false;
for (int j=0; j < memberList.size(); j++)
{
if (inputInfo == memberList.at(j)->getMemberId())
{
operation = true;
}
else
{
operation = false;
}
}
if (operation == true)
cout << "member found\n";
else
cout << "member not found\n";
break;
case 'L': //List Members
if (sizeof(memberList) == 0)
{
cout << "no member\n" << endl;
}
else
{
for (int j=0; j < memberList.size(); j++)
{
memberList.at(j)->printInfo();
}
}
break;
case 'Q': //Quit
for (int j=0; j < memberList.size(); j++)
{
delete memberList.at(j);
}
break;
case '?': //Display Menu
printMenu();
break;
default:
cout << "Unknown action\n";
break;
}
} while (input1 != 'Q' && input1 != 'q'); // stop the loop when Q is read
}
// 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