Hi im trying to loop through an input file such as:

333 45 56 67
456 80 90 74
...


First number is id, followed by three exam scores..and the "stud.h" is my struct for each piece of data.

My problem is looping through each line of data. Any ideas what im doing wrong?

#include "stud.h"

void ReadRd(ifstream& inFile, StudRd& rd)
{
    inFile >> rd.id;

    while(inFile){
    inFile >>  rd.exam1 >> rd.exam2 >> rd.exam3;

    inFile >> rd.id;
    }
}

void WriteRd(ofstream& outFile, StudRd rd)
{
    int i;

    outFile << "*~< Student Exam Report >~*" << endl;

    outFile << "ID" << setw(10) << "Exam 1" << setw(20)
    << "Exam 2" << setw(20) << "Exam 3" << setw(20)
    << "AVG" << endl;

    outFile << "--" << setw(10) << "------" << setw(20)
    << "------" << setw(20) << "------" << setw(20)
    << "---" << endl;


    outFile << rd.id << setw(10) << rd.exam1 << setw(20)
    << rd.exam2 << setw(20) << rd.exam3 << setw(20)
    << setprecision(4) << rd.avg << endl;


    outFile << "END" << endl;

}
Member Avatar for JSPMA1988

I would help, but out of fear of getting negative rep for doing so I have to restrain myself. However, it appears that the error comes from the while loop in "ReadRd", which should look something more like this:

while (!inFile.eof())
// Code goes here

The problem is that your inFile variable in the while loop never advances to the next piece of data in the file.

commented: that's it +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.