Home > Software engineering >  Im trying to assign value to a array of char using strcpy(), but it gives a error that cannot conver
Im trying to assign value to a array of char using strcpy(), but it gives a error that cannot conver

Time:02-03

I'm working on a assignment and I can't seem to figure out the reason of the error. The strcpy() function was working when I tried on the University's PC, now I'm trying to do it at home and its not working properly.

#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>

class Employee{
    int E_Id;
    char*E_Name[30];
    int No_Hours;
    int Rate_Hour;
    public:
        void SetData(int Id, char*Name[30], int Hours, int Rate)
        {
            E_Id = Id;
            strcpy(E_Name,Name); //Error Here
            No_Hours = Hours;
            Rate_Hour = Rate;
        }
        void DispData()
        {
            cout<<"Employee ID: "<<E_Id<<endl;
            cout<<"Employee Name: "<<E_Name<<endl;
            cout<<"Number of Hours: "<<No_Hours<<endl;
            cout<<"Rate per Hour: "<<Rate_Hour<<endl;
        }
        void InputData()
        {
            cout<<"Give Employee ID: ";
            cin>>E_Id;
            cout<<"Give Employee Name: ";
            cin>>E_Name;
            cout<<"Give Number of Hours: ";
            cin>>No_Hours;
            cout<<"Give Rate per Hour: ";
            cin>>Rate_Hour;
        }
        int GetEId()
        {
            return E_Id;
        }
        char*GetEName()
        {
            return E_Name;
        }
        int GetNoHours()
        {
            return No_Hours;
        }
        int GetRateHour()
        {
            return Rate_Hour;
        }
        Employee()
        {
            PId = 0;
            strcpy(E_Name, "")
            No_Hours = 0;
            Rate_Hour = 0;
        }
        Employee(int Id, char*Name, int Hours, int Rate)
        {
            E_Id = Id;
            strcpy(E_Name, Name); //Error Here
            No_Hours = Hours;
            Rate_Hour = Rate;
        }
        ~Employee()
        {
            cout<<"Obeject Destroyed"<<endl;
        }
    
};
int main()
{
    Employee*e;
    e = new Employee[10];
    int i;
    cout<<"Give Data"<<endl;
    for(i=0;i<10;i  )
    {
        (e i)->InputData();
    }
    int high = (e 0)->GetNoHours()*(e 0)->GetRateHours();
    int loc = 0;
    for(i=0;i<10;i  )
    {
        if((e i)->GetNoHours()*(e i)->GetRateHours()>high)
        {
            high = (e i)->GetNoHours()*(e i)->GetRateHours();
            loc = i;
        }
    }
    cout<<"Employee with Highest Salary"<<endl;
    (e loc)->DispData();
    delete[]e;
    getch();
    return 0;
}

In this program have to use pointers to make an array of 10 employees and tell which employee earns the most salary.

CodePudding user response:

This is wrong

char*E_Name[30]; // array of char pointers

it should be

char E_Name[30]; // array of chars

An array of chars can hold a string. An array of char pointers is something else.

This is wrong

void SetData(int Id, char*Name[30], int Hours, int Rate)

it should be

void SetData(int Id, char*Name, int Hours, int Rate)

Since you cannot have an array as a parameter to a function you use a pointer instead. So if you want to pass an array of char to a function, the function should be declared with a pointer to char.

Basically you should be using either char arrays or char pointers, but not both combined.

  •  Tags:  
  • c
  • Related