程序将无法使用具有结构对象的数组进行编译

时间:2018-03-10 00:28:37

标签: c++

这个程序应该允许我输入一个字符串,并允许我按名称,payrate或id搜索员工。但是,由于strlen()函数错误,它不会编译。

我得到的错误是:

  

main.cpp:88:39:错误:无法将参数'1'的'double'转换为'const char *'为'int strcmp(const char *,const char *)'

如何解决此错误?

#include <iostream>

using namespace std;
struct Employee
{
    int idNum;
    double payRate;
    char firstName, lastName;
};

int main()
{
    int error;
    const int SIZE = 5;
    Employee employee[SIZE];
    for (int k = 0; k < SIZE; ++k)
    {
        employee[k].idNum = 0;
        employee[k].payRate = 0;
    }
    for (int count = 0; count < SIZE; ++count)
    {
        error = 0;
        cout << "Enter the employee's id number " << endl;
        cin >> employee[count].idNum;
        for (int i = 0; i < SIZE; ++i)
        {
            if (employee[i].idNum == employee[count].idNum)
                error = 1;
        }
        while (error == 1)
        {
            cout << "Invalid entry. Please enter a new id number " << endl;
            cin >> employee[count].idNum;
            for (int i = 0; i < SIZE; ++i)
            {
                error = 0;
                if (employee[i].idNum == employee[count].idNum)
                    error = 1;

            }
        }
        cout << "Enter the employee's pay rate " << endl;
        cin >> employee[count].payRate;
        cout << "Enter the employee's first name " << endl;
        cin >> employee[count].firstName;
        cout << "Enter the employee's last name " << endl;
        cin >> employee[count].lastName;
        int choice;
        cout << "Enter 1 to search for an employee by id number, enter 2 to                 search by last name, and enter 3 to search by pay "
             << endl;
        cin >> choice;

    }
    int choice;
    cout << "Enter 1 to search for an employee by id number, enter 2 to    search by last name, and enter 3 to search by pay "
         << endl;
    cin >> choice;
    if (choice == 1)
    {
        int idNumC;
        cout << "Enter an id number ";
        cin >> idNumC;
        for (int count = 0; count < SIZE; ++count)
        {
            if (employee[count].idNum == idNumC)
                cout << employee[count].idNum;
        }
    }
    if (choice == 2)
    {
        string name;
        cout << "Enter the employee's last name " << endl;
        cin >> name;
        for (int count = 0; count < SIZE; ++count)
        {
            if (strcmp(employee[count].lastName, name) == 0)
                cout << "ID number: " << employee[count].idNum
                        << " First name: " << employee[count].firstName
                        << " Last Name: " << employee[count].lastName
                        << " Hourly Pay: " << endl;
        }
    }
    if (choice == 3)
    {
        int nam;
        cout << "Enter the employee's last name " << endl;
        cin >> nam;
        for (int count = 0; count < SIZE; ++count)
        {
            if (employee[count].payRate == nam)
                cout << "ID number: " << employee[count].idNum
                        << " First name: " << employee[count].firstName
                        << " Last Name: " << employee[count].lastName
                        << " Hourly Pay: " << endl;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

employee[count].lastName是一个char,而不是一个c-string(空终止的char数组)。此外,namestd::string,而不是c字符串。

它们必须是要比较的相同类型,因此选择一种类型。如果您想lastName std::string,那么您可以

if( employee[count].lastName == name )

否则,如果您将lastName设为c字符串,则还需要设置name一个字符串,以便执行以下操作:

if (strcmp(employee[count].lastName, name.c_str()) == 0)