if(inputFile.is_open()){
while(!inputFile.eof()){
getline(inputFile, line);
ss << line;
while(ss){
ss >> key;
cout << key << " ";
lineSet.insert(lineNumber);
concordance[key] = lineSet;
}
lineNumber++;
}
}
For some reason, the while loop is kicking out after the first iteration and only displays the first sentence of my input file. The rest of the code works fine, I just can't figure out why it thinks the file has ended after the first iteration.
Thanks
Firstly you should be reading the file without using
eof, as πάντα ῥεῖ notes (see here for explanation):Note that the preceding
ifis not necessary either.The main problem , assuming
ssis astringstreamyou defined earlier, comes from the logic:The
whileloop here only exits whenssfails. But you never resetssto be in a good state. So although your outer loop does read every line of the file, all of the lines after the first line never generate any output.Instead you need to reset the stringstream each time: