views:

80

answers:

4

I am attempting to complete a homework assignment and would like to make an array of data structure and initialise it with names. However, when I try to compile my code I get the following error:

**error C2440: '=' : cannot convert from 'const char [13]' to 'const char'**

I am including my code below. Please advise me what I might be doing wrong. Many thanks for your help.

#define STD_HOUR 40.0
#define OT_CONSTANT 1.5
#define SIZE 5

static const int ietrations = 5;                                //For Number of Records
void Init(void);                                                //Function Prototype to Initilize
void Calculate(int);                                            //Function Prototype to Calculate Wagets etc
void Display(void);                                             //Function Prototype to Display Data
void GetHours(void);                                            //Function Prototype to Get Hours

/////////////////////////////////////// Employee Structure ////////////////////////////////////////////////////
struct employee
{
    const char name[20]; 
    double clock;
    double wage;
    double hours_worked;
    double over_time_hours;
    double gross;
};
/////////////////////////////////////// Employee Structure Based Array ////////////////////////////////////////
employee data[SIZE];


using namespace std;

int main()
{   
    Init();                     //Call Initlize function
    GetHours();                 //Input Hours
    Display();                  //Display the Results
    getch();                    //Hold screen
return 0;

}//Main Ends here

void Init()
{
    data[0].name ="Connie Cobol";
    data[1].name ="Mary Apl";
    data[2].name ="Frank Fortran";
    data[3].name ="Jeff Ada";
    data[4].name = "Anton Pascal";

    //data[].name = {"Connie Cobol","Mary Apl","Frank Fortran","Jeff Ada","Anton Pascal"};

    data[0].clock =98401;
    data[1].clock =526488;
    data[2].clock =765349;
    data[3].clock =34645;
    data[4].clock =127615;

    data[0].wage = 10.60;
    data[1].wage = 9.75;
    data[2].wage = 10.50;
    data[3].wage = 12.25;
    data[4].wage = 8.35;
}
+3  A: 

You cannot assign character arrays like that, i.e. you cannot do:

char name[20];
name = "Ronald";

Assuming you're in C, you can do the following:

const char *name;
name = "Ronald";

(name will be a pointer to a string literal now), or:

char name[20];
strcpy(name, "Ronald");

(name is an array containing the null-terminated string "Ronald"). This second method is less safe, because there's nothing to prevent you copying a string longer than 19 into the array.

The safest method, assuming C++, is:

#include <string>
...
std::string name;
name = "Ronald";

All of the above applies equally well to standalone variables, or to structure member variables.

Oli Charlesworth
What if I intend to do it in c not c++? and without pointers
Afnan
You can't really manipulate strings without pointers.
Oli Charlesworth
+1  A: 

You cannot assign one array to another like that, you have to copy the array contents manually. I see you have namespace std in scope - any reason not to use std::string instead of the char array? if you do that the assignment of literals to .name will work.

std::string name = "My Name";
Steve Townsend
+1  A: 
data[0].name ="Connie Cobol";
data[1].name ="Mary Apl";
data[2].name ="Frank Fortran";
data[3].name ="Jeff Ada";
data[4].name = "Anton Pascal";

You just can't do this. You can initialize a char array by a string literal, but not assign to it. Use strcpy from <cstring> or use std::strings

Armen Tsirunyan
+1  A: 

Since you're using C++, change the type of employee::name from const char [20] to std::string. Then you'll be able to do the assignments as written.

Neither C nor C++ allow you to assign array contents using the = operator; array semantics are a little non-intuitive in both languages.

John Bode