Need help with this project reading file

/* Name: [Insert Name]

Project: GradeReport.cpp
Topic: Using files and reading strings and numbers

Date: [Insert Date]

Description:
The program calculates a weighted average and
determines the letter grade on 7-pt. scale.

Data file Grades.txt includes last name, first name,
Homework grades, test grades and final exam grade.

Algorithm:
[Insert your algorithm here...]

*/

#include <iostream>
#include <string>
#include <fstream> // For file streams
#include <iomanip>

using namespace std;

ifstream in; // Input file stream
ofstream out; // Output file stream

int main()
{
// Variables
string firstName, lastName;
double g1, g2, g3, g4, g5, final, hwAvg, testAvg, avg;
char grade;

in.open("grades.txt"); // Open input file grades.txt
if (in.fail())
{
cout << "Error with input file . . ." << endl;
exit(1);
}

out.open("report.txt"); // Create output file report.txt
if (out.fail())
{
cout << "Error with output file . . ." << endl;
exit(1);
}
out << fixed << showpoint << setprecision(1); // Set formatting flags

out << setw(25) << "Student Grade Report" << endl; // Write report title to file
out << endl;

in >> firstName >> lastName >> g1 >> g2 >> g3 >> g4 >> g5 >> final; // Read first values from input file

while (!in.eof()) // Loop until the end of file is reached
{

// Perform calculations


// Determine letter grade with if else statement


// Write name, average, and letter grade to file

in >> firstName >> lastName >> g1 >> g2 >> g3 >> g4 >> g5 >> final; // Read next values from file

} // End of while loop

// Display a message to screen to indicate processing is complete.
cout << "\nProcessing complete..." << endl << endl;
cout << "Open the file report.txt for results." << endl << endl;
system("pause");

// Close Files
in.close();
out.close();

return 0;
}

/* Program Output (to file)



*/
what is your question
Line ???: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


PLEASE ALWAYS 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.
Topic archived. No new replies allowed.