Search code examples
c++arraysifstream

array and stucture with textfile


try to put a text file in an array with a structure Students (FirstName) but it put

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Students' (or there is no acceptable conversion)))

Students stud[15];
ifstream myfile;
myfile.open("student.txt");
for (int i = 0; i<14; i++)
{
    myfile >> stud[i];
    cout << stud[i] << endl;
}
myfile.close();

Solution

  • In the Students class constructor it looks like you are making a variable to hold their first name, so when adding it to the text file you would have to call on that variable, not the entire Student object.

    myFile >> stud[i].firstName;
    

    Where firstName is the name of whatever variable holds FirstName from the Student constructor.