Writing from text file to multi-dimensional array.

What I'm trying to figure out is if it's possible to write a chart with numbers and words into a two dimensional array.

I have this in my text file:
1
2
3
4
5
6
7
8
9
Name    Exam1   Exam2   Exam3

John    90.9    69.9    60.0

Jane    87.0    71.0    91.0

Bill    95.5    80.7    88.0

Susan   75.0    75.1    80.0


And at the end, the program should output the average score of each row and column:

1
2
3
4
5
6
7
Name	Exam1	Exam2	Exam3	TotaOfScores	AverageScore
John	90.9	69.9	60.0	220.8		73.6
Jane	87.0	71.0	91.0	249.0		83.0
Bill	95.5	80.7	88.0	264.2		88.1
Susan	75.0	75.1	80.0	230.1		76.7
AvScore 87.1	74.2	79.8	241.0		80.3



What I can't figure out is how to put the relevant numbers from the charts into an array while ignoring the labels. I know that you can't put multiple data types into an array but I'm not sure if you can skip specific rows or data types.
Last edited on
At first read one line of four strings.
Then read multiple lines with the first item a string and the other three as numbers.
Reading ends on any error or end-of-file.
How would I go about alternating between the text lines and data types?

I've only figured out how to import the entire file into an array
Last edited on
Maybe have a struct/class holding the name and exam scores?
1
2
3
4
5
struct Student
{
    string name;
    vector<double> exam_scores;
};


Then you can overload the extraction operator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
istream& operator>>( istream& is, Student& s )
{
    if( !(is >> s.name) ) return is;

    vector<double> scores{};
    for( double d{}; is >> scores; ) {
        if( is.eof( ) ) break;

        scores.push_back( d );
    }

    s.exam_scores = scores;

    return is;
}


Then you can just read the headings into a string, then read each line, storing that into a Student object. Use a vector to store all the Students and just iterate through the Students vector and Student::exam_scores to calculate the total and average.
Topic archived. No new replies allowed.