0

I have a question for the code here , I am facing an error in this simple code with one struct , if you compile it then the compiler states in the error statement that " expression must be a modifiable lvalue " . What I basically want in this code is to assign for example a name with having an array of struct . So when there gets written x[1].identification = "Id"; , then the compiler gives the error . I am quite stuck in this problem for some time .

Can anybody give a solution to this ?!

Thank you


Here is the code :

#include "stdafx.h"
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<windows.h>
//#include <ctime>
//#include <dos.h>
#include<dos.h>
#include<conio.h>
#include<cstdio>
#define max 20
using namespace std;


struct person
{

char identification[20];
long int code;
char group [20];
int experience;
int age;

};


int main() 
{

person x[10];

x[1].identification = "Id";  // this is where the error is being shown

system("cls");
return 0;

}
ekcs
  • 21
  • 3
  • It try to assign an address of a const char pointer to constant address. `x[1].identification` is an array not a pointer so you can't put the address in it. if you must use a c string, use `strcpy` to copy it. – SHR Jan 11 '15 at 00:10

1 Answers1

1

You are trying to assign a const char* to a char array. That makes no sense. Use std::string instead:

struct person {
    std::string identification;
    long int code;
    std::string group;
    int experience;
    int age;
};

You might also want to create a constructor, otherwise code, experience and age are undefined. You should probably require them at construction.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • This does not solve the OP actual problem, even if it's a possible workaround... – nbro Jan 11 '15 at 00:03
  • 1
    @Rinzler How doesn't it solve OP's problem exactly? – Shoe Jan 11 '15 at 00:05
  • 1
    The original error and problem is: `expression must be a modifiable lvalue` and not how to pass from a char array to an `std::string` – nbro Jan 11 '15 at 00:07
  • @Rinzler Using an `std::string` solves the problem. End of. – juanchopanza Jan 11 '15 at 00:09
  • 1
    @juanchopanza Nice for you ;) – nbro Jan 11 '15 at 00:12
  • @Rinzler So, yeah, considering that he can't use an array there, are you saying that all solutions involving him not using a `char` array for `identifications` do "not solve OP's problem"? Because in that case I'm afraid there are none. – Shoe Jan 11 '15 at 00:17