title: Expression: String iterator not dereferencable
body: <p>I'm having a hard time using std::string::iterators in C++. This code compiles fine (still not getting correct output, but that's my fault: TODO, fix algorithm) in Dev-C++, and I don't get runtime errors. The error is with Visual Studio Express 2008 C++, where I'm getting an error pointing to &lt; xstring>: "Expression: string iterator not dereferencable," and points to line 112 of the &lt; xstring> file.</p>
  
  <p>My debugging tells me I might be trying to dereference past the end of the sentence input, but I can't see where. Can anyone shed some light?</p>
  
  <pre><code>std::string wordWrap(std::string sentence, int width)
  {    
      std::string::iterator it = sentence.begin();
  
      //remember how long next word is
      int nextWordLength = 0;
      int distanceFromWidth = width;
  
      while (it &lt; sentence.end())
      {
         while (*it != ' ' &amp;&amp; it != sentence.end())
         {
            nextWordLength++;
            distanceFromWidth--;
            it++;
         }
  
         if (nextWordLength &gt; distanceFromWidth)
         {
            *it = '\n';
            distanceFromWidth = width;
            nextWordLength = 0;
         }
  
         //skip the space
         it++;
  
     }
  
     return sentence;    
  }
  </code></pre>