Jump to content
  • Advertisement
Sign in to follow this  
Legend097

Wrapping Text

This topic is 5167 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

I am writing a console-based text-only game that have some very long descriptions. I am new to C++ and I wanted to know how to move the whole word to the next line when it doesn't fit instead of just part of the word. Would it be easier to write it in an application window? If so, how would I do that? Thanks for your help.

Share this post


Link to post
Share on other sites
Advertisement
To my knowledge, you're going to need to do it yourself. The process is pretty much that you'll have to keep track of how much is left on the line and the length of the next word. Check if word > space left. If true, reset line counter to line width, newline and print word; else print word, subtract word length from space left.

As to finding the screen width... I've no idea.

Share this post


Link to post
Share on other sites
I understand what you're saying, but how would I go about coding that. I'm a real newb in C++, and basically the only things I can do are use cout and cin, declare variables and such, and call functions. I really appreciate any further help.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string.h>

using namespace std;

void wrapPrint(char* str, int maxwidth)
{
// seps holds the charactors which denote the seperation of 2 words
char seps[] = " ";
char* token;

int currentpos = 0;

// loop while we still have words to print
token = strtok(str, seps);
while(token)
{
// if this word is too big
if(currentpos + strlen(token) > maxwidth)
{
cout << endl;
currentpos = strlen(token);
}
else
{
cout << " ";
currentpos += strlen(token) + 1;
}
cout << token;
token = strtok(NULL, seps);
}
cout << flush;
}

void main(void)
{
char text[] = "I am writing a console-based text-only game that have some very long descriptions. I am new to C++ and I wanted to know how to move the whole word to the next line when it doesn't fit instead of just part of the word. Would it be easier to write it in an application window? If so, how would I do that? Thanks for your help.";
wrapPrint(text, 40);
}


P.S. Make sure that your strings a null terminated, or if you are using std::string, just do this:

std::string str = "something with spaces";
wrapPrint(str.c_str(), maxwidth);


hope that helps!

Share this post


Link to post
Share on other sites

Here's another approach:


void print_width( char const * str, int width ) {
// how much is there to print?
int len = strlen( str );
while( len >= width ) {
// it's invariant that len is the length of string remaining
assert( strlen( str ) == len );
// fit as much as we can, but break at a space or hyphen
char const * end = str + width - 1;
while( *end != ' ' && *end != '-' && end > str ) {
--end;
}
// if there's no space or hyphen at all, print whole line
if( end == str ) {
end = str + len - 1;
}
// make sure the space/hyphen is included
++end;
// actually output
fwrite( str, 1, end-str, stdout );
fwrite( "\n", 1, 1, stdout );
// update to point at start of next line
len -= (end - str);
str = end;
}
// print any stragglers
assert( strlen( str ) == len );
if( *str ) {
fwrite( str, 1, len, stdout );
fwrite( "\n", 1, 1, stdout );
}
}


Note that a really good word-breaker would collapse multiple spaces at line breaks into just the line break; this one doesn't do that (but does, on average, pretty well).

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!