Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Advancing to the Next Print Line

With C++, text sent to the screen does not automatically advance to the next physical line. You have to print a newline character to do that. (Exception: If you never print a newline, the text may automatically “wrap” when the current physical line fills up, but this produces an ugly result.)

The easiest way to print a newline is to use the predefined constant endl. For example:

cout << "Never fear, C++ is here!" << endl;

Another way to print a newline is to insert the characters \n. This is an escape sequence, which C++ interprets as having a special meaning rather than interpreting it literally. The following statement has the same effect as the previous example:

cout << "Never fear, C++ is here!\n";

Example 1.2. Print Multiple Lines

The program in this section prints messages across several lines. If you’re following along and entering the programs, remember once again to use uppercase and lowercase letters exactly as shown—although you can change the capitalization of the text inside quotation marks and the program will still run.

If you’re working with Visual Studio, the only lines you should add are the ones shown here in bold. Leave #include stdafx.h and _tmain alone. If you’re working with another compiler, the code should look as follows, minus the comments (//).

print2.cpp

// If you're using Microsoft V.S. leave in this line:
// #include "stdafx.h"

#include <iostream>
using namespace std;

int main()
{
     cout << "I am Blaxxon," << endl;
     cout << "the godlike computer." << endl;
     cout << "Fear me!" << endl;
     return 0;
}

Remember that exact spacing does not matter, but case-sensitivity does.

The resulting program, if you’re working with Visual Studio, should be as follows. The lines in bold are what you need to add to the code Visual Studio provides for you.

#include "stdafx.h"

#include <iostream>
using namespace std;

int _tmain(int arg, _TCHAR* argv[])
{
     cout << "I am Blaxxon," << endl;
     cout << "the godlike computer." << endl;
     cout << "Fear me!" << endl;
     return 0;
}

After entering the program, compile and run it. Here’s what the program prints when correctly entered and run:

I am Blaxxon,
the godlike computer.
Fear me!

How It Works

how.jpg

This example is similar to the first one I introduced. The main difference is this example uses newline characters. If these characters were omitted, the program would print

I am Blaxxon, the godlike computer.Fear me!

which is not what we wanted.

Conceptually, here’s how the statements in the program work:

You can print any number of separate items this way, though again, they won’t advance to the next physical line without a newline character (endl). You could send several items to the console with one statement

cout << "This is a " << "nice " << "C++ program.";

which prints the following when run:

This is a nice C++ program.

Or, you can embed a newline, like this

cout << "This is a" << endl << "C++ program.";

which prints the following:

This is a
C++ program.

The example, like the previous one, returns a value. “Returning a value” is the process of sending back a signal—in this case to the operating system or development environment.

You return a value by using the return statement:

return 0;

The return value of main is a code sent to the operating system, in which 0 indicates success. The examples in this book return 0, but they could return an error code sometimes (−1 for example) if you found that to be useful. However, I would ignore that for now.

Exercises

exerc.jpg

Exercise 1.2.1. Remove the newlines from the example in this section, but put in extra spaces so that none of the words are crammed together. (Hint: Remember that C++ doesn’t automatically insert a space between output strings.) The resulting output should look like this:

I am Blaxxon, the godlike computer. Fear me!

Exercise 1.2.2. Alter the example so that it prints a blank line between each two lines of output—in other words, make the results double-spaced rather than single-spaced. (Hint: Print two newline characters after each text string.)

Exercise 1.2.3. Alter the example so that it prints two blank lines between each of the lines of output.

  • + Share This
  • 🔖 Save To Your Account