title: c++ find repeated substring within string
body: <p>Im trying to find the number of times a substring repeats within a string input but for some reason when I call the function it gives me a weird number. I have already tested the function within main and it works fine but when I make a standalone function it doesn't work.</p>
  
  <p>Thank you in advance</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int checkHope(string word1)
      {
      int answer;
      int counter;
      for(int i = 0; word1[i] != '\0'; i++)
      {
          answer = word1.find("h", i);
          if ((word1.find("o", (answer+1)) == i+1) &amp;&amp; (word1.find("e", (answer+3)) == i+3)) counter++;
      }
      return counter;
      }
  
  int main()
  {
      string word1;
  
      cout &lt;&lt; "Please enter a word to check how many times the word \"hope\" appears. You can also have any letter instead of p.: ";
      getline(cin, word1);
      cout &lt;&lt; checkHope(word1);
  
      return 0;
  }
  </code></pre>