arrays - How to do a for loop over values read by a string stream in c++? -


i have program supposed read input file via string stream. file contains student names, , test scores want average. file can contain number of test scores number of students greater 1 , less 10.

if reading values in file string stream, how store each test score value integer can sum them? here code have far, not sure correct:

 string fname, lname, line;  getline(cin, line);  istringstream sin;  sin.str(line);  sin >> fname >> lname; 

is right way parse through values? @ top, declared struct 'student' this:

struct student {   string first_name;   string last_name;   double avg_score; } student1; 

thank you!

if each line varies in number of scores i'd tend read in complete lines , parse them, 1 after other. thereby can rely on >>-operator return false once no more score read in line. think on right way. see following code demonstrating how deal return values of >>:

int main() {      ifstream f(datafile);     if(f) {         string line;         while (getline(f,line)) {             string fname,lname;             istringstream ss(line);             if (ss >> fname >> lname) {                 double sum = 0;                 double value;                 int count = 0;                 while (ss >> value) {                     sum += value;                     count++;                 }                 cout << line << " gives average: " << sum/count << endl;             }         }     } } 

storing values in struct straight forward (and left :-)). in case face troubles please ask. hope helps.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -