Chapter 2: Console Input and Output

Download CD Content

The previous chapter introduced you to some basics of C++ programming. This chapter covers another vital part of C++ programming: getting input from the user and displaying the results. This chapter concentrates on getting that input and providing the required output. This chapter focuses on doing this from some command line such as the Windows DOS prompt/command prompt, or a Unix/Linux shell. This means that you will learn how to take in what the user types, and to display results in a text format, back to the user. Obviously a program that does not take in input, or give back output, is ultimately useless, so please pay particular attention to this chapter.

Output to the screen

You will often want to provide the user with some type of text. It might be a message to the user, a request for input, or the results of some data that the program has computed. How do you take data from your program and convert it into some representation on the screen? Luckily for you, C++ provides some functions that handle screen output for you. All you have to do is learn how to use these functions. These functions are found in one of the header files that was mentioned in Chapter 1. The particular file you need to include is the iostream file. You include this by simply placing the following line at the top of your source code file.

#include <iostream>

Recall from Chapter 1 that, when you include a header file, you have access to all the functions defined in that file. By including this file you will have access to a number of functions for input and output. The two most important functions are cout and cin. Virtually all your input and output needs (at least for keyboard input and screen output) can be handled by these two functions. The first, cout, is how you display data to the screen. The following example shows the ubiquitous “hello world” program. (It seems like every programming book uses this example, so who are we to argue with tradition?)

Example 2.1

Step 1: Enter the following code into your favorite text editor then save the file as example02-01.cpp.

#include <iostream> using std::cout;   using std::cin; int main() {   // Print hello world on the screen   cout << "Hello World";   return 0; } 

Step 2: To compile this, simply type the code into a text file and save it as hello.cpp. Then you will run your command line compiler by typing in bcc32 hello.cpp. If you typed in the code properly you will then have a hello.exe, that you can run any time you wish.

Step 3: Run the executable you just created. When you run it, it should look similar to Figure 2.1. (This image is from the command prompt of a Windows 2000 professional machine. A Linux shell would look a little bit different, but would be essentially the same concept.)

click to expand
Figure 2.1: Hello World.

This may not be a particularly exciting program, but it does illustrate the basics of screen output. Notice that after we included <iostream> we also had two strange-looking lines of code.

using std::cout; using std::cin;

cout and cin are both defined inside of the iostream header file. You have to tell your program which parts of iostream you wish to use. These two lines of code tell the compiler that you wish to use cout and cin. (cin will be described in detail later in this chapter.)

The cout command tells the C++ compiler to redirect the output to the default display device, usually that’s the monitor. cout is short for “console output.” Notice the <<after the cout. The arrows literally point you in the direction the text will be sent. In the case of cout, the code is sent out of the program, thus the arrows point out of the code! This seems pretty simple so far, and it should. Now, what if we wish to format the code that we output in some special way? For example, when a program is done, the Windows 2000 command prompt (and earlier Windows DOS prompts) adds on the phrase “press any key to continue.” Perhaps you would like to place that on a separate line, to separate it from the output you are producing. That would be the logical thing to do because you do not wish to confuse the user into thinking that “press any key to continue” is your program’s output. Fortunately for you, C++ provides several formatting codes that you can add to any string to format it. For example, the \n code tells the C++ compiler to start a new line. Let’s rewrite the “hello world” program with this addition.

Example 2.2

Step 1: Type the following code into your favorite text editor and save it as example02-02.cpp.

#include <iostream> using std::cout;   using std::cin; int main() {   cout << "Hello World \n";   return 0; }

Step 2: Compile the code by running bcc32 example02-02.cpp.

Step 3: Execute the compiled code by typing example02-02. You should see an image like that depicted in Figure 2.2.

click to expand
Figure 2.2: Hello World 2.

Note that anything after “Hello world” is on a new line. That’s exactly what the \n command means; it means start a new line. There are actually several commands that you can execute in this manner to format the code in any way you like. These codes are often referred to as escape codes. Table 2.1 summarizes most of them for you.

Table 2.1: Escape Codes.

Escape Sequence



New line


Carriage return




Horizontal tab


Single quotation mark


Form feed


Bell (alert)


Vertical tab




Double quotation mark


Literal question mark

As you can see, there is a plethora of options for formatting output to the screen. Throughout this chapter and the next you will see several of these codes used in examples. This table provides a summary of the escape characters you can use. You should recall from Chapter 1 that C++ is built on the framework of C. These formatting keys (also called escape keys) are a prime example. These keys work exactly the same way in C as they do in C++. The following is an example that illustrates everything covered thus far.

Example 2.3

Step 1: Enter the following code into a text editor of your choice and save it as example02-03.cpp.

#include <iostream> using std::cout;   using std::cin; int main() {   // the following code demo's various escape keys   cout << "\" Hello World \" \n";   cout << "C++ is COOL! \n \a";   return 0; }

Step 2: Compile the code by typing in bcc32 example02-03.cpp.

Step 3: Run your code by typing in example02-03.

If you entered the code in properly you will here a beep. (Remember that \a causes a beep.) You will see something similar to the image shown in Figure 2.3.

click to expand
Figure 2.3: Hello World 3.

You should notice several things about this code. First, notice that you can place more than one escape sequence in order. You should notice that this was done in this example. You can use as many escape characters as is necessary in a given string of characters. You should also notice that the way to place quotes inside a string of characters is to use the proper escape character.


If you try to simply put quotes inside of quotes, then your quotes will terminate the string. You must use the escape character. If you think about this, it makes perfect sense. As soon as the C++ compiler sees the quotation marks (if they are not preceded by the \) it will think that your string is now ending.

Finally, you should also notice the “beep” provided by \a. It is often useful to provide the user with audio signals in addition to visual signals.

Using these various escape sequences you manipulate the output of your program in a variety of ways. You can also, as you have already seen, create some audio output. The following example should illustrate this to you.

Example 2.4

Step 1: Enter the following code into your favorite text editor.

#include <iostream> using std::cout; int main() { cout << "As you can see these \" escape keys \" \n"; cout << "are quite \'useful \' \a \\ in your code \\ \a     \n"; return 0;    } 

Step 2: Compile that code.

Step 3: Execute the code. You should see something similar to Figure 2.4.

click to expand
Figure 2.4: Using various escape keys.

These keys give you a wide range of formatting options as well as some sound effects. You will probably find these escape keys quite useful in your various programming projects.

In addition to the escape keys you have already seen, there are some other techniques for manipulating your output. For example, you will frequently see C++ programmers choosing the endl command for a new line at the end of screen output, rather than the escape key \n. To use endl just end your quotation marks, then type the << endl command, and terminate it with a semicolon. The following example demonstrates this.

Example 2.5

Step 1: Enter the following code into your favorite text editor.

#include <iostream> using std::cout;   using std::cin; using std::endl; int main() {  cout << "You have previously used the \\n key to get a     new line \n";  cout << "However you can also use the endl     command"<<endl;   return 0; }

You will notice that two of the lines of code in this sample wrap to the next line. This is simply a book formatting issue. When you type code into your text editor, put everything on one line, up to the semicolon.

Step 2: Compile that code.

Step 3: Execute the code. You should see something similar to what is displayed in Figure 2.5.

click to expand
Figure 2.5: Using endl.

As you can see, the endl is just as useful in creating a new line. From a technical perspective, it also flushes C++’s buffer stream. What that means is that as you send content to the screen via cout, it is placed in a temporary buffer. The endl command causes that buffer to be emptied.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: