str.erase(); doesn't work inside a function
<p>I have a code here that ignores comment lines. The thing is, i tried creating a function with what i had so that i can use it later anywhere within my code. But i am encountering a problem. My code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;
string comment(string str);

int main ()
{
  string str;
  cout &lt;&lt; "Enter the string&gt; ";
  getline( cin, str );
  comment(str);
  cout &lt;&lt; str &lt;&lt; "\n";

  return 0;
}

string comment(string str)
{

  unsigned found = str.find('#');
  if (found!=std::string::npos)
    str.erase(found);
  else
    return(str);           

  return(str);

}
</code></pre>

<p>Sample input:(from keyboard)
    Enter the string> my name is #comment</p>

<p>output i am getting:</p>

<pre><code>my name is #comment
</code></pre>

<p>output i am supposed to get:</p>

<pre><code>my name is
</code></pre>

<p>Also if i use the same code without the function, i get the correct answer. And here it is:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

int main ()
{
  string str;

  cout &lt;&lt; "Enter the string&gt; ";
  getline( cin, str );
  unsigned found = str.find('#');
  if (found!=std::string::npos)
    str.erase(found);


  cout &lt;&lt; str &lt;&lt; "\n";

  return 0;
}
</code></pre>
