Reading in scores from a file

I am having trouble figuring out how to read in scores from a file. The first line of the file has three integers, each indicating the number of quiz grades, homework grades, and exam grades.
The subsequent lines have the last name, first name, and the grades for each assignment.
What I am having trouble with is figuring out how to sum the number (x) of quiz grades, then move on to sum the number (y) of the homework grades, etc...

Please Help!!!
Please show what you've tried and be sure to also show a sample of your input file.

What I've tried:

string first_name, last_name;
int a, b, c, sum, Qt, Ht, Et;
int Total, num, count;
int x, y, z;
float avg;

infile >> a >> b >> c;
sum = a + b + c;

getline(infile, last_name);

while(!infile.eof() && infile.good())
{
getline(infile, first_name);

if (infile.fail()) // Check to make sure the file is not in the fail state.
{
cout << string(15, '*') << " File Read Error " << string(15, '*') << endl;
cout << "==> Program failed to read the exam scores!!" << endl;
cout << "==> Error in file content." << endl;
cout << "==> Terminating program!!!" << endl;
cout << string(47, '*') << endl;
return 1;
}
num = 0;
count = 0;
infile >> x, y, z;
while(count <= a)
{
Qt = num + x;
num = Qt;
infile >> x;
}

Input file:
4 4 2
Key Key 10 10 10 10 20 20 20 20 100 100
Franklin Ben 10 10 10 10 20 20 20 20 100 100
Washington George 10 10 0 10 20 0 0 20 100 80
Hello bigmoney221,

I will start withe the problems that are easy to see: you did not include what include files that you used. Do not know is some of this will even work. Then the line infile >> a >> b >> c;. Where did you define infile and where did you open the file to read?

Not a good idea to check for end of file for the while condition because eof() does not work the way you think. The eof() bit is set when you try to read past the end of the file. By then it is being check at the wrong time and you are trying to process invalid data. The while loop works better as: while (getline(infile, last_name)). This way if then read fails the while loop will be false and end. Using the 4 4 2 from the first line of the file the while loop will need to input in groups of 4, 4 and 2 or whatever the first line numbers may be.

Hope that helps,

Andy
I defined infile and opened the input file earlier in the program. eof() is there as !infile.eof(). So the the loop will continue while the end of the file has not been reached. The only part I am having trouble with is getting the values for each of the categories. The first line indicates how many values are in each category. I need the total for each category and then the overall total after the total for each category has been calculated.
Hello bigmoney221,

I just finished the program and I used three for loops inside the while loop to read the file and create a total. When the three for loops have finished you can use the totals from each loop to create a grand total or you can create the grand total in a cout statement.

Post your code changes and we can work from there.

As a note please use code tags the <> button to the right under format:.

Hope that helps,

Andy
I thought about for loops, but I am having trouble thinking logically how to set it up.
you need to use something like this
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Man { // create stuct, whitch will be discribe data 
	string name;
	string surname;
	int a[5];  // array of values 
	void set(int x, int ind) {
		a[ind]=x;
	}

};
int main() {
	vector<Man>val;
	ifstream ff;
	Man max;
	ff.open("tr.txt");  // open file 
	int a1,a2,a3;
	ff>>a1>>a2>>a3;      // read 1,2,3 number 
	cout<<a1<<a2<<a3<<endl; // wtite them 
	while(!ff.eof()) {   // while file is not emoty 
		string n,s;     
		int m;
		ff>>n>>s; // read 1 and 2sd strings
		max.name=n;       // pass into stuck
		max.surname=s;
		for(int i=0; i!=5; i++) {    // read number
			ff>>m;
			max.set(m,i);   // pass it int struct array 
		}
		val.push_back(max); // input struct on vector 
	}
	ff.close();
	for(vector<Man>::iterator it=val.begin(); it!=val.end(); it++) {    // read object of vector 
			int sum=0;
		cout<<it->name<<" "<<it->surname<<endl;
		for(int i=0; i!=5; i++) {
			cout<<it->a[i]<<" ";
			sum+=it->a[i];          // summ
		}
		cout<<"Sum "<<it->name<<" : "<<sum;
		cout<<endl;
	}
	
	return 0;
}
I thought about for loops, but I am having trouble thinking logically how to set it up.

Perhaps, if you used more meaningful variables names, it might be clearer.

Here's an example of using for loops to read the scores:
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
#include <iostream> 
#include <string>
#include <fstream>
using namespace std;

int main ()
{   string first_name, last_name;
    int num_quiz, quiz_score, quiz_tot;
    int num_hw, hw_score, hw_tot;
    int num_exam, exam_score, exam_tot;
    int total;
    ifstream    infile ("data.txt");
        
    //  Read the first line
    infile >> num_quiz >> num_hw >> num_exam;
    while (infile >> last_name >> first_name)
    {   //  Read the quiz scores
        quiz_tot = 0;        
        for (int i=0; i<num_quiz; i++)
        {   infile >> quiz_score; 
            quiz_tot += quiz_score;
        }
        //  Read the homework scores
        hw_tot = 0;
        for (int i=0; i<num_hw; i++)
        {   infile >> hw_score;
            hw_tot += hw_score;
        }
        //  Read the exam scores
        exam_tot = 0;
        for (int i=0; i<num_exam; i++)
        {   infile >> exam_score;
            exam_tot += exam_score;
        }
        total = quiz_tot + hw_tot + exam_tot;
        cout << last_name << ", " << first_name << " = " << total << endl;
    }
    system ("pause");
}    
Key, Key = 320
Franklin, Ben = 320
Washington, George = 250
Press any key to continue . . .


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Topic archived. No new replies allowed.