0

I am trying to get a program to read grades and other information from a data file, and then output the grades, and averages for each student, not knowing how many students will be in the class.

My datafile looks like this:

Jackson Greaves 1 2 3 4 5 6 7 8 9 10 1 1 1 1 1 1 1 1 100 100

Daniel Delgadillo 1 2 3 4 5 6 7 8 9 10 2 2 2 2 2 2 2 2 95 13

Nolyn prestion 10 10 10 10 10 10 10 10 10 10 8 8 8 8 8 8 8 8 100 0

And here is the program I have so far (It only reads the first line, and won't loop to read the other lines, How do I get this to loop and loop line by line until the file ends?):

//Lab 1, 9/17/14
//George W. Bush
//Miley Cyrus
//Bob Dylan

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    float lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9, lab10;
    float hw1, hw2, hw3, hw4, hw5, hw6, hw7, hw8;
    float midterm;
    float finall;
    float labScore;
    float labAvg;
    float hwScore;
    float hwAvg;
    float finalGrade;
    string letterGrade;
    string studentNameFirst;
    string studentNameLast;
    ifstream inFile;

    inFile.open("inputGrades.txt", ios::in); // opens input file
    if (!inFile) {
        cout << "Cannot open input file. "; // file not open
        return 1;
    }

    while (!inFile.eof()) {// until file ends
        inFile >> studentNameFirst >> studentNameLast >> lab1 >> lab2 >> lab3 >> lab4 >> lab5 >> lab6 >> lab7 >> lab8 >> lab9 >> lab10 >> hw1 >> hw2 >> hw3 >> hw4 >> hw5 >> hw6 >> hw7 >> hw8 >> midterm >> finall;

        labScore = ((lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10) / 100) * 100;

        labAvg = (lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10) / 10;

        hwScore = ((hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7 + hw8) / 80) * 100; // calculate homework scores

        hwAvg = (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7 + hw8) / 8; //calculates lab averages

        finalGrade = (.2 * (labScore) + .1 * (hwScore) + .3 * (midterm) + .4 * (finall)); // calculates percent final grade

        // Assigns a letter grade based on percent
        if (finalGrade >= 90) 
            letterGrade = "A";

        else if (finalGrade >= 85)
            letterGrade = "A-";

        else if (finalGrade >= 80)
            letterGrade = "B+";

        else if (finalGrade >= 75)
            letterGrade = "B";

        else if (finalGrade >= 70)
            letterGrade = "B-";

        else if (finalGrade >= 65)
            letterGrade = "C+";

        else if (finalGrade >= 60)
            letterGrade = "C";

        else if (finalGrade >= 55)
            letterGrade = "C-";

        else if (finalGrade >= 50)
            letterGrade = "D+";

        else if (finalGrade >= 45)
            letterGrade = "D";

        else if (finalGrade >= 40)
            letterGrade = "D-";

        else letterGrade = "F";

        cout << "Name: " << studentNameFirst << " " << studentNameLast << "Lab Average: " << labAvg << "Hw Average: " << hwAvg << "Final Grade: " << finalGrade << "Letter Grade: " << letterGrade << endl;

        return 0;
    }
}
APerson
  • 7,304
  • 5
  • 32
  • 48
user3006937
  • 47
  • 2
  • 9
  • 1
    You're expecting `eof` to predict the results of a read that will occur in the future rather than testing if your read actually succeeded. – David Schwartz Sep 29 '14 at 23:02

1 Answers1

4

You have return 0; inside your while cycle. That's why you never get to the next iteration of this cycle: your function just returns.

Removing this line should make things better. Apart from that, checking for eof() makes sense after you have (tried to) read some data, not before (as already noticed).

Ashalynd
  • 11,728
  • 2
  • 29
  • 35