Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

comments for the following code: main.cpp: #include \"functions.h\" #include <io

ID: 3831446 • Letter: C

Question

comments for the following code:

main.cpp:

#include "functions.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;


int main()
{

   int choice;
   functions fun;
   while (1) {
      
       fun.showMenu();
       cin >> choice;
       if (choice == 1) {
           char a[50];
           cout << "Input file:";
           cin >> a;
           try
           {
               fun.readfromfile(a);
           }
           catch (...)
           {
               cout << "error reading files" << endl;
               exit(0);
           }
           cin.get();
           cin.get();
       }
       else if (choice == 2) {
           if (managers.list.empty())
           {
               cout << "u need to hire manager" << endl;
           }
           else managers.print();
           cin.get();
           cin.get();
       }
       else if (choice == 3) {
           if (worker.list.empty())
           {
               cout << "u need to hire worker" << endl;
           }
           else worker.print();
           cin.get();
           cin.get();
       }
       else if (choice == 4) {
           bool boolean = fun.EditEmployee();
           if (boolean == true)
           {
               cout << "information sas changed" << endl;
           }
           else cout << "the employee you search is not in database" << endl;
           cin.get();
           cin.get();
       }
       else if (choice == 5) {
           fun.searchEmployee();
           cin.get();
           cin.get();
       }
       else if (choice == 6) {
           bool boolean = fun.addEmployee();
           if (boolean == false)
           {
               cout << "you did not hire this guy eventually";
           }
           else cout << "Hired!" << endl;
           cin.get();
           cin.get();
       }
       else if (choice == 7) {
           string name;
           cout << "Input name:";
           cin.get();
           getline(cin, name);
           int index = managers.search(name);
           if (index != -1) {
               managers.list.erase(managers.list.begin() + index);
               cout << "Released successfuly!" << endl;
           }
           else {
               index = worker.search(name);
               if (index != -1) {
                   worker.list.erase(worker.list.begin() + index);
                   cout << "Released successfuly!" << endl;
               }
               else cout << "the employee you search is not in database" << endl;
           }
           cin.get();
           cin.get();
       }
       else if (choice == 8) {
           managers.Sort();
           worker.Sort();
           cin.get();
           cin.get();
       }
       else if (choice == 9) {
           fun.printTofile();
           break;
       }
       else {
           cout << "please chooose between 1-9 to use the program!";
           cin.get();
           cin.get();
       }
   }
   cout << "Thanks for use!" << endl;
   return 0;
}

_______________________

employee.cpp

#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;

Employee::Employee()
{
   Name = "notdefined";
   Tel = "notdefined";
   Address = "notdefined";
   Sex = "notdefined";
   Ages = 0;
   Department = "notdefined";

}

Employee::Employee(string name,string tel,string address, string sex, int ages, string department)
{
   Name = name;
   Tel = tel;
   Address = address;
   Sex = sex;
   Ages = ages;
   Department = department;

}
Employee::~Employee()
{

};
Worker::Worker():Employee()
{

}

Worker::Worker(string name, string tel, string address, string sex, int ages, string department,string manager) :Employee(name, tel, address, sex, ages, department)
{
   Name = name;
   Tel = tel;
   Address = address;
   Sex = sex;
   Ages = ages;
   Department = department;
   Manager = manager;
}
Worker::~Worker()
{

}

Manager::Manager():Employee()
{

}
Manager::Manager(string name, string tel, string address, string sex, int ages, string department,int managerednum) : Employee(name, tel, address, sex, ages, department)
{
   Name = name;
   Tel = tel;
   Address = address;
   Sex = sex;
   Ages = ages;
   Department = department;
   ManageredNum = managerednum;
}
Manager::~Manager()
{

}

