简体   繁体   中英

(C++) How can I print a long message without breaking words at the end of a console line?

This is technically two things, but they're essentially the same so I combined them into one question.

I want to print long messages without having to control where a line break is in the text. For example, I wrote a long string and printed it with std::cout << str << std::endl; . Pipes | added here for demonstration purposes showing the end of the console line at the current window size, and an @ sign to show where the text stopped printing when not at the end where the pipe is.

$ ./text_test
This sentence is so long that the word 'developer' splits into two, and the deve|
loper has no clue how this placeholder sentence was a good idea at the time.@   |
$

What I want this example text to look like printed is this:

$ ./text_test
This sentence is so long that the word 'developer' splits into two, and the@    |
developer has no clue how this placeholder sentence was a good idea at the time.|
$

There should also be no space after the 'the' at the end of the first line, because with the right sentence, the space spills over onto the next line, as shown by this example, with the code for it beneath the output:

$ ./long_test
You are lost in the forest and you have nothing on you. How did you end up here?|
 Questions can be answered later; looks like trouble is on its way! Prepare for |
battle! @
$
// here ^ notice how there is a space after the ! sign, when there shouldn't be,
// and also notice the improper space before "Questions".
// The second line should have also ended with 'r', not ' ', as I've marked by
// the lack of an @ sign after "for".
// Source code:

#include <iostream>
#include <string>
#include <sstream>

void lprint (std::string str) {
  std::istringstream ss;
  std::string unit;
  while (ss >> unit) {
    std::cout << unit << " ";
  }
}

int main() {
  std::string str " /* long message */ ";
  std::cout << str << std::endl;
  return 0;
}

I would also like to know (if this is mutually exclusive with what I'm already asking) how to detect the end of a line on-screen, perhaps by the number of columns of text that can be displayed (like 80 etc.). Thank you very much.

You could use a function like

void OutputText(std::string s)
{
    int bufferWidth = GetBufferWidth();
 
    for (unsigned int i = 1; i <= s.length() ; i++)
    {
        char c = s[i-1];
 
        int spaceCount = 0;
 
        // Add whitespace if newline detected.
        if (c == ‘n’)
        {
            int charNumOnLine = ((i) % bufferWidth);
            spaceCount = bufferWidth – charNumOnLine;
            s.insert((i-1), (spaceCount), ‘ ‘);
            i+=(spaceCount);
            continue;
        }
 
        if ((i % bufferWidth) == 0)
        {
            if (c != ‘ ‘)
            {
                for (int j = (i-1); j > -1 ; j–)
                {
                    if (s[j] == ‘ ‘)
                    {
                        s.insert(j, spaceCount, ‘ ‘);
                        break;
                    }
                    else spaceCount++;
                }
            }
        }
    }
 
    // Output string to console
    std::cout << s << std::endl;
 }

Which is further explained on this website .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM