Wrapping Text

Started by
10 comments, last by Legend097 19 years, 7 months ago
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.
Advertisement
Can somebody please help me with this?
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.
if it's a console program, it should be 80 columns wide and 24 rows high.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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.
Bump...
#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!
Thanks! Much appreciated.

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).

enum Bool { True, False, FileNotFound };
This may be a stupid question but where do I insert the text on that code?

This topic is closed to new replies.

Advertisement