Hey all,
I'm writing a program that find all the phrases of a certain length in a file. So it runs through the file from start to end finding every phrase and stores them in a vector of strings. It works almost perfectly, except it sometimes deletes one character in the same place every time a new phrase is found.
Esperanza feels lost, alone,
feels lost, alone, and
lost, alone, and confused.
alone, and confused. She
and onfused. She believes
onfused. he believes that
he elieves that the
elieves hat the only
hat he only ones
he nly ones who
nly nes who can
As you can see above, starting on the fifth line, the second word in the phrase loses its first letter.
The relevant class code for generating the above is below:
void Doc::findPhrases() {
string curPhrase;
string buffer;
fstream file("small.txt");
int curLoc;
while(true) {
curPhrase = "";
if (file.eof()) return;
file >> curPhrase;
curLoc = file.tellg();
for (int i=0; i<phraseLength-1; i++) {
if (file.eof()) return;
file >> buffer;
curPhrase = curPhrase + " " + buffer;
}
phrases.push_back(curPhrase);
file.seekg(curLoc);
}
file.close();
}
void Doc::printPhrases() {
for (int i=0; i<50; i++) {
cout << phrases << endl;
}
}
I think it has to something to do with how I read in the first buffer word (the second letter in the phrase)... but I'm not sure what exactly is wrong.
Any ideas?
Thanks for the help!