Basic output (printing) and inpur (text based) in C++
The standard library defines a handful of stream objects that can be used to access what are considered the standard sources and destinations of characters by the environment where the program runs:
stream | description |
---|---|
cin |
standard input stream |
cout |
standard output stream |
cerr |
standard error (output) stream |
clog |
standard logging (output) stream |
Standard output (cout)
On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout
.
For formatted output operations, cout
is used together with the insertion operator, which is written as <<
(i.e., two “less than” signs).
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
Standard input (cin)
In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin
.
For formatted input operations, cin
is 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:
int age;
cin >> age;
Mastery 10 by Mauricio Cooper is licensed under a Creative Commons Attribution 4.0 International License.