Printing Multiple Lines of Text with a Single Statement

A single statement can print multiple lines by using newline characters, as in line 8 of Fig. 2.4. Each time the \n (newline) escape sequence is encountered in the output stream, the screen cursor is positioned to the beginning of the next line. To get a blank line in your output, place two newline characters back to back, as in line 8.

 1   // Fig. 2.4: fig02_04.cpp 2   // Printing multiple lines of text with a single statement. 3   #include <iostream> // allows program to output data to the screen 4  5   // function main begins program execution 6   int main() 7   { 8      std::cout << "Welcome \n to\n\n C++!\n"; 9   } // end function main

WelcometoC++! ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.