Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>lastSpace</code> iterator starts on the first character of <code>sentence</code>:</p> <pre><code>//this iterator = it when you reach a space; will place a newline here //if you reach width; std::string::iterator lastSpace = sentence.begin(); </code></pre> <p>When you reach the fifth character of "this is a..." (the space), the inner while loop exits (because <code>distanceToWidth == width</code>), before it is recognized that the current character is a space. Then a newline is inserted in position <code>lastSpace</code>, which still points to the first character of the string. This way the "t" of "this" is lost.</p> <p>Next <code>distanceToWidth</code> is reset to zero and another <code>width</code> characters are appended, although the line was not split at the current position, but some characters before (at <code>lastSpace</code>). So this line can end up containing more characters than expected. In the example, "is" is still on the same line as "this" while it should be wrapped to the next line.</p> <p>You probably need to:</p> <ul> <li>change the condition of the inner while to <code>&lt;=</code> so that the correct width is checked</li> <li>not initialize <code>lastSpace</code> to the first character of the string. Probably better:</li> </ul> <p><sub></sub></p> <pre><code>std::string::iterator lastSpace; ... if (lastSpace) { *lastSpace = '\n'; } </code></pre> <ul> <li>count how many characters were found since the last space, and uses this to reset <code>distanceToWidth</code> after inserting a line break</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload