I'm using cin to take in a string of "name,surname,student number,marks" all seperated by commas only. Note the marks can be any number of marks as a percentage seperated by whitespace i.e '10 20 30 40'. I store each value into a a vector using push_back(). I use .back() to look at the value at the end of the vector and set it as 'mark' in my struct StudentRecords. I then use pop_back() to remove the mark value from the vector and repeat the process for studentNumber,surname and name.
My problem is that for some reason when I print out student information (name,surname,student number,marks) where I have more than one mark, it only prints out the first mark. If I have two surnames seperated by ' ' my program crashes saying >>> Makefile:16: recipe for target 'run' failed make: *** [run] Segmentation fault (core dumped).
I've looked around and all I seem to find is that I'm supposed to use cin.ignore()/cin.ignore(256, '\n') before getline but that doesn't seem to be working.
Example, given: 'Thomas,Anderson,ANDTHO111,10 20 30' Output will be: 'Thomas,Anderson,ANDTHO111,10'
cout << "enter student data (name,surname,student number,marks) seperated only by ',' " << endl;
cin >> studentData;
ss << studentData;
if (!(studentData.empty())){
string to;
cin.ignore();
while(getline(ss,to,',')){
stuDetails.push_back(to);
}
}
//use to print out marks
back = stuDetails.back();
cout << back << endl;
stuRec.marks = stuDetails.back();
stuDetails.pop_back();
stuRec.sNum = stuDetails.back();
stuDetails.pop_back();
stuRec.sName = stuDetails.back();
stuDetails.pop_back();
stuRec.name = stuDetails.back();
stuDetails.pop_back();
cout << stuRec.name <<endl;
cout << stuRec.sName <<endl;
cout << stuRec.sNum <<endl;
cout << stuRec.marks <<endl;
db.addStudent(stuRec.name, stuRec.sName,stuRec.sNum,stuRec.marks);
Aucun commentaire:
Enregistrer un commentaire