title: Finding all instances of a word in a string in C++
body: <p>So I am trying to make a program that gets an input from a text file, and has the user enter the term they want to search for. From there, the file should be printed out with 3 asterisks in front and behind all of the terms.</p>
  
  <p>It works currently, but it only finds the first term in the file, I want it to find all terms. Any help?</p>
  
  <p>Thanks</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cassert&gt;
  #include &lt;cstdio&gt;
  
  using namespace std;
  
  int main(void)
  {
      int a, i, z, loc;
      string inputFileName;
      string s, term;
      ifstream fileIn;
      char ch;
      cout &lt;&lt; "Enter the name of a file of characters: ";
      cin &gt;&gt; inputFileName;
      fileIn.open(inputFileName.data());
      assert(fileIn.is_open() );
      i = 0;
  
      while (!(fileIn.eof()))
      {
          ch = fileIn.get();
          s.insert(i, 1, ch);     //insert character at position i
          i++;
      }
  
      cout &lt;&lt; s;
      cout &lt;&lt; "Enter a word/phrase you want to search for in the file you entered" &lt;&lt; endl;
      cin &gt;&gt; term;
      cout &lt;&lt; "The word/phrase " &lt;&lt; term &lt;&lt; " will have '***' before it and after it" &lt;&lt; endl;
      z = (int)term.length();
      loc = s.find(term);
  
      s.insert(loc, 1, '*');
      s.insert(loc+1, 1, '*');
      s.insert(loc+2, 1, '*');
  
      s.insert(loc+3+z, 1, '*');
      s.insert(loc+4+z, 1, '*');
      s.insert(loc+5+z, 1, '*');
      cout &lt;&lt; s;
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Text output</strong><br>
  Enter the name of a file of characters: text.txt</p>
  
  <p>Repair- Determined by the difficulty of the fix, and the parts needed in order for it to work.</p>
  
  <p>Enter a word/phrase you want to search for in the file you entered </p>
  
  <p>the</p>
  
  <p>The word/phrase by will have '***' before it and after it</p>
  
  <p>Repair- Determined by <strong><em>the</em></strong> difficulty of the fix, and the parts needed in order for it to work.</p>