void Employee::setName(string name)
{
   Name = name;
}
void Employee::setTel(string tel)
{
   Tel = tel;
}
void Employee::setAddress(string address)
{
   Address = address;
}
void Employee::setSex(string sex)
{
   Sex = sex;
}
void Employee::setAges(int ages)
{
   Ages = ages;
}
void Employee::setDepartment(string department)
{
   Department = department;
}
string Employee::getName()
{
   return Name;
}
string Employee::getTel()
{
   return Tel;
}
string Employee::getAddress()
{
   return Address;
}
string Employee::getSex()
{
   return Sex;
}
string Employee::getDepartment()
{
   return Department;
}
int Employee::getAges()
{
   return Ages;
}
void Worker::setManager(string manager)
{
   Manager = manager;
}
string Worker::getManager()
{
   return Manager;
}
string Worker::getPosition()
{
   return "Worker";
}

void Manager::setManageredNum(int managerednum)
{
   ManageredNum = managerednum;
}
int Manager::getManageredNum()
{
   return ManageredNum;
}
string Manager::getPosition()
{
   return "Manager";
}

______________

employee.h:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Employee {
protected:
   string Name;
   string Tel;
   string Address;
   string Sex;
   int Ages;
   string Department;
public:
   Employee();
   Employee(string, string, string, string, int, string);
virtual ~Employee();
   void setName(string name);
   void setTel(string tel);
   void setAddress(string address);
   void setSex(string Sex);
   void setAges(int ages);
   void setDepartment(string department);
   string getName();
   string getTel();
   string getAddress();
   string getSex();
   virtual string getPosition()=0;
   int getAges();
   string getDepartment();
};
class Worker : public Employee {
private:
   string Manager;
public:
   Worker();
   Worker(string, string, string, string, int, string,string);
   ~Worker();
   void setManager(string manager);
   string getManager();
   bool operator < (Worker &a) {
       return getAges()<a.getAges();
   }
   ostream& operator <<(ostream& outs) {
       outs << "Name: " << getName() << endl;
       outs << "Tel: " << getTel() << endl;
       outs << "Address: " << getAddress() << endl;
       outs << "Sex: " << getSex() << endl;
       outs << "Position: " << getPosition() << endl;
       outs << "Ages: " << getAges() << endl;
       outs << "Department: " << getDepartment() << endl;
       outs << "Manager: " << getManager() << endl;
       outs << "--------------------------------" << endl;
       return outs;
   }
   virtual string getPosition();
};

class Manager : public Employee {
private:
   int ManageredNum;
public:
   Manager();
   Manager(string, string, string, string, int, string,int);
   ~Manager();
   void setManageredNum(int num);
   int getManageredNum();
   bool operator < (Manager &a)
   {
       return getAges()<a.getAges();
   }
   ostream& operator <<(ostream& outs) {
       outs << "Name: " << getName() << endl;
       outs << "Tel: " << getTel() << endl;
       outs << "Address: " << getAddress() << endl;
       outs << "Sex: " << getSex() << endl;
       outs << "Position: " << getPosition() << endl;
       outs << "Ages: " << getAges() << endl;
       outs << "Department: " << getDepartment() << endl;
       outs << "Managered Number: " << getManageredNum() << endl;
       outs << "--------------------------------" << endl;
       return outs;
   }
   virtual string getPosition();
};

class functions
{
public:
   void showMenu();
   void readfromfile(char* url) throw (FileIOException);
   bool EditEmployee();
   void searchEmployee();
   bool addEmployee();
   void printTofile();
};

template<typename T>
class Company {
public:
   vector<T> list;
   void add(T o);

   int search(string name);

   void print();
  
   void Sort();

  

};

class FileIOException
{
     
};

__________

functions.h:

#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
Company<Worker> worker;
Company<Manager> managers;

