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.