title: C++ function that returns string doesn't work unless there's an endl involved...?
body: <p>I've got a function inside of a class that returns a string. Inside this function, I can only get it to work when I add  <code>cout&lt;&lt;endl</code>  to the function before the return statement. Any idea why this is, or how I can fix it? I'm running this in Eclipse on a Mac</p>
  
  <p>In "main.cpp":</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;cstdlib&gt;
  #include "Braid.h"
  using namespace std;
  
  static int size=3;
  
  int main(){  
  Braid * b1 = new Braid(size);
  b1-&gt;setCanon();//creates canonical braid. 
  
  cout&lt;&lt;"a ";
  cout&lt;&lt;b1-&gt;getName()&lt;&lt;endl;
  cout&lt;&lt;" b ";
  }  
  </code></pre>
  
  <p>In "Braid.h" :  </p>
  
  <pre><code>  public:  
          Braid(int);  
          void setCanon();  
          string getName(); 
      };  
  </code></pre>
  
  <p>And in "Braid.cpp":  </p>
  
  <pre><code>string Braid::getName(){  
      string sName="";  
  
      /* body commented out
      for(int i=0; i&lt;height; i++)
      {
          for(int j=2; j&lt;(width-2); j++)
              {
                  sName += boxes[i][j];
                  sName += "|";
              }
          }
  
          */  
          //cout&lt;&lt;endl;
          return sName;
      } 
  </code></pre>
  
  <p>When I run my main code, without the body of that function commented, the output I get is<br>
   <strong>"a 0|0|12|12|0|0|2|1|1|1|1|2|"</strong>  </p>
  
  <p>The "name" it returns is correct, but it's not making it past the function call. If I uncomment the <code>//cout&lt;&lt;endl</code> line, the function works and my output is<br>
   <strong>"a 0|0|12|12|0|0|2|1|1|1|1|2|<br>
  b "</strong></p>
  
  <p>After commenting out the body of the function, so that it only creates an empty string, and returns it, my output is only "a"  then if I add the endl back, I get the "a b" that is expected.</p>
  
  <p>What am I doing wrong? Is there something that comes with endl that I'm missing?</p>