Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:35:59+00:00 2026-05-29T07:35:59+00:00

I’m trying to wrap the words of a given string and display it in

  • 0

I’m trying to wrap the words of a given string and display it in a message box using SFML (<– this doesn’t matter ).

So, what I am trying to do is to insert a new line before a word that is greater than the width of the message box. Here’s the code:

string& message::word_wrap( string& msg , int w, int pt_size )
{
    int text_width = w;
    vector< string > lines;
    string temp ( msg );
    temp += " ";
    int n = 0, p = 0;

    while ( n != -1 )
    {
        string substr;
        n = temp.find( " ", p+1 );
        if ( (n * pt_size) >= text_width )
        {
            substr = temp.substr( 0, p );
            lines.push_back( substr );
            if ( n != -1 )
                temp = temp.substr( p+1, string::npos );
            p = 0;
        }

        else p = n;
    }

    string rtn;
    for ( int i = 0; i < lines.size(); i++ ) rtn += lines[i] + '\n';
    return rtn;
}

I use it in my class like this:

message::message( sf::RenderWindow& App, const string& msg, int w, int h ) : app(App)
{
    /* irrelevant code */

    string m = msg; // <--
    m = this->word_wrap( m, w, text.GetSize() ); // <--
    text.SetText(m);
}

There are two problems.

  1. The algorithm wraps the words way before they exceed the width of
    the message box.
  2. The last few words are skipped for some reason, when I use
    it like this:

    pro::message::display( App, "Hello World! Yada Yada Bla Bla Yda Yada Hehehe Hehe Hfmaf Heh last few words" );
    

It skips the last word, words :

last few words get skipped

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:36:00+00:00Added an answer on May 29, 2026 at 7:36 am

    The easy solution is to make a copy of the string, and replace the space with a newline when appropriate.

    The problem you are having, is because when you find the last space where you should to a linebreak, you still have text after that that is not pushed into the vector. You need to check if there are more text after the loop, and add that as a separate line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.