C ++数组对象检查值

时间:2018-07-04 18:43:21

标签: c++ arrays

我应该写一个具有两个类的程序,EmployeeDepartment。当main()函数运行时,它要求用户选择显示的六个编号选项之一,并为EmployeeDepartment对象创建数组。我不理会选项1(创建部门选项)以外的所有其他选项,因此,此问题的重点将是Department类和Department department[3]数组。

while函数中有一个main()循环不断运行,直到用户决定退出为止。如果用户输入选项1,则将创建一个Department数组对象,然后用户还为该对象输入departmentIDdepartmentNamedepartmentHeadNamewhile循环会通知用户数组是否有三个Employee对象。但是,我遇到了困难,因为每个departmentID必须是唯一的。例如,我不能为第一个数组对象的departmentID输入1,然后再为第二个数组对象的departmentID输入1。如何检查用户的departmentID输入是否已存在于先前的对象中?

#include <iostream>
#include <string>
using namespace std;

// Employee Class
class Employee
{
private:
    string employeeID;
    string employeeName;
    string employeeDepartmentID;
    double employeeSalary;
    int employeeAge;

public:
    void createEmployee()
    {
        cout << "Please Enter Employee Details:" << endl;

        cout << "Employee ID : ";
        cin >> employeeID;

        cout << "Employee Name :";
        cin >> employeeName;

        cout << "Salary: $";
        cin >> employeeSalary;

        cout << "Age : ";
        cin >> employeeAge;

        cout << "Department ID : ";
        cin >> employeeDepartmentID;
    }
};

// Department Class
class Department
{
private:
    string departmentID;
    string departmentName;
    string departmentHeadName;

public:
    void createDepartment()
    {
        cout << "Please Enter Department Details: \n";

        cout << "Department ID : ";
        cin >> departmentID;

        cout << "Department Name : ";
        cin >> departmentName;

        cout << "Head of Department : ";
        cin >> departmentHeadName;
    }
};

// Function prototype
void displayMenu(); 

// Client main function
int main()
{
    Employee employee[5];
    Department department[3];

    int choice;
    int departmentCount = 0;
    int employeeCount = 0;

    while (true)
    {
        displayMenu();
        cin >> choice;

        if (choice == 1 && departmentCount < 3)
        {
            department[departmentCount].createDepartment();
            departmentCount = departmentCount + 1;
        }
        else if (choice == 1 && departmentCount >= 3)
        {
            cout << "\nThe array is full, you can not add any more Departments." << endl;
        }
        else if (choice == 2 && employeeCount < 5)
        {
            employee[employeeCount].createEmployee();
            employeeCount = employeeCount + 1;
        }
        else if (choice == 2 && employeeCount >= 5)
        {
            cout << "The array is full, you can not add any more Employees." << endl;
        }
        else if (choice == 6)
        {
            cout << "Thank you, goodbye." << endl;
            break;
        }
    }
    return 0;
}

// Display menu function
void displayMenu()
{
    cout << "1. Create Department" << endl;
    cout << "2. Create Employee" << endl;
    cout << "3. Write Out Data File" << endl;
    cout << "4. Read In Data File" << endl;
    cout << "5. Display Salary Report" << endl;
    cout << "6. -- Quit -- " << endl;
    cout << "Please make a selection : ";
}

2 个答案:

答案 0 :(得分:0)

尝试这个

#include <iostream>
#include <string>
using namespace std;

// Employee Class
class Employee
{
private:
    string employeeID;
    string employeeName;
    string employeeDepartmentID;
    double employeeSalary;
    int employeeAge;

public:
    void createEmployee()
    {
        cout << "Please Enter Employee Details:" << endl;

        cout << "Employee ID : ";
        cin >> employeeID;

        cout << "Employee Name :";
        cin >> employeeName;

        cout << "Salary: $";
        cin >> employeeSalary;

        cout << "Age : ";
        cin >> employeeAge;

        cout << "Department ID : ";
        cin >> employeeDepartmentID;
    }


};

// Department Class
class Department
{
private:
    string departmentID;
    string departmentName;
    string departmentHeadName;

public:
    void createDepartment()
    {
        cout << "Please Enter Department Details: \n";

        cout << "Department ID : ";
        cin >> departmentID;

        cout << "Department Name : ";
        cin >> departmentName;

        cout << "Head of Department : ";
        cin >> departmentHeadName;
    }

      public:
        string getDepartmentID(){

            return departmentID;

          }
};


// Function prototype
void displayMenu(); 

// Client main function
int main()
{
    Employee employee[5];
    Department department[3];


    int choice;
    int departmentCount = 0;
    int employeeCount = 0;

    while (true)
    {
        displayMenu();
        cin >> choice;

        if (choice == 1 && departmentCount < 3)
        {
            department[departmentCount].createDepartment();

            for(int i=0;i<departmentCount;i++)
           {
                if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
                 {

                    cout<<"already exists......................... \n";
                 }

            }


            departmentCount = departmentCount + 1;
        }
        else if (choice == 1 && departmentCount >= 3)
        {
            cout << "\nThe array is full, you can not add any more Departments." << endl;
        }
        else if (choice == 2 && employeeCount < 5)
        {
            employee[employeeCount].createEmployee();
            employeeCount = employeeCount + 1;
        }
        else if (choice == 2 && employeeCount >= 5)
        {
            cout << "The array is full, you can not add any more Employees." << endl;
        }
        else if (choice == 6)
        {
            cout << "Thank you, goodbye." << endl;
            break;
        }
    }
    return 0;
}

// Display menu function
void displayMenu()
{
    cout << "1. Create Department" << endl;
    cout << "2. Create Employee" << endl;
    cout << "3. Write Out Data File" << endl;
    cout << "4. Read In Data File" << endl;
    cout << "5. Display Salary Report" << endl;
    cout << "6. -- Quit -- " << endl;
    cout << "Please make a selection : ";
}

答案 1 :(得分:0)

我在部门类中使用getDepartmentID函数从每个部门对象获取部门ID

public:
        string getDepartmentID(){

            return departmentID;

          }

应该将返回类型为string。因为您已将DepartmentID创建为字符串。

并且我在主要功能中使用For Loop来比较相关部门ID是否存在

for(int i=0;i<departmentCount;i++)
           {
                if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
                 {

                    cout<<"already exists......................... \n";
                 }

            }