程序使用数组中的字符串给出错误

时间:2018-03-09 16:16:40

标签: c++

我正在创建一个带有数组的员工结构。但是,它会一直返回此错误。 该程序应该允许用户输入身份证号码并进行验证,这已经有效。然后它应该允许用户在数组内搜索一个值。

#include <iostream>
#include <string.h>
using namespace std;
struct Employee
{
    int idNum;
    double payRate;
    string 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;
        int tempId;
        cin >> tempId;
        for (int i = 0; i < SIZE; ++i)
        {
            if (employee[i].idNum == tempId)
                error = 1;
        }
        while(error == 1)
        {
            cout << "Invalid entry. Please enter a new id number " << endl;
            cin >> employee[count].idNum;
            for(int i = 0; i < count; ++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;
    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 pay rate " << 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;
        }
    }
}

我得到了

76:41: error: cannot convert 'std::string {aka 
std::basic_string<char>}' to 'const char*' for argument '1' to 'int 
strcmp(const char*, const char*)'

如何修复此错误?谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

您可以将两个字符串与==进行比较,因此我应该if(employee[count].lastName==name)代替if(strcmp(employee[count].lastName, name) == 0)

但如果你想用strcmp试试

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