Stuck on number averages

Hello, I've been going over this for a couple of days now. I was able to get the program to do the other aspects that's required except for one. I need it to read the file, "students.txt" and output on the console the highest overall grade average. Any help would be greatly appreciated.

Below is taken from, "students.txt"

Einstein Albert 80 95 85.5 80 95
Franklin Ben 70 75.5 80 82 90
Edison Tom 70 80 75.5 60 40
Tesla Nick 75 75.5 80 90 80
Dijkstra Ed 92 80 70.5 60 85

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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream infile;
	string LastName = "", FirstName = "";
	double grade1 = 0, grade2 = 0, grade3 = 0, grade4 = 0, grade5 = 0, grades = 0, total = 0, five = 5, high = 0, num=0, totals=0;
	int count = 0;
	infile.open("students.txt");
	bool done;

	if (!(infile.eof()))
	{
		infile >> LastName;
		infile >> FirstName;
		infile >> grade1;
                infile >> grade2;
                infile >> grade3;
                infile >> grade4;
                infile >> grade5;
                done = false;
	}

	else
        done = true;

	while (done == false)
	{
	    if (grades = grade1 + grade2 + grade3 + grade4 + grade5)
        {
		total = (grades / five);
        }

		cout << LastName << ", " << FirstName << "      " << total << endl;
		count++;

		if (!(infile.eof()))
        {
            infile >> LastName;
            infile >> FirstName;
            infile >> grade1;
            infile >> grade2;
            infile >> grade3;
            infile >> grade4;
            infile >> grade5;
            done = false;
        }

        else
            done = true;
	}

	cout << endl;
        cout << "The highest grade is: " << total << endl;

	infile.close();
	return 0;
}
Hello rjmacready,

Your program is a good start, but only reads the file twice.

Testing for "eof" does not work the way you might think. By the time you check for "eof" you have already read past "eof", the "infile" stream has failed and you will be processing information from the last read. It is kind of pointless though because you only read two lines, but process one, so the chances of reaching "eof" are small.

The second if statement at line 42 really does nothing but read the file nothing is processed from the read. What would work better is something like this for a start

1
2
3
4
5
6
7
8
while (infile>>LastName)  // <--- Corrects the problem with eof and fails when it tries to read past eof.
{
    infile >> FirstName;
    infile >> grade1;
    ...

    //  other code to process the grades.
}


My question is do you want the highest overall grade average for each line or the entire file? It sounds like the entire file which will take some more work.

Hope that helps,

Andy
Thank you for the reply. I'm not sure exactly so I've just been trying to get an average of one line which would be Einstein Albert 80 95 85.5 80 95. I'm trying to fit it in the program, but haven't had any luck. I think my mind has finally fried.
Hello rjmacready,

I through this together kind of quick, but it will give you a good idea of what could be done.

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 <iomanip>
#include <fstream>
#include <algorithm>
#include <chrono>
#include <thread>

int main()
{
	std::string iFileName{ "Students.txt" };
	std::string lastName{ "" }, firstName{ "" }, msg{ "" };
	double grade1{ 0.0 }, grade2{ 0.0 }, grade3{ 0.0 }, grade4{ 0.0 }, grade5{ 0.0 }, avgGrade{ 0.0 };
	double totAvg{ 0.0 }, maxGrade{ 0.0 };

	std::vector<double> grades;

	std::ifstream inFile;

	inFile.open(iFileName);

	if (inFile.is_open())
	{
		// Uncomment for testing when first starting.
		//std::cout << "\n File " << iFileName << " is open" << std::endl;
		//std::this_thread::sleep_for(std::chrono::seconds(2));
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));
		exit(1);
	}

	while (inFile >> lastName)
	{
		inFile >> firstName;
		inFile >> grade1;
		inFile >> grade2;
		inFile >> grade3;
		inFile >> grade4;
		inFile >> grade5;

		avgGrade = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0;

		msg = firstName + " " + lastName + "'s avgerage grade is: ";
		std::cout << "\n" << std::setw(37) << msg << avgGrade;

		grades.push_back(avgGrade);
	}

	std::sort(grades.begin(), grades.end());

	maxGrade = grades[0];

	for (size_t lp = 0; lp < grades.size() - 1; lp++)
	{
		if (grades[lp + 1] > grades[lp])
			maxGrade = grades[lp + 1];
	}

	for (auto&& lp : grades)
		totAvg += lp;

	std::cout << "\n\n Overall average is: " << (totAvg / grades.size()) << std::endl;
	std::cout << "\n The highest average is: " << maxGrade << std::endl;
}


I put this together from my original code. I do not think I missed anything.

Hope that helps,

Andy
I appreciate the response Andy. Your "for" statement had given me an idea and I was able to get it working. Thank you.
Hello rjmacready,

Your welcome. Any time.

Andy
Topic archived. No new replies allowed.