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. :)