My code functions as it should, however I'm in search of a more elegant solution
In this for-loop, I'm parsing a line of text from a file with data members seperated by a comma. This has been stored in vector<str>str
file in text:
bob,1,2,3
sally,5,8,6
joe,5,1,9
I need to store seperately the name and the 3 corresponding scores into their respective compartments in vector<student> s
.
Any way to further condense this?
for (string currLine : str)
{
int pos = 0;
int next = 0;
next = currLine.find (',', pos);
string name (currLine.substr (pos, next));
pos = next + 1;
next = currLine.find (',', pos);
string expr (currLine.substr (pos, next));
int a = atoi (expr.c_str());
pos = next + 1;
next = currLine.find (',', pos);
expr = (currLine.substr (pos, next));
int b = atoi (expr.c_str());
pos = next + 1;
next = currLine.find (',', pos);
expr = (currLine.substr (pos, next));
int c = atoi (expr.c_str());
student s (name, a, b, c); //create student with extracted name and scores
vec.push_back (s); //now push this student onto the vector
}//end for()