Add employee
Delete employee
save file
exit
I have added another option, which is to load the saved file back into the vector. I have tried to write the code, but it goes straight to "Database could not be loaded", and I am stuck. I have only posted the code relevant to load. Name, salary and ID are all in the base class Employee, and Manager, Engineer & Researcher are all derived from that using polymorphism. I can write the load code, to print out the saved file, but cannot write it back into the vector, which is what I am trying to do. So here are the code snippets, the main is the full main.cpp. I want to be able to load the file based on the ID number, which is 1 = manager, 2 = engineer & 3 = researcher. I will only post the code relevant to a manager, as the rest are the same.
Code:
//Employee.h
class Manager : public Employee
{
public:
Manager();
Manager(const string& fName,
const string& lName,
int sal,
int IDnum,
int meeting,
int holiday);
void createEmployee();
void showEmployee();
void save(ofstream& outfile);
void load(ifstream& infile);
private:
int mIDNum;
int mNumMeeting;
int mNumHoliday;
};
Code:
//Employee.cpp
Manager::Manager()
{
int IDnum = 0;
int mNumMeeting = 0;
int mNumHoliday = 0;
}
Manager::Manager(const string& fName,
const string& lName,
int sal,
int IDnum,
int meeting,
int holiday)
: Employee(fName, lName, sal)
{
mIDNum = IDnum;
mNumMeeting = meeting;
mNumHoliday = holiday;
}
void Manager::createEmployee()
{
Employee::createEmployee();
mIDNum = 1;
cout << "Number of meetings to attend per week ";
cin >> mNumMeeting;
cout << endl;
cout << "Number of holidays per year ";
cin >> mNumHoliday;
cout << endl;
}
void Manager::showEmployee()
{
Employee::showEmployee();
cout << "Number of meetings to attend per week " << mNumMeeting;
cout << endl;
cout << "Number of holidays per year " << mNumHoliday;
cout << endl;
}
void Manager::save(ofstream& outfile)
{
Employee::save(outfile);
outfile << "ID number is " << mIDNum << endl;
outfile << "Number of meetings to attend per week " << mNumMeeting << endl;
outfile << "Number of holidays per year " << mNumHoliday << endl;
outfile << endl;
}
void Manager::load(ifstream& infile)
{
Employee::load(infile);
string garbage;
infile >> garbage >> mIDNum;
infile >> garbage >> mNumMeeting;
infile >> garbage >> mNumHoliday;
}
Code:
//main.cpp
//Main.cpp
#include <crtdbg.h> // To check for memory leaks
#define _CRTDBG_MAP_ALLOC // To check for memory leaks
#include <iostream>
#include <string>
#include <vector>
#include "Employee.h"
using namespace std;
int main()
{
// To check for memory leaks
int nDbgFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
nDbgFlags |= _CRTDBG_LEAK_CHECK_DF;
nDbgFlags |= _CRTDBG_ALLOC_MEM_DF;
_CrtSetDbgFlag(nDbgFlags);
vector<Employee*> Database;
int choice = 0;
bool quit = false;
while(!quit)
{
cout << "Database size = " << Database.size() << endl << endl;
cout << "Database contains: " << endl << endl;
for(unsigned int i = 0; i < Database.size(); ++i)
cout << Database[i]->getFirstName() << " " <<
Database[i]->getLastName() << " " << endl;
cout << endl << endl;
cout << "Employee database: Please select an option" << endl;
cout << "==========================================" << endl;
cout << "1) Add an employee" << endl;
cout << "2) Delete an employee" << endl;
cout << "3) Save the database" << endl;
cout << "4) Load the database" << endl;
cout << "5) Exit" << endl << endl;
cout << "Please enter your selection: ";
cin >> choice;
cout << endl << endl;
if(choice <=0 || choice >5)
{
cout << "Invalid selection,please try again" << endl;
continue;
}
if(choice == 1)
{
int choice = 0;
cout << "1) Add Manager" << endl;
cout << "2) Add Engineer" << endl;
cout << "3) Add Researcher" << endl << endl;
cout << "Enter selection: ";
cin >> choice;
cout << endl << endl;
switch(choice)
{
case 1:
{
Employee* manager = new Manager;
manager->createEmployee();
Database.push_back(manager);
break;
}
case 2:
{
Employee* engineer = new Engineer;
engineer->createEmployee();
Database.push_back(engineer);
break;
}
case 3:
{
Employee* researcher = new Researcher;
researcher->createEmployee();
Database.push_back(researcher);
break;
}
}
}
if(choice == 2)
{
string firstName;
string lastName;
cout << "Please enter the first & last name of the employee to remove: ";
cin >> firstName >> lastName;
cout << endl;
for(unsigned int i = 0; i < Database.size(); ++i)
{
if(Database[i]->getFirstName()== firstName && Database[i]->getLastName()== lastName )
{
Database.erase(Database.begin() + i);
cout << firstName << " " << lastName << " has now been removed, database updated" << endl;
cout << endl;
}
else
{
cout << firstName << " " << lastName << " does not exist" << endl;
continue;
}
}
}
if(choice == 3)
{
ofstream outfile ("EmployeeDataBase.txt");
if(outfile)
{
for(unsigned int i = 0; i < Database.size(); ++i)
Database[i]->save(outfile);
}
else
cout << "Could not save the data" << endl;
continue;
}
if(choice == 4)
{
ifstream infile ("EmployeeDataBase.txt");
int IDnum = 0;
if(infile)
{
for(unsigned int i = 0; i < Database.size(); ++i)
Database[i]->load(infile);
if(IDnum == 1)
{
Employee* manager = new Manager;
Database.push_back(manager);
}
else if(IDnum == 2)
{
Employee* engineer = new Engineer;
Database.push_back(engineer);
}
else if(IDnum == 3)
{
Employee* researcher = new Researcher;
Database.push_back(researcher);
}
else
cout << "Database could not be loaded" << endl;
cout << endl;
continue;
}
}
if(choice == 5)
{
quit = true;
break;
}
}
for(unsigned int i = 0; i < Database.size();++i)
delete Database[i];
}

