Helping with calculating files brought into the program.

I'm stuck, I'm not sure how to calculate a line from a txt file I import into the program. The txt file has a set of 4 grades for each line, I have to calculate the average for each one and display it which I can do but I'm not sure how to get IDE to read each line and find the average.

The data in the txt file:

44 55 77 88
79 88 100 99
77 99 98 99
100 88 89 100
55 56 40 77
100 100 99 95
88 84 87 88
96 97 99 100
30 44 77 55
79 77 88 0
54 52 60 77
88 77 88 77
44 77 10 95


code so far to read the file in:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
//Each line represents the four tests taken by students in a class during the semester.
//You need to calculate the average for each student in the class by reading in the file.
//Display the average for each student along with their letter grade.
int infile;
string number;
ifstream myfile ("Grades.txt");
if (myfile.is_open())
{
while ( getline (myfile, number) )
{
cout << number << endl;
}
myfile.close();
}
else cout << "Unable to open file.";

while (myfile >> number)
{
cout << number << endl;

}

Last edited on
closed account (o3hC5Di1)
Hi there,

Try the following:

1
2
3
4
5
6
int grade1, grade2, grade3, grade4;

while (myfile >> grade1 >> grade2 >> grade3 >> grade4)
{
    //calculate average
}


Of course, the grades could just be an array rather than 4 variables.

Hope that helps, please do let us know if you require any further help.

All the best,
NwN
Topic archived. No new replies allowed.