Section 1.4 Communication through console. | ||
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.
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 << "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 insertion operator (<<) may be used more than once in a same sentence:cout << "Hello"; // prints Hello on screen cout << Hello; // prints the content of Hello variable on screen
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 " << "a C++ sentence";
If we supose that variable age contains the number 24 and the variable zipcode contains 90064 the output of the previous sentence would be:cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;
Hello, I am 24 years old and my zipcode is 90064It 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.";will be shown followed in screen:
cout << "This is another sentence.";
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 ";produces the following output:
cout << "Second sentence.\nThird sentence.";
First sentence.Additionally, to add a new-line, you may also use the endl manipulator. For example:
Second sentence.
Third sentence.
cout << "First sentence." << endl;would print out:
cout << "Second sentence." << endl;
First 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.
Second sentence.
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.
int 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 >> age;
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;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.
cin >> b;
© The C++ Resources Network, 2000-2003 - All rights reserved |
Previous: 1-3. Operators. |
index |
Next: 2-1. Control structures. |