|standard input stream|
|standard output stream|
|standard error (output) stream|
|standard logging (output) stream|
cin(the standard output and input streams);
clogare also output streams, so they essentially work like
cout, with the only difference being that they identify streams for specific purposes: error messages and logging; which, in many cases, in most environment setups, they actually do the exact same thing: they print on screen, although they can also be individually redirected.
coutis used together with the insertion operator, which is written as
<<(i.e., two "less than" signs).
<<operator inserts the data that follows it into the stream that precedes it. In the examples above, it inserted the literal string
Output sentence, the number
120, and the value of variable
xinto the standard output stream
cout. Notice that the sentence in the first statement is enclosed in double quotes (
") because it is a string literal, while in the last one,
xis not. The double quoting is what makes the difference; when the text is enclosed between them, the text is printed literally; when they are not, the text is interpreted as the identifier of a variable, and its value is printed instead. For example, these two sentences have very different results:
This is a single C++ statement. Chaining insertions is especially useful to mix literals and variables in a single statement:
I am 24 years old and my zipcode is 90064
This is a sentence.This is another sentence.
\n(i.e., a backslash character followed by a lowercase
n). For example:
endlmanipulator can also be used to break lines. For example:
endlmanipulator produces a newline character, exactly as the insertion of
'\n'does; but it also has an additional behavior: the stream's buffer (if any) is flushed, which means that the output is requested to be physically written to the device, if it wasn't already. This affects mainly fully buffered streams, and
coutis (generally) not a fully buffered stream. Still, it is generally a good idea to use
endlonly when flushing the stream would be a feature and
'\n'when it would not. Bear in mind that a flushing operation incurs a certain overhead, and on some devices it may produce a delay.
cinis used together with the extraction operator, which is written as
>>(i.e., two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For example:
age, and the second extracts from
cina value to be stored in it. This operation makes the program wait for input from
cin; generally, this means that the program will wait for the user to enter some sequence with the keyboard. In this case, note that the characters introduced using the keyboard are only transmitted to the program when the ENTER (or RETURN) key is pressed. Once the statement with the extraction operation on
cinis reached, the program will wait for as long as needed until some input is introduced.
cinuses the type of the variable after the
>>operator to determine how it interprets the characters read from the input; if it is an integer, the format expected is a series of digits, if a string a sequence of characters, etc.
Please enter an integer value: 702 The value you entered is 702 and its double is 1404.
cinseems to make the task of getting input from the standard input pretty simple and straightforward. But this method also has a big drawback. What happens in the example above if the user enters something else that cannot be interpreted as an integer? Well, in this case, the extraction operation fails. And this, by default, lets the program continue without setting a value for variable
i, producing undetermined results if the value of
iis used later.
cinwithout further checking. A little later we will see how stringstreams can be used to have better control over user input.
cincan also be chained to request more than one datum in a single statement:
a, and another for variable
b. Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab, or a new-line character.
cinto get strings of characters in the same way as with fundamental data types:
cinextraction always considers spaces (whitespaces, tabs, new-line...) as terminating the value being extracted, and thus extracting a string means to always extract a single word, not a phrase or an entire sentence.
cin, there exists a function, called
getline, that takes the stream (
cin) as first argument, and the string variable as second. For example:
What's your name? Homer Simpson Hello Homer Simpson. What is your favorite team? The Isotopes I like The Isotopes too!
getline, we used the same string identifier (
mystr). What the program does in the second call is simply replace the previous content with the new one that is introduced.
getlineto obtain input from the user. Therefore, unless you have a strong reason not to, you should always use
getlineto get input in your console programs instead of extracting from
<sstream>defines a type called
stringstreamthat allows a string to be treated as a stream, and thus allowing extraction or insertion operations from/to strings in the same way as they are performed on
cout. This feature is most useful to convert strings to numerical values and vice versa. For example, in order to extract an integer from a string we can write:
stringwith initialized to a value of
"1204", and a variable of type
int. Then, the third line uses this variable to extract from a
stringstreamconstructed from the string. This piece of code stores the numerical value
1204in the variable called
Enter price: 22.25 Enter quantity: 7 Total price: 155.75
cin, we get lines from it into a string object (
mystr), and then we extract the values from this string into the variables
quantity. Once these are numerical values, arithmetic operations can be performed on them, such as multiplying them to obtain a total price.
Statements and flow control