1. Intro
cin and cout are predefined objects of the iostream library used for standard input and standard output in C++. cin reads data typed at the keyboard, and cout displays data on the console. Both are declared in the <iostream> header and live in the std namespace.
Cricket analogy: A stadium's scoring terminal has a predefined input pad for the scorer to type in runs (like cin) and a predefined display board to show the score to the crowd (like cout), both part of the venue's built-in <iostream>-equivalent scoring system.
2. Syntax
cin >> variable; // extraction: reads input into variable
cout << expression; // insertion: writes expression to output3. Explanation
cout uses the insertion operator << to send data to the standard output stream, and cin uses the extraction operator >> to pull data from the standard input stream into a variable. Both operators can be chained, so cout << a << b; prints a followed by b, and cin >> a >> b; reads two values separated by whitespace in one statement. The >> operator automatically skips leading whitespace (spaces, tabs, newlines) before reading the next token, which is why cin >> cannot read a full sentence containing spaces — it stops at the first whitespace character.
Cricket analogy: Announcing cout << over << ballsBowled; chains two pieces of info onto the scoreboard in one line just like a << b, while a scorer typing runs and wickets separated by a space types them as cin >> runs >> wickets;, but >> stops at the first space, so it can't capture a full sentence like a player's two-word nickname.
Since cin >> stops reading at whitespace, use getline(cin, str) instead of cin >> str when you need to capture an entire line, including spaces.
4. Example
#include <iostream>
using namespace std;
int main() {
int age;
string firstName;
cout << "Enter your first name: ";
cin >> firstName;
cout << "Enter your age: ";
cin >> age;
cout << "Hello " << firstName << ", you are " << age << " years old." << endl;
return 0;
}5. Output
Enter your first name: Alice
Enter your age: 30
Hello Alice, you are 30 years old.6. Key Takeaways
cinuses>>(extraction) to read input;coutuses<<(insertion) to write output.- Both operators support chaining, e.g.
cin >> a >> b;andcout << a << b;. cin >>skips leading whitespace and stops reading at the next whitespace, so it cannot capture multi-word input.cinandcoutrequire#include <iostream>and are part of thestdnamespace.
Practice what you learned
1. Which operator does `cout` use to send data to the console?
2. What does `cin >> name;` do if the user types "John Smith"?
3. Which header must be included to use `cin` and `cout`?
4. What is the effect of chaining `cout << a << b << c;`?
5. What does `cin >>` do with leading whitespace before a value?
Was this page helpful?
You May Also Like
getline() in C++
Learn how to use `getline()` to read entire lines of text including spaces, and avoid common pitfalls when mixing it with `cin >>`.
cerr and clog in C++
Understand the difference between `cerr` and `clog` in C++ for writing error and log messages, including buffering behavior.
I/O Manipulators in C++
Explore C++ I/O manipulators from `<iomanip>` like `setw`, `setprecision`, `setfill`, and `fixed` used to control stream formatting.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics