Section 1.4
Communication through console.
cplusplus.com

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.

Output (cout)

The cout stream is used in conjunction with the overloaded operator << (a pair of "less than" signs).
cout << "Output sentence"; // prints Output sentence on screen
cout << 120;               // prints number 120 on screen
cout << x;                 // prints the content of variable x on screen
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 x into 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:
cout << "Hello";      // prints Hello on screen
cout << Hello;        // prints the content of Hello variable on screen
The insertion operator (<<) may be used more than once in a same sentence:
cout << "Hello, " << "I am " << "a C++ 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:
cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;
If we supose that variable age contains the number 24 and the variable zipcode contains 90064 the output of the previous sentence would be:
Hello, I am 24 years old and my zipcode is 90064
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:
cout << "This is a sentence.";
cout << "This is another sentence.";
will be shown followed in screen:
This is a sentence.This is another sentence.
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:
cout << "First sentence.\n ";
cout << "Second sentence.\nThird sentence.";
produces the following output:
First sentence.
Second sentence.
Third sentence.
Additionally, to add a new-line, you may also use the endl manipulator. For example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;
would print out:
First sentence.
Second sentence.
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.

Input (cin).

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:
int age;
cin >> age;
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.

// i/o example
#include <iostream.h>

int main ()
{
  int i;
  cout << "Please enter an integer value: ";
  cin >> i;
  cout << "The value you entered is " << i;
  cout << " and its double is " << i*2 << ".\n";
  return 0;
}
Please enter an integer value: 702
The value you entered is 702 and its double is 1404.

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:

cin >> a >> b;
is equivalent to:
cin >> a;
cin >> b;
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.

© The C++ Resources Network, 2000-2003 - All rights reserved

Previous:
1-3. Operators.

index
Next:
2-1. Control structures.