Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 2.1 More Output

As I mentioned in the last chapter, you can put as many statements as you want in main. For example, to output more than one line:
Listing 2.1.1. This program prints two different statements on two different lines using endl.
As you can see, it is legal to put comments at the end of a line, as well as on a line by themselves.
The phrases that appear in quotation marks are called strings, because they are made up of a sequence (string) of letters.

Note 2.1.1.

In C++, strings are declared as type string. We’ll explain what that means in the next few pages.
Actually, strings can contain any combination of letters, numbers, punctuation marks, and other special characters.
Often it is useful to display the output from multiple output statements all on one line. You can do this by leaving out the first endl:
Listing 2.1.2. This program prints two different statements on the same line.
In this case the output appears on a single line as Goodbye, cruel world!. Notice in main that there is a space between “Goodbye,” and the second quotation mark. This space appears in the output, so it affects the behavior of the program.
Spaces that appear outside of quotation marks generally do not affect the behavior of the program. For example, I could have written:
Listing 2.1.3. This program accomplishes the same thing as the one above. The difference is that there are no spaces separating the different components of each line.
This program would compile and run just as well as the original. The breaks at the ends of lines (newlines) do not affect the program’s behavior either, so I could have written:
Listing 2.1.4. This program accomplishes the same thing as the two above, but it only uses one line. However, this format is pretty messy and relatively hard to follow.
That would work, too, although you have probably noticed that the program is getting harder and harder to read. Newlines and spaces are useful for organizing your program visually, making it easier to read the program and locate syntax errors.

Checkpoint 2.1.1.

Checkpoint 2.1.2.

On how many separate lines will the 7’s be printed?
#include <iostream>
using namespace std;

int main() {
  cout << 7 << endl;
  cout << 7;
  cout << 7;
  cout << 7;
}
  • There is an "endl" statement, implying that a new line is created.
  • "endl" creates one new line. The first line will say 7, while the second will print 777.
  • In C++, you must make sure to say "endl" every time you’d like to create a new line.
  • In C++, you must make sure to say "endl" every time you’d like to create a new line.

Checkpoint 2.1.3.

Construct a main function that prints “Snap!” on the first line, “Crackle!” on the third line, and “Pop!” on the sixth line. You might not use all of endl blocks provided.

Checkpoint 2.1.4.

Construct a main function that prints “Hello, world!” so that “Hello,” and “world!” are printed on two separate lines.
You have attempted 1 of 9 activities on this page.