< previous page page_107 next page >

Page 107
Hi there,

Lois Lane.
Whenever you use an endl immediately after another endl, a blank line is produced. As you might guess, three consecutive uses of endl outputs two blank lines, four consecutive uses outputs three blank lines, and so forth.
Note that we have a great deal of flexibility in how we write an output statement in a C++ program. We could combine the three preceding statements into two statements:
cout << "Hi there, " << endl << endl;
cout << "Lois Lane." << endl;
In fact, we could do it all in one statement. One possibility is
cout << "Hi there, " << endl << endl << "Lois Lane." << endl;
Here's another:
cout << "Hi there, " << endl << endl
     << "Lois Lane." << endl;
The last example shows that you can spread a single C++ statement onto more than one line of the program. The compiler treats the semicolon, not the physical end of a line, as the end of a statement.
Inserting Blanks Within a Line
To control the horizontal spacing of the output, one technique is to send extra blank characters to the output stream. (Remember that the blank character, generated by pressing the space bar on a keyboard, is a perfectly valid character in C++.)
To prevent the output of 15, 2, and 6 from looking like this:
Results: 1526
you could print a single blank (as a char constant) between the numbers:
cout << "Results: " << i << ' ' << j << ' ' << k;

 
< previous page page_107 next page >