Jump to content
  • Advertisement
Sign in to follow this  
NewbJ

Working with text

This topic is 5156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I am once again wrestling with how to most efficiently deal with text (splitting it into multiple lines and all) in a C++ program I am writing. I have written this function to do that:
  int totalLength = strlen(myString);

  int totalSpaces = 0;
  int spacePos = 35;
  for ( int line=0; line<7; line++ )  //there shouldn't be more than 6 lines
  {
    std::cout << "Line iteration " << line << "\n";

    for ( int character=spacePos-1; character>=0; character-- )
    {
      std::cout << ":" << character+(totalSpaces*line) << myString[character+(totalSpaces*line)] << ":";
      if ( (myString[character+(totalSpaces*line)] == ' ') || (myString[character+(totalSpaces*line)] == '\0') )
      {
        spacePos = character; //where the first space is before the max # of chars
        std::cout << "\n";
        std::cout << "spacePos set to " << spacePos << "\n";
        break;
      }
    }

    std::cout << "totalSpaces is " << totalSpaces << "\n";
    char tempString[35]; //holds the current line
    for ( int position=0; position <= 34; position++ ) //putting the line into the string...
    {
      tempString[position] = myString[position+totalSpaces];
      std::cout << "Written: MyString: " << position+totalSpaces << ": " << myString[position+totalSpaces] << "\n";
      if ( position == spacePos )
      {
        std::cout << "SPACE FOUND; NEWLINE\n";
        tempString[position] = '\0'; //write the end of the current line
        break;
      }
    }
    drawString(screen, font, 95, (250+(line*50)), tempString);

    totalSpaces += spacePos;

    if ( (totalLength - totalSpaces) <= 0 )
    {
      std::cout << "totalLength - totalSpaces = " << totalLength - totalSpaces << "\n";
      break;
    }
  }
it works for most text sequences, but not for certain ones, like this one: Journeymen were the most inexperienced craftsmen in a guild. for this, it displays the first like properly, then parses the remaining text into way too many lines, with only a few characters on each line, or into just 1 too many, and I can't seem to find the bug. can anyone spot it and/or have any suggestions for the code? thanks!

Share this post


Link to post
Share on other sites
Advertisement
Instead of:
myString[character+(totalSpaces*line)]

try using
myString[character+totalSpaces]

on both occasions, that might be the problem?

Ohh and i think you'll need to reset your "spacePos" to 35 each loop, you haven't added that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!