我的员工班级有问题。该程序本身有一个样本雇员,并允许用户向该程序输入更多雇员。我遇到的问题是我可以添加一个新员工并打印样本员工和新添加的员工的列表,但是当我尝试添加第二个员工并打印时,第二个覆盖了第一个员工增加了员工。
我不知道问题出在使用im数组(有4个元素,数字,名字,姓氏和部门)还是在调用代码下面的函数ys。
头文件:
#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文件:
#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()
{
}
主要:
#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 ;
}
(很抱歉发布所有代码,不想遗漏重要的内容)
香港专业教育学院尝试了几种不同的方法来打印第二个(和随后的雇员),但无论我尝试改变数组或函数的哪种方式,我都会出错并且代码无法编译。
我想添加新员工时要使用
empnew[j]= new employee (num ,fname,lname,dept);
但这给了我“错误:没有运算符” =“匹配这些操作数”
以及许多其他无效的变体。
正如您可能会猜到的,我只是从C ++开始的,所以可以极大地帮助您。
最佳答案
您将员工数据设置为empnew[j]
数组项,但是j
变量永不更改。从以下代码段来看:
empnew[j].Set(num, fname, lname, dept);
empcount++;
可能您应该更改为
empnew[empcount++].Set(num, fname, lname, dept);