0

I a little new to istringstream operations. I need to find a way to iterate over words in a text file containing eight paragraphs. I need to get each words, test for certain conditions, and then store into a linked list if it passes the checklist. All I need help doing is how to extract each word one by one, use it, check it, and so forth. Here is some code I had: Could anyone lend some advice? I have a loop but it doesnt update the value of the string substrings, Thanks

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

string getLineOfText(ifstream &strIn);
string parseLineOfText(string&);


int main()
{
ifstream in("Text.txt");
istringstream strI;
string substrings;
string strg;
int lineCount = 0;




while (!in.eof())
{
    strg = getLineOfText(in);   
    ++lineCount;

    strI.str(strg);

    while (strI >> substrings)
        cout << substrings << " ";

    strI.str("");
}

cout << substrings << endl;
cout << endl << lineCount << endl;

system("pause");

return 0;
}


string getLineOfText(ifstream &strIn)
{
string lineTxt;
getline(strIn, lineTxt);

return lineTxt;
}
  • 2
    [Don't put `eof` inside a loop condition.](http://stackoverflow.com/q/5605125/2589776) Use `while (getline(in, strg))` instead. – herohuyongtao May 16 '14 at 03:24

1 Answers1

0

Move the definition of strI inside the outer while loop.

int main()
{
   ifstream in("Text.txt");
   string strg;
   int lineCount = 0;

   while (!in.eof())
   {
      strg = getLineOfText(in);   
      ++lineCount;

      istringstream strI(strg);
      string substrings;

      while (strI >> substrings)
         cout << substrings << " ";
   }

   cout << endl << lineCount << endl;

   system("pause");

   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270