-2

Keep getting stuck in an infinite loop dont know where my logic went wrong

used while eof and dont know what else is missing, also break statement didnt do anything but print out my test statement once

void readSetupData(string sfile, string efile, string afile, students 
studArray[])
{
    ifstream inS(sfile.c_str());
    ifstream inA(afile.c_str());
    ifstream inE(efile.c_str());
    int numStudents = 0;


       while (!inS.eof())
        {
        cout << "BEEP" << endl;

                int id;
                int examScore;
                string name;
                inS >> name >> studArray[numStudents].id >> 
studArray[numStudents].name;

                int examId;

                inE >> id >> examId >> examScore;

                int studentIndex = findStudent(id, studArray);
                int examIndex = findExam(examId, 
studArray[studentIndex].examArray);
                studArray[studentIndex].examArray[examIndex].pointsScored 
= 
examScore;
                int pointsAvail = 
studArray[studentIndex].examArray[examIndex].pointsAvail;
                studArray[studentIndex].examArray[examIndex].percentage = 
(float)examScore / (float)pointsAvail;

        }


        while (!inA.eof())
        {
                int id;
                int assignId;
                int assignScore;

                inA >> id >> assignId >> assignScore;

                int studentIndex = findStudent(id, studArray);
                int assignIndex = findAssignment(assignId, 
studArray[studentIndex].assignArray);
                studArray[studentIndex].assignArray[assignIndex].pointsScored 
= assignScore;


        }
}

the first void function is the problem and the test statement BEEP is repeated when compiled and ran with ./a.out student_info.txt exam_info assignment_info.txt exam_scores.txt assignment_scores grades.out

potart
  • 1
  • 1
    `while (!inA.eof())` is wrong. See https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong. PS: There maybe other errors in your code. – R Sahu Jan 06 '19 at 07:52
  • The logic error seems to be that you are trying to find students before you have read in the whole student array. Try separating out the `inS` and `inE` loops. And as has already been said `while (!inS.eof())` is wrong, Write your loop like this `while (inS >> name >> studArray[numStudents].id >> studArray[numStudents].name)` Another error is that you don't increment the `numStudents` variable in your first loop. There are probably loads of errors, you should learn how to use a debugger so you can find them for yourself. – john Jan 06 '19 at 07:58

1 Answers1

0

You are expecting eof to predict the future and assure you that a subsequent read won't encounter an end-of-file. That's just not what it does. Don't use the eof function at all.

You have no handling for any errors except to repeat the operation that failed which, with near certainty, will fail again and repeat the cycle. Instead, check each and every operation to see whether it succeeded or failed. If it fails, don't just keep going because it will just fail again leading to an endless loop.

One of the first things you should do when you don't understand why your code is doing what it's doing is add error checking to every single library call or function call that can possibly error in any conceivable way.

David Schwartz
  • 166,415
  • 16
  • 184
  • 259