4

I do not understand logic in following expression, although it works perfectly:

cout << left << setw(6) << "hello" << "there." ; 

The previous code correctly outputs what I expect: hello there.

My logic is:

cout << "hello" << left << setw(6) << "there."; 

But it outputs something unexpected: hellothere. My expectation is that first character "t" of "there" be at 7th column on output area that is after 6 columns width. In other words my concept is that "left setw(n)" should mean "n columns (spaces) from first one on output area", like some data forms with numbered columns for easy find data.

Could you please explain?

  • 1
    Is this only research you have done so far? – Saleem Apr 20 '16 at 12:22
  • What problem are you trying to solve? How does the code execution differ from your expectations? – Kerrek SB Apr 20 '16 at 12:23
  • Mr Kerrek many thanks for your question. First sentence (its logic I can not understand) output is what I expect [hello there.], but second (my logic)'s, is not [hellothere.] – George Theodosiou Apr 20 '16 at 12:29
  • 1
    Can you please edit your question to say all those things? I.e. say what you expected, what happened instead, and why you think it shouldn't... the question should be self-contained, if you see what I mean. – Kerrek SB Apr 20 '16 at 12:35
  • 4
    Please, please, cannot we have a question with a positive score in c++ ? This is a legitimate question from someone **just learning** c++. – fjardon Apr 20 '16 at 12:52
  • Mr fjardon, many thanks for your statement: "This is a legitimate question from someone just learning c++". – George Theodosiou Apr 26 '16 at 14:53

2 Answers2

5

The setw iostreams manipulator applies to the next item that is output, and to that item only. So in the first snippet, "hello" is modified with "left, field width 6", and thus produces the following output:

|h|e|l|l|o| |

The filler character by default is the space (' '), which is what's output when there is no more input data and the field width hasn't been reached yet.

In the second snippet, only the item "there." is manipulated. Since it already consists of six output characters, the manipulators have no effect.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

The manipulators change the state of the ostream. So they should applied before the stream is asked to output something.

In your case you ask:

  • format next output left
  • format next outputs with a size of 6 characters
  • output 'hello'

So the stream outputs the word "hello" followed by 1 space to reach the size 6, as asked by the manipulator sequence.

You can find references there:

And I quote the most important sentences std::left + operator << :

if (os.flags() & ios_base::adjustfield) == ios_base::left, places os.width()-str.size() copies of the os.fill() character after the character sequence

And std::setw:

The width property of the stream will be reset to zero (meaning "unspecified") if any of the following functions are called:

Input
   operator>>(basic_istream&, basic_string&)
   operator>>(basic_ostream&, char*)

Output
  Overloads 1-7 of basic_ostream::operator<<() (at Stage 3 of num_put::put())
  operator<<(basic_ostream&, char) and operator<<(basic_ostream&, char*)
  operator<<(basic_ostream&, basic_string&)
  std::put_money (inside money_put::put())
  std::quoted (when used with an output stream)
fjardon
  • 7,921
  • 22
  • 31
  • Mr fjardon, thanks for your answer. Let me say you that you correctly stated in previous message that I'm someone just learning c++. Then what you suggest me to read - and understand - is american chinese for me. I would you please suggest me some tutorial online. For the time being I try undertstnad c++ with the help of "http://www.cplusplus.com/doc/tutorial/ ". Regards. – George Theodosiou Apr 29 '16 at 08:48
  • @GeorgeTheodosiou I'm sorry my answer doesn't help you. Unfortunately, I didn't learn C++ online so I cannot recommend any beginner's resources. There is a question on SO about the [recommended books for C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Maybe can you find some of these books (or translation) in a library near you. – fjardon Apr 29 '16 at 09:07
  • Mr fjardon, many thanks for your recommendation. already I have begun reading at https://books.google.fr/books?id=J1HMLyxqJfgC&printsec=frontcover&dq=C%2B%2B+Primer+%285th+Edition%29&hl=fr&sa=X&redir_esc=y#v=onepage&q&f=true – George Theodosiou Apr 29 '16 at 09:58
  • Regarding c++ Primer, pdf at https://breezeflutter.files.wordpress.com/2013/06/c_primer_5th_edition.pdf is much better than google book I mentioned in previous message. By the way in page 39 exercise 1.11 "Print each number in the range specified by those two integers". What it means? Regards. – George Theodosiou May 04 '16 at 09:04