C++ block of text to cout

Heylo,
I am trying to write a simple program (in/output to terminal) but at some point it must print a large block of text. I want all the lines of the block to be indented by 1 and all the end of lines also indented by 1 (in short a right and left margin of 1).

How do i do this? i tried setw but it seems its only for padding...

Thanks
Output a space at the beginning.
cout << " " << whatever << endl;
setw() only sets the width of the field of output.

http://www.cplusplus.com/reference/iostream/manipulators/setw/

And AFAIK, there is no simple way of formatting block text in console. You can write a function which splits the block into smaller length strings.

Say I have a console with width 80 chars. I would break the large text into chunks of length 78 each. Then I would simply

1
2
for (int i = 0; i < number_of_chunks; i++)
cout << ' ' << chunks[0] << endl;


And if you don't know the width of your console, look into its properties. Where the properties are, depends on what OS you use.

The difficulty of the problem changes as you bring more complex objective into it. For example, if you have to perform word wrap, you will have to incorporate some mechanism to check if each end of chunk ends with a full word. :\
Last edited on
Topic archived. No new replies allowed.