Sign in to follow this  

Wrapping Text

This topic is 4857 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
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
Quote:
Original post by Legend097
This may be a stupid question but where do I insert the text on that code?


it's the first argument in the function call:


void print_width( char const * str, int width )



-me

Share this post


Link to post
Share on other sites

This topic is 4857 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this