Indenting Paragraph With cout
Posted
by
Eric
on Stack Overflow
See other posts from Stack Overflow
or by Eric
Published on 2011-03-12T05:49:32Z
Indexed on
2011/03/12
16:10 UTC
Read the original article
Hit count: 257
Given a string of unknown length, how can you output it using cout so that the entire string displays as an indented block of text on the console? (so that even if the string wraps to a new line, the second line would have the same level of indentation)
Example:
cout << "This is a short string that isn't indented." << endl;
cout << /* Indenting Magic */ << "This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line..." << endl;
And the desired output:
This is a short string that isn't indented.
This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line...
Edit: The homework assignment I'm working on is complete. The assignment has nothing to do with getting the output to format as in the above example, so I probably shouldn't have included the homework tag. This is just for my own enlightment.
I know I could count through the characters in the string, see when I get to the end of a line, then spit out a newline and output -x- number of spaces each time. I'm interested to know if there is a simpler, idiomatic C++ way to accomplish the above.
© Stack Overflow or respective owner