Calculating Grades

Hey everyone,

I'm doing a program that has structs and here's my assignment:

For this lab, you will make an array of structures of arrays. There is a file in the dropbox of a pretend grading spreadsheet of CS 161 students. Your program should open this file and read it into the correct data structure. Since CS 161 consists of 10 assignments, 10 quizzes, 10 labs, a midterm, and a final, your data structure should look like this:

struct Student {
string name;
int asgn[10];
int quiz[10];
int lab[10];
int midterm;
int final;
};

You can assume that at this point in the quarter, there are 20 students left in the class. Thus there will be 20 entries in the file, and you will need to create an array of 20 students (called students) in main, like this:

Student students[20];


I'm working on the first part and having trouble with my code where it would have one person's name from a txt file and not the others.


Here's my code:



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 <fstream>
        #include <sstream>
        using namespace std;
        
        const int ARRAY_SIZE = 10;
        
        //all of a students info is stored here
        struct Student {
        	string name;
        	int asgn[10];
        	int quiz[10];
        	int lab[10];
        	int midterm;
        	int final;
        
        };
        Student s[20];
        
        
        void fill_array(int *arr, stringstream &ss) {
        	for (int idx = 0; idx < ARRAY_SIZE; idx++) {
        		ss >> arr[idx];
        		cout << arr[idx] << " ";
        	}
        	cout << "\n";
        }
        
        int main() {
        
        	string str;
        	ifstream file;
          
        	file.open("Grades.txt");
        
        	for (int i = 0; getline(file, str); i++) {
        
        		stringstream ss(str);
        		ss >> s[i].name;
        
        		fill_array(s[i].asgn, ss);
        		fill_array(s[i].quiz, ss);
        		fill_array(s[i].lab, ss);
        
        		ss >> s[i].midterm >> s[i].final;
        
        		cout << s[2].name << s[2].lab[9] << endl;
        	}
        
        
        	//pause and exit
        	getchar();
        	getchar();
        	return 0;
        }
Last edited on
bumpin' this, how's it going OP? I'm working on the same thing and it's due in a few hours. Unfortunately, I can't offer any insight just yet as I'm having trouble getting mine off the ground.

Can you post where your currently at, Revert? It would help me a bit if you didn't mind
Last edited on
If I understand it correctly, you have to read the data from the file inside the structure, not a string stream. Simply input the values inside the fields of your structure from the file. And depending on what separators you got in the file (either space or newline) set your getline arguments.
Topic archived. No new replies allowed.