void functions::showMenu() {
   cout << "-------------------------------------------" << endl;
   cout << " Welcome to the employee system" << endl;
   cout << " You can hire 5 worker and 5 manager, 10 total employee" << endl;
   cout << " 1. Read from file" << endl;
   cout << " 2. Show all Manager" << endl;
   cout << " 3. Show all Worker" << endl;
   cout << " 4. Edit employee's information" << endl;
   cout << " 5. Search employee " << endl;
   cout << " 6. Add employee" << endl;
   cout << " 7. release employee" << endl;
   cout << " 8. sort employee by ages" << endl;
   cout << " 9. prnit to file and exit" << endl;
   cout << "-------------------------------------------" << endl;
}
void functions::readfromfile(char* url) throw (FileIOException)
{
   char buffer[50];
   string name, tel, address, sex, department, position, manager;
   int years, num;
   int i = 1;
   ifstream infile(url);
   if (infile.fail())
   {
       throw 0;
   }
   while (!infile.eof())
   {

       infile.getline(buffer, 50);
       if (i == 1) {
           name = "";
           for (int j = 0; j<strlen(buffer); j++) name += buffer[j];
           i++;
       }
       else if (i == 2) {
           tel = "";
           for (int j = 0; j<strlen(buffer); j++) tel += buffer[j];
           i++;
       }
       else if (i == 3) {
           address = "";
           for (int j = 0; j<strlen(buffer); j++) address += buffer[j];
           i++;
       }
       else if (i == 4) {
           sex = "";
           for (int j = 0; j<strlen(buffer); j++) sex += buffer[j];
           i++;
       }
       else if (i == 5) {
           years = atoi(buffer);
           i++;
       }
       else if (i == 6) {
           department = "";
           for (int j = 0; j<strlen(buffer); j++) department += buffer[j];
           i++;
       }
       else if (i == 7) {
           position = "";
           for (int j = 0; j<strlen(buffer); j++) position += buffer[j];
           i++;
       }
       else {
           if (position == "Worker"||"worker") {
               Worker emp;
               emp.setName(name);
               emp.setTel(tel);
               emp.setAddress(address);
               emp.setSex(sex);
               emp.setAges(years);
               emp.setDepartment(department);
               manager = "";
               for (int j = 0; j<strlen(buffer); j++) manager += buffer[j];
               emp.setManager(manager);
               worker.add(emp);
           }
           else {
               Manager ma;
               ma.setName(name);
               ma.setTel(tel);
               ma.setAddress(address);
               ma.setSex(sex);
               ma.setAges(years);
               ma.setDepartment(department);
               num = atoi(buffer);
               ma.setManageredNum(num);
               managers.add(ma);
           }
           i = 1;
       }
   }
}
bool functions::EditEmployee() {
   string name;
   int index;
   cout << "Input name:";
   cin.get();
   getline(cin, name);
   index = managers.search(name);
   if (index != -1) {

       string tel, address, sex, department;
       int manageredNum, ages;
       cout << "Input Tel:";
       cin >> tel;
       managers.list[index].setTel(tel);
       cin.get();
       cout << "Input Address:";
       address = " ";
       getline(cin, address);
       managers.list[index].setAddress(address);
       cout << "Input Sex:";
       cin >> sex;
       managers.list[index].setSex(sex);
       cout << "Input Ages:";
       cin >> ages;
       managers.list[index].setAges(ages);
       cout << "Input Department:";
       cin >> department;
       managers.list[index].setDepartment(department);
       cout << "Input ManageredNum";
       cin >> manageredNum;
       managers.list[index].setManageredNum(manageredNum);
       return true;
   }
   else {
       index = worker.search(name);
       if (index != -1) {
           string tel, address, sex, department, manager;
           int ages;
           cout << "Input Tel:";
           cin >> tel;
           worker.list[index].setTel(tel);
           cout << "Input Address:";
           cin.get();
           getline(cin, address);
           worker.list[index].setAddress(address);
           cout << "Input Sex:";
           cin >> sex;
           worker.list[index].setSex(sex);
           cout << "Input ages:";
           cin >> ages;
           worker.list[index].setAges(ages);
           cout << "Input Department:";
           cin >> department;
           worker.list[index].setDepartment(department);
           cout << "Input Managere";
           cin >> manager;
           worker.list[index].setManager(manager);
           return true;
       }
       else return false;
   }
   return false;
}
void functions::searchEmployee() {
   string name;
   int index;
   cout << "Input name:";
   cin.get();
   getline(cin, name);
   index = managers.search(name);
   if (index != -1)
   {
       managers.list[index] << cout;
   }
   else
   {
       index = worker.search(name);
       if (index != -1)
       {
           worker.list[index] << cout;
       }
       else cout << "The employee you search is not on database" << endl;
   }
}
bool functions::addEmployee() {
   string name, tel, address, sex, position, department;
   int ages;
   cout << "Input name:";
   cin.get();
   getline(cin, name);
   if (worker.search(name) != -1 || managers.search(name) != -1) {
       return false;
   }
   cout << "Input Tel:";
   cin >> tel;
   cout << "Input Address:";
   cin.get();
   getline(cin, address);
   cout << "Input Sex:";
   cin >> sex;
   cout << "Input Ages:";
   cin >> ages;
   cout << "Input Department:";
   cin >> department;
   cout << "Input position:";
   cin >> position;
   if (position == "Manager") {
       int num;
       cout << "Input ManageredNum:";
       cin >> num;
       Manager mana;
       mana.setName(name);
       mana.setTel(tel);
       mana.setAddress(address);
       mana.setSex(sex);
       mana.setAges(ages);
       mana.setDepartment(department);
       mana.setManageredNum(num);
       if (managers.list.size() >= 5) {
           cout << "Manager list is full" << endl;
           return false;
       }
       else managers.add(mana);
   }
   else {
       string manager;
       cout << "Input Manager:";
       cin >> manager;
       Worker wo;
       wo.setName(name);
       wo.setTel(tel);
       wo.setAddress(address);
       wo.setSex(sex);
       wo.setAges(ages);
       wo.setDepartment(department);
       wo.setManager(manager);
       if (worker.list.size() >= 5) {
           cout << "Worker list is full!" << endl;
           return false;
       }
       else worker.add(wo);
   }
   return true;
}
void functions::printTofile() {
   int i;
   ofstream outfile("database.txt", ofstream::out);
   for (i = 0; i<managers.list.size(); i++) {
       outfile << managers.list[i].getName() << endl << managers.list[i].getTel() << endl << managers.list[i].getAddress() << endl
           << managers.list[i].getSex() << endl << managers.list[i].getAges() << endl << managers.list[i].getDepartment() << endl << managers.list[i].getPosition() << endl
           << managers.list[i].getManageredNum() << endl;
   }
   for (i = 0; i<worker.list.size(); i++) {
       outfile << worker.list[i].getName() << endl << worker.list[i].getTel() << endl << worker.list[i].getAddress() << endl
           << worker.list[i].getSex() << endl << worker.list[i].getAges() << endl << worker.list[i].getDepartment() << endl << worker.list[i].getPosition() << endl
           << worker.list[i].getManager() << endl;
   }
   outfile.close();
}

template<typename T>
void Company<T>::add(T o)
{
   list.push_back(o);
}

template<typename T>
int Company<T>::search(string name)
{
  
   for (int i = 0; i<list.size(); i++) {
       if (list[i].getName() == name) {
           return i;
           break;
       }
   }
   return -1;
}

template<typename T>
void Company<T>::print()
{
   for (int i = 0; i<list.size(); i++)
   {
       list[i] << cout;
   }
}
template<typename T>
void Company<T>::Sort()
{
   sort(list.begin(), list.end());
   cout << "sorted by ages successfully" << endl;
}

Explanation / Answer

main.cpp:

#include "functions.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;


int main()
{

   int choice;
   functions fun;

//run forever
   while (1) {
      
       fun.showMenu();//shows the menu
       cin >> choice;

//invokes appropriate action based on choice
       if (choice == 1) {
           char a[50];
           cout << "Input file:";
           cin >> a;
           try
           {
               fun.readfromfile(a);
           }
           catch (...)
           {
               cout << "error reading files" << endl;
               exit(0);
           }
           cin.get();
           cin.get();
       }
       else if (choice == 2) {
           if (managers.list.empty())
           {
               cout << "u need to hire manager" << endl;
           }
           else managers.print();
           cin.get();
           cin.get();
       }
       else if (choice == 3) {
           if (worker.list.empty())
           {
               cout << "u need to hire worker" << endl;
           }
           else worker.print();
           cin.get();
           cin.get();
       }
       else if (choice == 4) {
           bool boolean = fun.EditEmployee();
           if (boolean == true)
           {
               cout << "information sas changed" << endl;
           }
           else cout << "the employee you search is not in database" << endl;
           cin.get();
           cin.get();
       }
       else if (choice == 5) {
           fun.searchEmployee();
           cin.get();
           cin.get();
       }
       else if (choice == 6) {
           bool boolean = fun.addEmployee();
           if (boolean == false)
           {
               cout << "you did not hire this guy eventually";
           }
           else cout << "Hired!" << endl;
           cin.get();
           cin.get();
       }
       else if (choice == 7) {
           string name;
           cout << "Input name:";
           cin.get();
           getline(cin, name);
           int index = managers.search(name);
           if (index != -1) {
               managers.list.erase(managers.list.begin() + index);
               cout << "Released successfuly!" << endl;
           }
           else {
               index = worker.search(name);
               if (index != -1) {
                   worker.list.erase(worker.list.begin() + index);
                   cout << "Released successfuly!" << endl;
               }
               else cout << "the employee you search is not in database" << endl;
           }
           cin.get();
           cin.get();
       }
       else if (choice == 8) {
           managers.Sort();
           worker.Sort();
           cin.get();
           cin.get();
       }
       else if (choice == 9) {
           fun.printTofile();
           break;
       }
       else {
           cout << "please chooose between 1-9 to use the program!";
           cin.get();
           cin.get();
       }
   }
   cout << "Thanks for use!" << endl;
   return 0;
}

_______________________

employee.cpp

#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;

//default values assigned.

Employee::Employee()
{
   Name = "notdefined";
   Tel = "notdefined";
   Address = "notdefined";
   Sex = "notdefined";
   Ages = 0;
   Department = "notdefined";

}

//parametrizd constructoe to intialize the values

Employee::Employee(string name,string tel,string address, string sex, int ages, string department)
{
   Name = name;
   Tel = tel;
   Address = address;
   Sex = sex;
   Ages = ages;
   Department = department;

}
Employee::~Employee()
{

};
Worker::Worker():Employee()
{

}

//constructor for child class worker which invokes constrcutor of parent class Emplyee to intilaize the values.

Worker::Worker(string name, string tel, string address, string sex, int ages, string department,string manager) :Employee(name, tel, address, sex, ages, department)
{
   Name = name;
   Tel = tel;
   Address = address;
   Sex = sex;
   Ages = ages;
   Department = department;
   Manager = manager;
}
Worker::~Worker()
{

}

Manager::Manager():Employee()
{

}

//constructor for child class Manage which invokes constrcutor of parent class Emplyee to intilaize the values.


Manager::Manager(string name, string tel, string address, string sex, int ages, string department,int managerednum) : Employee(name, tel, address, sex, ages, department)
{
   Name = name;
   Tel = tel;
   Address = address;
   Sex = sex;
   Ages = ages;
   Department = department;
   ManageredNum = managerednum;
}
Manager::~Manager()
{

}

//getters an setters.

void Employee::setName(string name)
{
   Name = name;
}
void Employee::setTel(string tel)
{
   Tel = tel;
}
void Employee::setAddress(string address)
{
   Address = address;
}
void Employee::setSex(string sex)
{
   Sex = sex;
}
void Employee::setAges(int ages)
{
   Ages = ages;
}
void Employee::setDepartment(string department)
{
   Department = department;
}
string Employee::getName()
{
   return Name;
}
string Employee::getTel()
{
   return Tel;
}
string Employee::getAddress()
{
   return Address;
}
string Employee::getSex()
{
   return Sex;
}
string Employee::getDepartment()
{
   return Department;
}
int Employee::getAges()
{
   return Ages;
}
void Worker::setManager(string manager)
{
   Manager = manager;
}
string Worker::getManager()
{
   return Manager;
}
string Worker::getPosition()
{
   return "Worker";
}

void Manager::setManageredNum(int managerednum)
{
   ManageredNum = managerednum;
}
int Manager::getManageredNum()
{
   return ManageredNum;
}
string Manager::getPosition()
{
   return "Manager";
}

______________

employee.h:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Employee {
protected:
   string Name;
   string Tel;
   string Address;
   string Sex;
   int Ages;
   string Department;
public:
   Employee();
   Employee(string, string, string, string, int, string);
virtual ~Employee();
   void setName(string name);
   void setTel(string tel);
   void setAddress(string address);
   void setSex(string Sex);
   void setAges(int ages);
   void setDepartment(string department);
   string getName();
   string getTel();
   string getAddress();
   string getSex();
   virtual string getPosition()=0;
   int getAges();
   string getDepartment();
};
class Worker : public Employee {
private:
   string Manager;
public:
   Worker();
   Worker(string, string, string, string, int, string,string);
   ~Worker();
   void setManager(string manager);
   string getManager();

//overloaded < operator which retrun true if this.age< other.age else false.
   bool operator < (Worker &a) {
       return getAges()<a.getAges();
   }

//overloaded << output stream operator which output employees info
   ostream& operator <<(ostream& outs) {
       outs << "Name: " << getName() << endl;
       outs << "Tel: " << getTel() << endl;
       outs << "Address: " << getAddress() << endl;
       outs << "Sex: " << getSex() << endl;
       outs << "Position: " << getPosition() << endl;
       outs << "Ages: " << getAges() << endl;
       outs << "Department: " << getDepartment() << endl;
       outs << "Manager: " << getManager() << endl;
       outs << "--------------------------------" << endl;
       return outs;
   }
   virtual string getPosition();
};

//manager extends employees

class Manager : public Employee {
private:
   int ManageredNum;
public:
   Manager();
   Manager(string, string, string, string, int, string,int);
   ~Manager();
   void setManageredNum(int num);
   int getManageredNum();
   bool operator < (Manager &a)
   {
       return getAges()<a.getAges();
   }
   ostream& operator <<(ostream& outs) {
       outs << "Name: " << getName() << endl;
       outs << "Tel: " << getTel() << endl;
       outs << "Address: " << getAddress() << endl;
       outs << "Sex: " << getSex() << endl;
       outs << "Position: " << getPosition() << endl;
       outs << "Ages: " << getAges() << endl;
       outs << "Department: " << getDepartment() << endl;
       outs << "Managered Number: " << getManageredNum() << endl;
       outs << "--------------------------------" << endl;
       return outs;
   }
   virtual string getPosition();
};

//it has various useful funtions

class functions
{
public:
   void showMenu();
   void readfromfile(char* url) throw (FileIOException);
   bool EditEmployee();
   void searchEmployee();
   bool addEmployee();
   void printTofile();
};

template<typename T>
class Company {
public:
   vector<T> list;
   void add(T o);

   int search(string name);

   void print();
  
   void Sort();

  

};

class FileIOException
{
     
};

__________

functions.h:

#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
Company<Worker> worker;
Company<Manager> managers;

void functions::showMenu() {
   cout << "-------------------------------------------" << endl;
   cout << " Welcome to the employee system" << endl;
   cout << " You can hire 5 worker and 5 manager, 10 total employee" << endl;
   cout << " 1. Read from file" << endl;
   cout << " 2. Show all Manager" << endl;
   cout << " 3. Show all Worker" << endl;
   cout << " 4. Edit employee's information" << endl;
   cout << " 5. Search employee " << endl;
   cout << " 6. Add employee" << endl;
   cout << " 7. release employee" << endl;
   cout << " 8. sort employee by ages" << endl;
   cout << " 9. prnit to file and exit" << endl;
   cout << "-------------------------------------------" << endl;
}

//a file url is passed as parameter

//it reads a file and creates a manager or worker instance based on data
void functions::readfromfile(char* url) throw (FileIOException)
{
   char buffer[50];
   string name, tel, address, sex, department, position, manager;
   int years, num;
   int i = 1;
   ifstream infile(url);
   if (infile.fail())
   {
       throw 0;
   }
   while (!infile.eof())
   {

       infile.getline(buffer, 50);
       if (i == 1) {
           name = "";
           for (int j = 0; j<strlen(buffer); j++) name += buffer[j];
           i++;
       }
       else if (i == 2) {
           tel = "";
           for (int j = 0; j<strlen(buffer); j++) tel += buffer[j];
           i++;
       }
       else if (i == 3) {
           address = "";
           for (int j = 0; j<strlen(buffer); j++) address += buffer[j];
           i++;
       }
       else if (i == 4) {
           sex = "";
           for (int j = 0; j<strlen(buffer); j++) sex += buffer[j];
           i++;
       }
       else if (i == 5) {
           years = atoi(buffer);
           i++;
       }
       else if (i == 6) {
           department = "";
           for (int j = 0; j<strlen(buffer); j++) department += buffer[j];
           i++;
       }
       else if (i == 7) {
           position = "";
           for (int j = 0; j<strlen(buffer); j++) position += buffer[j];
           i++;
       }
       else {
           if (position == "Worker"||"worker") {
               Worker emp;
               emp.setName(name);
               emp.setTel(tel);
               emp.setAddress(address);
               emp.setSex(sex);
               emp.setAges(years);
               emp.setDepartment(department);
               manager = "";
               for (int j = 0; j<strlen(buffer); j++) manager += buffer[j];
               emp.setManager(manager);
               worker.add(emp);
           }
           else {
               Manager ma;
               ma.setName(name);
               ma.setTel(tel);
               ma.setAddress(address);
               ma.setSex(sex);
               ma.setAges(years);
               ma.setDepartment(department);
               num = atoi(buffer);
               ma.setManageredNum(num);
               managers.add(ma);
           }
           i = 1;
       }
   }
}

//asks for name

//then allows to edit the employee info

//return true if edited else return false
bool functions::EditEmployee() {
   string name;
   int index;
   cout << "Input name:";
   cin.get();
   getline(cin, name);
   index = managers.search(name);
   if (index != -1) {

       string tel, address, sex, department;
       int manageredNum, ages;
       cout << "Input Tel:";
       cin >> tel;
       managers.list[index].setTel(tel);
       cin.get();
       cout << "Input Address:";
       address = " ";
       getline(cin, address);
       managers.list[index].setAddress(address);
       cout << "Input Sex:";
       cin >> sex;
       managers.list[index].setSex(sex);
       cout << "Input Ages:";
       cin >> ages;
       managers.list[index].setAges(ages);
       cout << "Input Department:";
       cin >> department;
       managers.list[index].setDepartment(department);
       cout << "Input ManageredNum";
       cin >> manageredNum;
       managers.list[index].setManageredNum(manageredNum);
       return true;
   }
   else {
       index = worker.search(name);
       if (index != -1) {
           string tel, address, sex, department, manager;
           int ages;
           cout << "Input Tel:";
           cin >> tel;
           worker.list[index].setTel(tel);
           cout << "Input Address:";
           cin.get();
           getline(cin, address);
           worker.list[index].setAddress(address);
           cout << "Input Sex:";
           cin >> sex;
           worker.list[index].setSex(sex);
           cout << "Input ages:";
           cin >> ages;
           worker.list[index].setAges(ages);
           cout << "Input Department:";
           cin >> department;
           worker.list[index].setDepartment(department);
           cout << "Input Managere";
           cin >> manager;
           worker.list[index].setManager(manager);
           return true;
       }
       else return false;
   }
   return false;
}

