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...
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.
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.
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:.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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.