100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

cin and cout in C++

Learn how `cin` and `cout` handle standard input and output in C++ using the stream extraction and insertion operators, with chaining and gotchas.

Input & OutputBeginner6 min readJul 7, 2026
Analogies

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

cpp
cin >> variable;   // extraction: reads input into variable
cout << expression; // insertion: writes expression to output

3. 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

cpp
#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

text
Enter your first name: Alice
Enter your age: 30
Hello Alice, you are 30 years old.

6. Key Takeaways

  • cin uses >> (extraction) to read input; cout uses << (insertion) to write output.
  • Both operators support chaining, e.g. cin >> a >> b; and cout << a << b;.
  • cin >> skips leading whitespace and stops reading at the next whitespace, so it cannot capture multi-word input.
  • cin and cout require #include <iostream> and are part of the std namespace.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#CinAndCoutInC#Cin#Cout#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep