Im having a issue with my employee class. The program itself has a sample employee and allows the user to input more employees to the program. The issue I am having is that I can add in 1 new employee and print the list of both the sample employee and the newly added employee, however when i try to add in a second employee and print, the second one with over write the first added employee.
Im not sure if the issue is with the array im using (which has 4 elements, number, first name, last name and department) or if its with the function thats being called, below the code ive done up.
header file:
#include <iostream>
using namespace std;
class employee
{
public :
employee();
employee(int, char*, char*, char*); // employee works number, name, department
void Set( int, char*, char*, char*); // set the number, name and dept
void Print();
void printmenu();
~employee(); //destructor
private:
int e_num; // employee number
char e_fname[30];
char e_lname [30];
char e_dept[30];
};
CPP File:
#include <iostream>
#include "employee.h"
using namespace std;
employee::employee()
{
}
employee::employee(int num, char* fname, char* lname, char* dept)
{
Set(num, fname, lname, dept);
}
void employee::Set( int num, char* fname, char* lname, char* dept)
{
if (num < 0 )
{
return ; // add in code here to give error message if works is less than 0
}
e_num = num;
strcpy (e_fname, fname);
strcpy (e_lname, lname);
strcpy (e_dept, dept);
}
void employee::Print()
{
cout << e_num <<" \t " << e_fname <<" "<< e_lname <<" \t " << e_dept << " \n";
}
void employee::printmenu()
{
cout << "EMPLOYEE MENU\n"
<< "~~~~~~~~~~~~~\n"
<< "1. Add New Employee\n"
<< "2. Edit Employee\n"
<< "3. Delete Employee\n"
<< "4. Print Employee List\n"
<< "5. Exit\n";
}
employee::~employee()
{
}
Main:
#include <iostream>
#include "employee.h"
using namespace std;
void main (void)
{
char input;
bool done = false;
employee emp1(1, "Joe", "Bloggs", "Customer Service");// sample employee
int empcount = 1;
employee empnew[20];
int num, j=0, k=0; // num is employee number
char* fname = new char [20]; // employee first name
char* lname = new char [20]; // employee last name
char* dept = new char [30]; // employee department
while(!done)
{
employee pmenu;
pmenu.printmenu();
cout <<"Please make your selection:";
cin >> input;
switch (input)
{
case '1': // add new employee
cout<<"\nEnter Employee Number:";
cin >> num;
cout<<"\nEnter first name:";
cin >> fname;
cout<<"\nEnter Last name:";
cin >> lname;
cout << "\nEnter Department:";
cin >> dept;
cout << "\n";
empnew[j].Set(num, fname, lname, dept);
empcount++;
cout << "New employee added:"<< fname <<" "<<lname << "\n";
cout<< "New Number of Employees:" << empcount<< "\n\n";
break;
case '2': //Edit Employee
// enter stuff here
break;
case '3': //Delete Employee
// enter stuff here
break;
case '4': // print employee list
cout <<"Total number of Employees:"<<empcount<<endl;
cout <<"Number \t Employee Name \t Department \n" ;
if (empcount == 1)
{
emp1.Print();
}
else
{
emp1.Print();
empnew[j].Print();//print input
}
cout <<"\n";
break;
case '5':
cout << "Program closing\n";
done = true;
break;
}
}
getchar();
return ;
}
(sorry for posting all the code, didnt want to leave something important out)
Ive tried several different ways to get the 2nd (and subsequent employee) to print but which ever way i try to change the array or the function i get errors and the code wont compile.
I thought when added the new employee to use
empnew[j]= new employee (num ,fname,lname,dept);
but that gives me “Error: no operator “=” matches these operands”
along with many other variations of that which dont work either.
As you might guess ive just started with c++ so any help would be greatly appricated.
You set employee data into
empnew[j]array item, yetjvariable never changes. Judging from this snippet:probably you should change into to