Originally Posted by
tabstop
Code:
StaffMember * parseStringToMember(inputInfo);
You want to call a function, not declare a variable.
Okay, so like this, maybe?
Code:
parseStringToMember(inputInfo);
I am getting an error at that line, though, so I'm guessing that is not right. Here is the error:
45 Assignment7.cpp `parseStringToMember' undeclared (first use this function)
Originally Posted by
tabstop
And I don't think I was thinking clearly: I'm pretty sure, at the top, you want
Code:
StaffMember * newguy;
(Notice how the type means we're declaring a variable?) Then I'm pretty sure you can do
Code:
newguy = new Whatever_type(appropriate things here);
rather than a separate pointer for each different type.
Okay, that makes sense. I think I get that. I am still getting a few errors, though many many less. I am getting these errors:
64 StaffMemberParser.h `Volunteer' has not been declared
84 StaffMemberParser.h `HourlyEmployee' has not been declared
90 StaffMemberParser.h `FullTimeEmployee' has not been declared
on the lines where I use the code you suggested above, though.
Here are the updated versions of my code:
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;
parseStringToMember(inputInfo);
break;
case 'C': //Compute Pay
for (int j=0; j < memberList.size(); j++)
{
memberList.at(j)->computePay();
}
cout << "pay computed\n";
break;
default:
cout << "Unknown action\n";
break;
}
} while (input1 != 'Q' && input1 != 'q'); // stop the loop when Q is read
}
Code:
// StaffMemberParser.h
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
#include "StaffMember.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
{
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); // this is line 63
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