Jump to content

Paragraph Formatting in C++, help!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
biciclub

biciclub

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, I'm new to programming forums of any kind, but I have a question which I can't seem to find the answer to from search engines or C++ reference books.

I'm about 1500 lines into a text-based game I'm designing. It's pretty functional already, has a decent english parser, good error checking, implements synonyms well, phrasal verbs, and can handle some non-linear events like dream sequences, changing environments, etc. I've been using cygwin/X and emacs on Windows XP. I've purposely eschewed looking at other text-based game code for the challenge and to home my own nascent programming skills.

But my question is pretty basic: how do I format the output strings so that the words will wrap from line to line like in a normal word processor, rather than just getting chopped in half at the end of the console? Ideally I'd like something I can code in at the beginning that will be broadly applicable, not just in main() but in all the functions as well.

I'd rather not add endline commands within the strings since that would take forever and also if I changed my mind on the width I'd have to redo it all. cout.setw() doesn't seem applicable here either. The only other thing I can think of is overloading the << operator and coding something in there, but that's not something I'm used to or familiar with.

In case it matters the output strings are in two basic areas; the more general, short strings are within the various verb functions I've set up. The more specific, longer strings are member variables of two classes: item objects and room objects.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I think the reason you're having trouble is because of the specific nature of what you're trying to do. Many GUI libraries, for example, have ways to handle word-wrap in text fields, but each one is different. When working with cout, there is no assumption about "window width" that can be applied to it. What you may want to look at is creating a function that will automatically do wrapping for you. You pass it a string and a max length, and let it dynamically insert newlines at the word breaks for you. You'll probably want to investigate some of the algorithms in the STL to keep your code efficient.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
biciclub

biciclub

    Newbie

  • Members
  • Pip
  • 4 posts
well I don't think I'd have too much trouble developing that kind of function, but if I left it as a standard "string wordWrap(string) {}" type function then I'd still have to go and change every instance of cout in the program to a different, clunkier syntax. Don't get me wrong, I like the idea, but is there some way to overload the << operator so that it does both tasks of formatting and outputting?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You could wrap your string inside another class and overload << for that class using the same logic as the function.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
biciclub

biciclub

    Newbie

  • Members
  • Pip
  • 4 posts
Hi,

Thought I'd post the code as an update. Instead of overloading the << operator, I decided instead to format the strings as they were input into their respective classes, with a private member function. It still doesn't apply to 100% of the strings in the program, but gets the vast majority.

Thanks again for the help.
string item::wordWrap(string a)
{
  const int WIDTH = 70; // may take this line out if I implement global width
  int index;
  string wrapped = "";
  while(a.length()>=WIDTH)
    {
      index = a.substr(0,WIDTH).find_last_of(" ");
      wrapped.append(a.substr(0,index));
      wrapped.append("\n");
      a = a.substr(index+1);
    }
  wrapped.append(a);
  return wrapped;
}

Edited by WingedPanther, 02 July 2009 - 05:47 AM.
add code tags (the # button)