Concatenating with cout

The second new feature of getinfo.cpp is combining four output statements into one. The iostream file defines the << operator so that you can combine (that is, concatenate) output as follows:

cout << "Now you have " << carrots << " carrots." << endl;

This allows you to combine string output and integer output in a single statement. The resulting output is the same as what the following code produces:

cout << "Now you have ";
cout << carrots;
cout << " carrots";
cout << endl;

While you’re still in the mood for cout advice, you can also rewrite the concatenated version this way, spreading the single statement over four lines:

cout << "Now you have "
     << carrots
     << " carrots."
     << endl;

That’s because C++’s free format rules treat newlines and spaces between tokens interchangeably. This last technique is convenient when the line width cramps your style.

Another point to note is that

Now you have 14 carrots.

appears on the same line as

Here are two more.

That’s because, as noted before, the output of one cout statement immediately follows the output of the preceding cout statement. This is true even if there are other statements in between.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190