Read in Data from a File to Create a 2D Array

I posted about this last week but the solution didn't provide me enough clarity so I'm hoping this time works out better. My assignment is to read in a text file containing a grid of grades for students into a two dimensional array, then to calculate the average of each student based on the grades on the assignments. Here is the text file:

Student1 98 71 73 71 82 88 77
Student2 87 83 98 76 76 80 75
Student3 82 92 85 80 88 79 87
Student4 84 95 89 70 86 90 99
Student5 96 90 87 84 100 72 73
Student6 86 75 98 71 75 89 72
Student7 70 84 85 80 90 90 78
Student8 77 96 97 75 77 73 71
Student9 89 79 87 86 70 77 84
Student10 74 79 87 96 96 94 73
Student11 90 88 95 95 90 86 76
Student12 92 87 80 87 98 71 76
Student13 79 94 75 93 86 88 93
Student14 82 90 92 83 88 94 84
Student15 79 78 72 72 81 90 90
Student16 100 84 79 95 78 77 95
Student17 77 98 98 98 74 80 89
Student18 83 84 96 75 98 75 99
Student19 94 98 89 86 98 76 84
Student20 87 76 98 83 76 72 88
Student21 98 89 93 82 73 84 96
Student22 81 98 85 73 73 99 76
Student23 98 70 80 84 76 80 82
Student24 81 73 74 74 77 86 87
Student25 94 76 83 83 72 76 100
Student26 70 70 82 74 85 85 94
Student27 75 77 72 84 89 90 84
Student28 78 88 81 85 87 72 82
Student29 98 99 88 70 71 88 70
Student30 95 100 77 79 70 89 89
Student31 75 91 93 93 71 74 77
Student32 72 90 76 98 88 90 82
Student33 82 84 75 75 100 88 93
Student34 97 100 74 93 84 82 97
Student35 92 93 74 93 72 96 92
Student36 100 96 77 91 95 78 90
Student37 90 75 82 96 98 82 76
Student38 81 72 80 84 86 84 88
Student39 71 71 96 94 76 81 85
Student40 85 93 99 94 91 83 95


I haven't even tried to create the calcaverage function for the averages because I can't get the data to properly read into the array. My professor said that we should use a nested for loop to read in the data, which makes sense to me, but for some reason everytime I try to read the data in everything goes to zero. Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  // Week 13 Program.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

const int STUDENTS = 40, ASSIGNMENTS = 7, TOTAL = 350;

int main()
{
	ifstream grades;
	string filename;
	int i = 0, row, col;
	vector< vector<int> > gradebook;

	cout << "Enter the filename:" << endl;
	getline(cin, filename);
	grades.open(filename);

	for (row = 0; row < STUDENTS; row++)
	{
		vector<int> row;
		for (col = 0; col < ASSIGNMENTS; col++)
		{
			int readFromFile = 0;
			grades >> readFromFile;
			row.push_back(readFromFile);
		}
		gradebook.push_back(row);
	}
	
	grades.close();
	return(0);
}


Alternatively, I've also tried, which is the primary intent of my professor. I only tried the vector method after looking around online after my first method did not work.
1
2
3
4

	for (row = 0; row < STUDENTS; row++)
		for (col = 0; col < ASSIGNMENTS; col++)
			grades >> gradebook[STUDENTS][ASSIGNMENTS];


Any help would be vastly appreciated, and if you could provide help for the calacaverage function as well, that would be very helpful.

Thanks
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iomanip>

struct student_info // information for each student
{
    std::string name ;
    std::vector<int> grades ;
};

// extract the student_info information from a line (in the file)
student_info get_student_info_from( std::string& line )
{
    student_info st_info ;

    // create a string stream to read from the line
    std::istringstream str_stm(line) ;

    str_stm >> st_info.name ; // read in the name (first fld in the line)
                              // we assume that the name does not contain spaces

    // read in as many grades as are there in the line and add them to the vector
    int grade ;
    while( str_stm >> grade ) st_info.grades.push_back(grade) ;

    return st_info ;
}

std::vector<student_info> get_students_from_file( const std::string& file_name )
{
    std::vector<student_info> all_students ;

    std::ifstream file(file_name) ; // open the file for input

    std::string line ;
    while( std::getline( file, line ) ) // for each line read from the file
    {
        // extract the student info from the line and add it to the vector
        all_students.push_back( get_student_info_from(line) ) ;
    }

    return all_students ;
}

int main() // a simple test program
{
     const std::string file_name = "student_infos.txt" ;

     // create a small test file
     std::ofstream(file_name) << "Bugs      11 12 13 14 15 16\n"
                                 "Daffy     21 22 23 24 25 26\n"
                                 "Porky     31 32 33 34 35 36\n"
                                 "Sylvester 41 42 43 44 45 46\n" ;

     const auto student_infos = get_students_from_file(file_name) ;

     // print out the student information that was read
     for( const auto& sinfo : student_infos )
     {
         std::cout << "name: " << std::setw(12) << sinfo.name << "  grades: [ " ;
         for( int grade : sinfo.grades ) std::cout << grade << ' ' ;
         std::cout << "]\n" ;
     }
}

http://coliru.stacked-crooked.com/a/0905350451bccc12
Last edited on
There are parts of this we haven't covered in class and wouldn't go over well if I turned in something using parts of this, like the whole string stream function. My professor wants us to use the nested for loop to read the data into the array, I just can't figure out how to get mine to work.
> My professor wants us to use the nested for loop to read the data into the array,
> I just can't figure out how to get mine to work.

Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>

struct student_info // information for each student
{
    std::string name ;
    std::vector<int> grades ;
};

std::vector<student_info> get_students_from_file( const std::string& file_name,
                                                  int n_assignments )
{   std::vector<student_info> all_students ;

    std::ifstream file(file_name) ; // open the file for input

    student_info student ;
    student.grades.resize(n_assignments) ; // make it large enough to hold all the grades

    // for each name read from the file (we assume that the name does not contain spaces)
    while( file >> student.name ) // for( int i = 0 ; file >> student.name ; ++i )
    {
        // read the grades
        for( int j = 0 ; j < n_assignments ; ++j ) file >> student.grades[j] ;

        // if the read was successful, add this student to the vector
        if(file) all_students.push_back(student) ;
    }

    return all_students ;
}

int main() // a simple test program
{
     const std::string file_name = "student_infos.txt" ;

     const int N_ASSIGNMENTS = 7 ;

     // create a small test file
     std::ofstream(file_name) << "Bugs      11 12 13 14 15 16 17\n"
                                 "Daffy     21 22 23 24 25 26 27\n"
                                 "Porky     31 32 33 34 35 36 37\n"
                                 "Sylvester 41 42 43 44 45 46 47\n" ;

     const auto student_infos = get_students_from_file( file_name, N_ASSIGNMENTS ) ;

     // print out the student information that was read
     for( const auto& sinfo : student_infos )
     {
         std::cout << "name: " << std::setw(12) << sinfo.name << "  grades: [ " ;
         for( int grade : sinfo.grades ) std::cout << grade << ' ' ;
         std::cout << "]\n" ;
     }
}

http://coliru.stacked-crooked.com/a/2a6791680e2ffc65
Yes that worked very well and I intuitively understood it as well. Thank you very much for your help!
Topic archived. No new replies allowed.