0

我的员工班有问题。该程序本身有一个示例员工,并允许用户向该程序输入更多员工。我遇到的问题是我可以添加 1 名新员工并打印示例员工和新添加的员工的列表,但是当我尝试添加第二名员工并打印时,第二个会覆盖第一个添加了员工。

我不确定问题是否与我使用的数组(它有 4 个元素,数字,名字,姓氏和部门)有关,或者它是否与正在调用的函数有关,在我完成的代码下方。

头文件:

#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++,所以任何帮助都会非常有用。

4

2 回答 2

2

您将员工数据设置为empnew[j]数组项,但j变量永远不会改变。从这个片段来看:

empnew[j].Set(num, fname, lname, dept);
empcount++;

也许你应该变成

empnew[empcount++].Set(num, fname, lname, dept);
于 2012-11-08T11:32:23.023 回答
1

new运算符返回一个指针,并且您有一个值数组。

您必须创建一个指针数组,或者(我建议)实现一个复制构造函数并使用复制:

empnew[j] = employee (num ,fname, lname, dept);
于 2012-11-08T11:30:49.750 回答