0

I am trying to only loop through a certain part of code when reading in a text file because the first line is the only unique line. The rest of the lines repeat as far as the types of variables they are assigned to and what they are supposed to represent. I've tried adding "break" a couple different times and only get it halfway fixed. What is a more efficient way to do this because I don't feel that using "break" is correct? : / Here is the code I'm referring to....

void RegOffice::analyzeFile()
{
    cout << "Please enter the location of the file to be read. " << endl;
    cin >> fileName;
    cout << endl;
    inData.open(fileName.c_str());

    if (inData.is_open())
    //while file is open
    {
        int c;
        while(inData)       //While file is still good
        {

            /* 
            The function below reads in one int at a time on each line. First line is the number of windows
            open. The next line will be the time (or clock tick) at which the next student(s) arrive. 
            The next line will be the number of students that arrive at that time. The lines after that
            will be the amount of time each student needs at a window in minutes.
            */


            inData >> winOpen;  //Gets the number of windows open
            cout << "Windows Open: " << winOpen << endl;            
            windows = new GenQueue<Window>(winOpen);    //Sets up an array of open windows
            while(!inData.eof())  //This code below is what I want to loop through separately (rather than the above code being called again)
            {
                inData >> time;     //Gets the first time that students arrive
                cout << "Time: " << time << endl;   
                inData >> numStudents;      //Gets the number of students that enter the line at that time
                cout << "Number of Students at time " << time << " is " << numStudents << endl; 

                for(int i = 0; i < numStudents; i++)    // numStudents is the number read in by the text file
                {
                    Student *stu = new Student(); //creating a new instance of a student
                    inData >> c;
                    stu->setTimeAtWin(c);  //Setting student's wait time to the next number in file
                    stu->setArrivalTime(time);   //Setting student's arrival time
                    cout << "New Student created! Info for Student #" << i + 1 << ":" << endl;
                    stu->print();
                    line->addBack(*stu);    //Inserting that student to the back of the queue (The first student                                                //will be first in line though if no one is in line)
                    cout << "Number of people in line: " << line->getSize() << endl;
                }

            }
        }
        inData.close();
    }
    else    //Shows error message
        cout << "Error opening file! Please try again!" << endl;
}
Melissa
  • 29
  • 6
  • 1
    You should read this: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Retired Ninja Mar 28 '15 at 08:05

1 Answers1

0

You can create a variable to keep track of the state of reading lines while you are executing the while-loop.

Then, you'd adjust your while-loop logic, and change the state.

enum FileReadingState { LOADING_DATA, LOADING_DATA_COMPLETED };

If you have special words in your file, you then look for them with if statements, and switch state of FileReadingState accordingly.

mrflash818
  • 930
  • 13
  • 24