I have a struct of Examinee like below (each has 1 id and their scores of different subjects):
struct Examinee
{
string id;
float math, literature, physic, chemistry, biology, history, geography, civic_education, natural_science,
social_science, foreign_language;
};
Now i want to write a function that reads from a string different values and assign them to an Examinee. The string looks like this (each info is separated by a comma):
BD1200001,9,4.0,5.0,10,3.5,7.5,4.25,7.0,7.75,9.25,2.0
This is what i have done so far:
Examinee readExaminee(string line_info) {
//turn line_info to char*
int Line_info_length = line_info.length();
char* info = new char[Line_info_length + 1];
strcpy(info, line_info.c_str());
//create examinee
Examinee examinee;
//read id into examinee by token
char* token = strtok(info, ",");
examinee.id = token;
//read score and assign to subjects
while (token != NULL)
{
float score = strtof(token, NULL);
//assign score to appropriate subject
token = strtok(NULL, ",");
}
delete[] info;
return examinee;
}
The question is: Can i assign each score to each subject in while loop like above? How can i do that? If not, is assigning each score manually the only way?
*(&math + i)in loop. But I do not suggest to do things like that. I would usemap<string, float> scorsand obtain string from some build-in array or csv headersstring headers[]{"math",...}and assign in loop toscore[headers[i]] = val[i].