title: How do you search a document for a string in c++?
body: <p>Here's my code so far:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  
  using namespace std;
  
  int main()
  {
      int count = 0;
      string fileName;
      string keyWord;
      string word;
  
  
      cout &lt;&lt; "Please make sure the document is in the same file as the program, thank you!" 
           &lt;&lt; endl &lt;&lt; "Please input document name: " ;
      getline(cin, fileName);
      cout &lt;&lt; endl;
  
      cout &lt;&lt; "Please input the word you'd like to search for: " &lt;&lt; endl;
      cin &gt;&gt; keyWord;
      cout &lt;&lt; endl;
      ifstream infile(fileName.c_str());
      while(infile.is_open())
      {
          getline(cin,word);
          if(word == keyWord)
          {
              cout &lt;&lt; word &lt;&lt; endl;
              count++;
          }
          if(infile.eof())
          {
              infile.close();
          }
  
      }
      cout &lt;&lt; count;
  
  }
  </code></pre>
  
  <p>I'm not sure how to go to the next word, currently this infinite loops...any recommendation?</p>
  
  <p>Also...how do I tell it to print out the line that that word was on?</p>
  
  <p>Thanks in advance!</p>