Tag Archives: bugerror: invalid array assignment

[How to Solve]error: invalid array assignment

Error code:

struct STUD
{
    int ID;
    char name[20];
    float score;
}stud;
STUD SS[10];
student.open("student.dat",ios::in|ios::binary);
    if(!student)
    {
        cout<<"failed to open file"<<endl;
        return 0;
    }
    int j=0;
    student.read((char *)&stud,sizeof(stud));
    while(!student.eof()){
        SS[j].name=stud.name;//error
        SS[j].ID=stud.ID;
        SS[j].score=stud.score;
        j++;
        student.read((char *)&stud,sizeof(stud));
    }
    student.close();

The purpose of this code is to read the data from the file. DAT and store it in the structure array object SS [J]. However, the assignment of SS [J]. Name is an error, which can be changed to strcpy (SS [J]. Name, student. Name). Here, if you use the equal sign, you need to overload the operator =. Pay attention to add the header file # include <cstring>