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 ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.