3

So I'm trying to make a little text based game for my first real project in C++. When the game calls for a big block of text, several paragraphs worth, I'm having trouble getting it to look nice. I want to have uniform line lengths, and to do that I'm just having to manually enter line breaks in the appropraite places. Is there a command to do this for me? I have seen the setw() command, but that doesn't make the text wrap if it goes past the width. Any advice? Here is what I'm doing right now if that helps.

cout << "    This is essentially what I do with large blocks of" << '\n';
cout << "    descriptive text. It lets me see how long each of " << '\n';
cout << "    the lines will be, but it's really a hassle, see? " << '\n';            
Hammurabi8
  • 35
  • 1
  • 4
  • 1
    Use the Header file it provides various functions for text formatting –  Feb 16 '15 at 04:54
  • @RohitSaluja , can you be more specific? The only one I see on the list I found would be `setw()`, which I don't think works. Or could you post an example of how you would do this? – Hammurabi8 Feb 16 '15 at 04:58

3 Answers3

2

Getting or writing a library function to automatically insert leading whitespace and newlines suitable for a particular width of output would be a good idea. This website considers library recommendations off topic, but I've included some code below - not particularly efficient but easy to understand. The basic logic should be to jump forwards in the string to the maximum width, then move backwards until you find whitespace (or perhaps a hyphen) at which you're prepared to break the line... then print the leading whitespace and the remaining part of the line. Continue until done.

#include <iostream>

std::string fmt(size_t margin, size_t width, std::string text)
{
    std::string result;
    while (!text.empty())   // while more text to move into result
    {
        result += std::string(margin, ' ');  // add margin for this line

        if (width >= text.size())  // rest of text can fit... nice and easy
            return (result += text) += '\n';

        size_t n = width - 1;  // start by assuming we can fit n characters
        while (n > width / 2 && isalnum(text[n]) && isalnum(text[n - 1]))
            --n; // between characters; reduce n until word breaks or 1/2 width left

        // move n characters from text to result...
        (result += text.substr(0, n)) += '\n';
        text.erase(0, n);
    }
    return result;
}

int main()
{
    std::cout << fmt(5, 70,
        "This is essentially what I do with large blocks of "
        "descriptive text. It lets me see how long each of "
        "the lines will be, but it's really a hassle, see?"); 

}

That hasn't been tested very thoroughly, but seems to work. See it run here. For some alternatives, see this SO question.

BTW, your original code can be simplified to...

cout << "    This is essentially what I do with large blocks of\n"
        "    descriptive text. It lets me see how long each of\n"
        "    the lines will be, but it's really a hassle, see?\n"; 

...as C++ considers the statement unfinished until it hits the semicolon, and double-quoted string literals that appear next to each other in the code are concatenated automatically, as if the inner double quotes and whitespace were removed.

Community
  • 1
  • 1
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • I'm very new to coding and I think that's a little bit above my skill level. I'm terribly surprised that this sort of thing doesn't already exist. – Hammurabi8 Feb 16 '15 at 05:40
  • Sick dude, that works perfectly! I'm going to study it for awhile to figure out how it all works, but I'm really impressed. Thank you for responding so quickly and with such expertise! – Hammurabi8 Feb 16 '15 at 06:24
  • @Hammurabi8: sure - no worries. I added some more comments/explanation - hope that helps too. Cheers. – Tony Delroy Feb 16 '15 at 06:36
1

You can make a tierce simple function

void print(const std::string& brute, int sizeline)
{
     for(int i = 0; i < brute.length(); i += sizeline)
          std::cout << brute.substr(i, sizeline) << std::endl;
}

int main()
{
     std::string mytext = "This is essentially what I do with large blocks of"
                          "descriptive text. It lets me see how long each of "
                          "1the lines will be, but it's really a hassle, see?";

     print(mytext, 20);

     return 0;
}

but words will be cut

output:

This is essentially 
what I do with large
 blocks ofdescriptiv
e text. It lets me s
ee how long each of 
1the lines will be, 
but it's really a ha
ssle, see?
Monir Hadji
  • 119
  • 3
  • 11
1

There are pretty library to handle text formatting, named cppformat/cppformat on github.

By using this, you can manipulate text formatting easily.

yasuharu519
  • 244
  • 2
  • 8