Infinite looping problem... (help?)

Hey everyone,

I'm working on my weekly assignment, and have run into a problem that so far no one has been able to help me solve. Basically what I'm doing is reading a text file with multiple sections into an object so it can be printed out later by specific section.

To do this, I have a vector of vectors, which I'm filling with vectors of strings. I'm using nested while loops to read the contents of a text file into this form. Both the separate sections of text as well as the beginning and end of the text are represented by special lines of text (repeated punctuation).

The problem is that, the loops appear to be working fine, reading in values and so forth, until it is time for the outer loop to exit. When this should happen, the loop suddenly starts executing endlessly, outputting whatever seems to have been the last thing in the input stream. I have no idea why this would happen.
Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
      // These are the lines of text which delimit the 
       //beginning/end of file, as well as the sections of text
    const string EQUAL40 = string(40, '=');
    const string DASH40 = string(40, '-');

     int i = 0;  //for testing only
     string line;
      vector<vector>itemList;
      vector<string> item;

 while(line != EQUAL40)
     {
        line = "empty";
        while((line != DASH40))
        {
         //cout << "in do-while \n";
         getline(inFile, line);
         item.push_back(line);
         cout << item[i] << endl;
         i++;
        }

        itemList.push_back(item);
        item.clear();  
        i = 0;
        cin.ignore(80, '\n');
     }


So there it is. What surprises me the most is that the loops seem to execute perfectly fine until the condition to end the outer loop is met.I'd think that, if something like this was going to happen, it would have more predictable behavior, instead of working one way then doing something totally unrelated.

Anyway, I know there's an answer, but I haven't yet found it. Any help with this will be really appreciated. :)
Last edited on
Your file needs to be something like this:
---- //Dashes
one
or
more
lines
------// Dashes
perhaps another line
-------//Dashes
=====//Equals


Is it?

The most obvious issue would be that line never becomes the equals string, so you keep trying to read data from a file that doesn't have anything left to read.
Yup, that's exactly how my input file is formatted. And due to the pause in the code, I know that every line of dashes causes the inner loop to exit. But when the line of equals is read into the vector, it begins printing endlessly. I have checked the length of the line of equal signs and it is definitely 40 characters. have even used different values for the condition for the outer loop -- other values of line, single characters, and even end of file, but the result is always whatever the last line is, repeated endlessly.

Last edited on
Maybe change
while((line != DASH40))
to
while((line != DASH40) && (line != EQUAL40))
line = DASH40
exit inner loop
back to top of outer loop
set line = "empty"
line != DASH40
enter inner loop
read next line (the 40 equal signs)
back to top of inner loop
line != DASH40
read next line
end of file, no input!
back to top of inner loop
line != DASH40
read next line
end of file, no input!
back to top of inner loop
line != DASH40
read next line
end of file, no input!
back to top of inner loop
...
Topic archived. No new replies allowed.