Console Input / Output in C++ – Programming, Pseudocode Example, C# Programming Example

Console Input / Output in C++

The console is the basic interface of computers, normally it is the set composed of the keyboard and the screen. The keyboard is generally the standard input device and the screen the standard output device.

In the iostream C++ library, standard input and output operations for a program are supported by two data streams: cin for input and cout for output. Additionally, cerr and clog have also been implemented – these are two output streams specially designed to show error messages. They can be redirected to the standard output or to a log file.

Therefore cout (the standard output stream) is normally directed to the screen and cin (the standard input stream) is normally assigned to the keyboard.

By handling these two streams you will be able to interact with the user in your programs since you will be able to show messages on the screen and receive his/her input from the keyboard.

Console Output in C++

The cout stream is used in conjunction with the overloaded operator << (a pair of “less than” signs).


The << operator is known as insertion operator since it inserts the data that follows it into the stream that precedes it. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and the variable xinto the output stream cout. Notice that the first of the two sentences is enclosed between double quotes (") because it is a string of characters. Whenever we want to use constant strings of characters we must enclose them between double quotes (") so that they can be clearly distinguished from variables. For example, these two sentences are very different:


The insertion operator (<<) may be used more than once in a same sentence


this last sentence would print the message Hello, I am a C++ sentence on the screen. The utility of repeating the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more than one variable:


If we supose that variable age contains the number 24 and the variable zipcode contains 90064 the output of the previous sentence would be:


It is important to notice that cout does not add a line break after its output unless we explicitly indicate it, therefore, the following sentences:

will be shown followed in screen:


even if we have written them in two different calls to cout. So, in order to perform a line break on output we must explicitly order it by inserting a new-line character, that in C++ can be written as \n:


produces the following output:


Additionally, to add a new-line, you may also use the endl manipulator. For example:


would print out:


The endl manipulator has a special behavior when it is used with buffered streams: they are flushed. But anyway cout is unbuffered by default.

You may use either the \n escape character or the endl manipulator in order to specify a line jump to cout. Notice the differences of use shown earlier.

Console Input  in C++

Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. This must be followed by the variable that will store the data that is going to be read. For example:


declares the variable age as an int and then waits for an input from cin (keyborad) in order to store it in this integer variable.

cin can only process the input from the keyboard once the RETURN key has been pressed. Therefore, even if you request a single character cin will not process the input until the user presses RETURN once the character has been introduced.

You must always consider the type of the variable that you are using as a container with cin extraction. If you request an integer you will get an integer, if you request a character you will get a character and if you request a string of characters you will get a string of characters.

The user of a program may be one of the reasons that provoke errors even in the simplest programs that use cin (like the one we have just seen). Since if you request an integer value and the user introduces a name (which is a string of characters), the result may cause your program to misoperate since it is not what we were expecting from the user. So when you use the data input provided by cin you will have to trust that the user of your program will be totally cooperative and that he will not introduce his name when an interger value is requested. Farther ahead, when we will see how to use strings of characters we will see possible solutions for the errors that can be caused by this type of user input.

You can also use cin to request more than one datum input from the user:

is equivalent to:

In both cases the user must give two data, one for variable a and another for variable b that may be separated by any valid blank separator: a space, a tab character or a newline.



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: