c++ - Reading from file to different class objects -


i'm beginner in c++ coding , have question regarding reading lines file specific class objects, 1 of array.

my class object:

const int size_of = 5;  class student { public:     student();     student(const student &);     student(string, int, int, int, int, int);     friend std::istream& operator >> (std::istream& in, student& s);     void display(); private:    string lastname;    int grades[size_of]; }; 

cpp file define functions:

student::student() {     int i;     string lastname = "default";     (i = 0; < 5; i++)     {         grades[i] = 0;     } }  student::student(const student & s) {     int i;     lastname = s.lastname;     (i = 0; < 5; i++)     {         grades[i] = s.grades[i];     } }  student::student(string s, int a, int b, int c, int d, int e) {     lastname = s;     grades[0] = a;     grades[1] = b;     grades[2] = c;     grades[3] = d;     grades[4] = e; }  std::istream& operator >> (std::istream& in, student& s) {     std::string line;     std::getline(in, line);     in >> s.lastname >> s.grades[0] >> s.grades[1] >> s.grades[2] >> s.grades[3] >> s.grades[4];     getline(in, s.lastname);     return in; }  void student::display() {     int i;     int sum = 0;     double average;     cout << "last name: " << lastname << endl;     cout << "grades: " << endl;     (i = 0; < 5; i++)     {         cout << grades[i] << endl;     }     (i = 0; < 5; i++)     {         sum = sum + grades[i];     }     average = sum / 5;     cout << "average: " << average;     } 

what need read file , save name under lastname object of student class. need save each of grades in array object. need have new student class object next name , repeat process 4 names. file i'm reading from:

george 75,85,95,100,44 peter 100,100,100,100,100 frank 44,55,66,77,88 alfred 99,88,77,66,55 

your following function seems wrong:

std::istream& operator >> (std::istream& in, student& s) {     std::string line;     std::getline(in, line);     in >> s.lastname >> s.grades[0] >> s.grades[1] >> s.grades[2] >> s.grades[3] >> s.grades[4];     getline(in, s.lastname);     return in; } 

you read lastname twice, not ignore ',' character... try this:

std::istream& operator >> (std::istream& in, student& s) {     char dummy;     in >> s.lastname >> s.grades[0]         >> dummy >> s.grades[1]         >> dummy >> s.grades[2]         >> dummy >> s.grades[3]         >> dummy >> s.grades[4];      return in; } 

update: if consider spaces inside lastname, should have change first line use std::getline follows:

std::istream& operator >> (std::istream& in, student& s) {     char dummy;     std::getline(in, s.lastname)      in >> s.grades[0]         >> dummy >> s.grades[1]         >> dummy >> s.grades[2]         >> dummy >> s.grades[3]         >> dummy >> s.grades[4];     in.ignore(); // remove last '\n'     return in; } 

then:

int main() {     student s;     std::vector<student> all;      while (cin>>s)       all.push_back(s); } 

...and...enough updates!


Comments