Working with text

Started by
1 comment, last by NewbJ 19 years, 6 months ago
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!
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.
-----------------Live for the LordRide for the Lordwww.sturmnacht.de.vu
Thanks, that did it!

This topic is closed to new replies.

Advertisement