0

This is for a lab in my CS programming class. This is what I wrote:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int grade, avg, count = 0, total = 0;

    ifstream gradeFile;
    gradeFile.open("Lab9C.txt");

    gradeFile >> grade;

    while (!gradeFile.eof())
    {
        cout << "Grades: ";

        while (grade != 0)
        {
            cout << grade << " "; 
            count++;
            total = total + grade;
            avg = total / count;
            gradeFile >> grade;
        }

        cout << "Average: " << avg << endl;
    }

    gradeFile.close();
    return 0;
}

The program is supposed to read each line of a file and print all the grades from that file, then average the grades and print that. Right now It's only printing: Grades: Average: 87 - on infinite repeat. I've tried moving my input statement to different areas, like within the first look and within the second one (initializing grade outside the loop with a grade = -1), and I get infinite loops no matter where I put that statement.

I looked at the other EOF questions on stackoverflow and they all appeared to be different questions. I'm pretty sure I formatted my eof correctly, since it's formatted exactly like the other eof while loops I wrote for this class. So why is it infinitely repeating?

Edit: Both an EOF and a nested sentinel loop are required for this assignment. Instructions:

• Use an eof controlled while loop to read an unknown number of lines from the text file, Lab9C.txt • Each line will contain quiz grades; the last number on each line will be a 0. This signals the end of that set of quiz grades. • Use a nested while loop (sentinel controlled) to read the quiz grades from the file, stopping when it reads the 0. • Print each grade as you read it and print all the quiz grades in a set on the same line with spaces in between them. (Don’t print the 0.) • Calculate the average of the quiz grades on a line and print it on a separate line with a label. (Use double variables for this program.)

Additionally, if it helps, here is the text from the outside file, 0 is intended as an "end line" instruction:

80 95 93 87 80 92 0 75 80 85 94 70 0 80 70 90 75 0 79 86 90 95 84 78 88 0 65 80 78 90 92 100 0

Ok, Friend turned me on to the solution, I should have put gradeFile >> grade; inside the first loop. I'm really annoyed because I was almost certain I had tried that already. Sigh. Either way, it works perfectly now.

Kind of annoyed by the guy who linked the completely inapplicable EOF archived post, which I had read before posting here, and which I already knew didn't apply. I guess some people just post that by habit.

  • By the way, shouldn't `count` and `total` be initialized before each inner loop? – MikeCAT Sep 26 '20 at 05:06
  • 1
    [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Sep 26 '20 at 05:09

1 Answers1

0

EOF is not checked in the loop:

        while (grade != 0)
        {
            cout << grade << " "; 
            count++;
            total = total + grade;
            avg = total / count;
            gradeFile >> grade;
        }

Therefore, it will run into infinite loop when it reaches EOF without 0 is entered.

On the other hand, when 0 is inputted, no more input will read from the file and therefore no EOF check will performed. This is why Grades: Average: 87 is repeated endlessly.

To improve:

  • Check for EOF in the inner loop.
  • Exit from outer loop when no input is taken in inner loop.

Try this:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int grade, avg, count = 0, total = 0;

    ifstream gradeFile;
    gradeFile.open("Lab9C.txt");

    for(;;)
    {
        cout << "Grades: ";

        bool inputTaken = false;
        while (gradeFile >> grade) // check for EOF in the inner loop
        {
            inputTaken = true;
            if (grade == 0) break;
            cout << grade << " "; 
            count++;
            total = total + grade;
            avg = total / count;
        }
        if (!inputTaken) break; // exit from outer loop when no input is taken in inner loop

        cout << "Average: " << avg << endl;
    }

    gradeFile.close();
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70