setw

I'm confused on this. if string kitty = "Flat Cat"

cout << setw(10) << kitty << endl;

How should this turn out?
spacespaceFlat Cat

It will display "Flat Cat" using 10 characters - by default it will use 2 leading spaces as the text "Flat Cat" is
8 characters long - 2 less than the specified 10 characters.
Last edited on
as described above the format is right justified unless a minus symbol is inserted in which is becomes left justified.

if your string were
1
2
3
string kitty = "Hello Kitty"

cout << setw(10) << kitty << endl;


Because the string is 11 characters long, it displays the whole string even past the point of the setw(10).

if code is set to
1
2
3
string kitty = "Hello Kitty"

cout << setw(100) << kitty << endl;


then you get 89 leading spaces, if you use:

1
2
3
string kitty = "Hello Kitty"

cout << setw(-100) << kitty << endl;


then Hello Kitty is displayed left justified with 89 spaces behind it.
Topic archived. No new replies allowed.