Loading lessons...
iostream: cout, cin, and endl
iostream: cout, cin, and endl
C++ talks to the outside world through streams. The header iostream gives us the most famous ones: cout, cin, and their helper endl.
The header
#include <iostream>
using namespace std;
iostream stands for "input/output stream" - one library, both directions.
cout: text going out
cout sends characters out to the console. It uses the insertion operator <<:
cout << "Hello world!";
The arrow points toward the screen: data flows out.
cin: input coming in
cin reads characters from the console. It uses the extraction operator >>:
int age;
cin >> age;
The arrow points from the keyboard into a variable: data flows in.
endl: end the line
endl writes a newline to the console (and flushes the output):
cout << "Done!" << endl;
The two streams together
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Type a number: ";
cin >> number;
cout << "I got " << number << endl;
return 0;
}
cin >>pulls the typed value in.cout <<pushes results back out.- Nothing gets lost along the way.
TL;DR
iostreamis the header with the console streams.cout <<inserts data going out to the screen.cin >>extracts data coming in from the keyboard.endlmoves to a new line and flushes.