//searches employee based on name
void functions::searchEmployee() {
   string name;
   int index;
   cout << "Input name:";
   cin.get();
   getline(cin, name);
   index = managers.search(name);
   if (index != -1)
   {
       managers.list[index] << cout;
   }
   else
   {
       index = worker.search(name);
       if (index != -1)
       {
           worker.list[index] << cout;
       }
       else cout << "The employee you search is not on database" << endl;
   }
}

//asks user various data for empployee

//creates instance of manage or worker based on position

//adds to workers or managers appropriately
bool functions::addEmployee() {
   string name, tel, address, sex, position, department;
   int ages;
   cout << "Input name:";
   cin.get();
   getline(cin, name);
   if (worker.search(name) != -1 || managers.search(name) != -1) {
       return false;
   }
   cout << "Input Tel:";
   cin >> tel;
   cout << "Input Address:";
   cin.get();
   getline(cin, address);
   cout << "Input Sex:";
   cin >> sex;
   cout << "Input Ages:";
   cin >> ages;
   cout << "Input Department:";
   cin >> department;
   cout << "Input position:";
   cin >> position;
   if (position == "Manager") {
       int num;
       cout << "Input ManageredNum:";
       cin >> num;
       Manager mana;
       mana.setName(name);
       mana.setTel(tel);
       mana.setAddress(address);
       mana.setSex(sex);
       mana.setAges(ages);
       mana.setDepartment(department);
       mana.setManageredNum(num);
       if (managers.list.size() >= 5) {
           cout << "Manager list is full" << endl;
           return false;
       }
       else managers.add(mana);
   }
   else {
       string manager;
       cout << "Input Manager:";
       cin >> manager;
       Worker wo;
       wo.setName(name);
       wo.setTel(tel);
       wo.setAddress(address);
       wo.setSex(sex);
       wo.setAges(ages);
       wo.setDepartment(department);
       wo.setManager(manager);
       if (worker.list.size() >= 5) {
           cout << "Worker list is full!" << endl;
           return false;
       }
       else worker.add(wo);
   }
   return true;
}

//print all info of managers and workres to "database.txt"
void functions::printTofile() {
   int i;
   ofstream outfile("database.txt", ofstream::out);
   for (i = 0; i<managers.list.size(); i++) {
       outfile << managers.list[i].getName() << endl << managers.list[i].getTel() << endl << managers.list[i].getAddress() << endl
           << managers.list[i].getSex() << endl << managers.list[i].getAges() << endl << managers.list[i].getDepartment() << endl << managers.list[i].getPosition() << endl
           << managers.list[i].getManageredNum() << endl;
   }
   for (i = 0; i<worker.list.size(); i++) {
       outfile << worker.list[i].getName() << endl << worker.list[i].getTel() << endl << worker.list[i].getAddress() << endl
           << worker.list[i].getSex() << endl << worker.list[i].getAges() << endl << worker.list[i].getDepartment() << endl << worker.list[i].getPosition() << endl
           << worker.list[i].getManager() << endl;
   }
   outfile.close();
}

//adds the object of type T to vector list

template<typename T>
void Company<T>::add(T o)
{
   list.push_back(o);
}

//saerches for object of type T in vector list

template<typename T>
int Company<T>::search(string name)
{
  
   for (int i = 0; i<list.size(); i++) {
       if (list[i].getName() == name) {
           return i;
           break;
       }
   }
   return -1;
}

//prints objects in list.

template<typename T>
void Company<T>::print()
{
   for (int i = 0; i<list.size(); i++)
   {
       list[i] << cout;
   }
}

//sorts employees according to age.
template<typename T>
void Company<T>::Sort()
{
   sort(list.begin(), list.end());
   cout << "sorted by ages successfully" << endl;
}