Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 08

Input and Output in C++ (cin, cout)

In this lesson: Take input with cin, print with cout and read full lines with getline in C++ — examples with output. In Hindi & English.

Streams: cout and cin

C++ handles input and output through streams. cout ("console out") sends data to the screen, and cin ("console in") reads what the user types. Both live in <iostream>.

Output with cout

You send values to cout with the insertion operator <<, and you can chain many together:

C++
int score = 90;
cout << "Your score is " << score << " out of 100\n";
Output:
Your score is 90 out of 100

Input with cin

You read a value into a variable with the extraction operator >>:

C++
#include <iostream>
using namespace std;
int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;
    cout << "Next year you will be " << age + 1 << "\n";
    return 0;
}
Run: Enter your age: 20
Output:
Next year you will be 21

Reading multiple values

One cin can read several values in a row — the user separates them with spaces or new lines:

C++
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum = " << a + b << "\n";
Run: Enter two numbers: 4 6
Output:
Sum = 10

Reading a full line

cin >> stops at the first space, so it reads only one word. To read a whole line, including spaces, use getline:

C++
#include <iostream>
using namespace std;
int main() {
    string fullName;
    cout << "Enter your full name: ";
    getline(cin, fullName);
    cout << "Hello, " << fullName << "!\n";
    return 0;
}
Run: Enter your full name: Amit Kumar
Output:
Hello, Amit Kumar!

Common input pitfalls

  • getline after cin >> reads an empty line — clear the buffer first with cin.ignore();.
  • Using cin >> name for a full name only captures the first word.
  • Typing letters when a number is expected leaves cin in a failed state.
  • Mixing up << and >> — output uses <<, input uses >>.
🏋️ Practice

Write a program that asks for the user's name (full line) and age (number), then prints a greeting. Remember to use cin.ignore() between reading the age and the line if you read the number first.

Summary

  • C++ input/output uses streams: cout to print, cin to read.
  • << sends to output; >> reads into variables.
  • One cin can read several values separated by spaces.
  • getline(cin, str) reads a full line including spaces.
  • Use cin.ignore() to fix an empty getline after reading a number.

Frequently Asked Questions

How do cin and cout work in C++?
cout is the output stream that prints to the screen using the insertion operator <<, while cin is the input stream that reads typed values into variables using the extraction operator >>. Both come from the <iostream> header.
What is the difference between << and >> in C++?
The << insertion operator sends data to an output stream like cout, while the >> extraction operator pulls data from an input stream like cin into a variable. A simple memory aid is that the arrows point in the direction the data flows.
How do you read a full line of text in C++?
Use getline(cin, str) to read an entire line including spaces into a string, because cin >> str stops at the first space and only reads one word. getline reads until you press Enter.
Why does cin skip input after reading a number?
After cin >> number reads a number, the newline you pressed stays in the input buffer. A following getline then reads that leftover newline as an empty line. The fix is to clear the leftover with cin.ignore() before calling getline.
What does cin >> skip by default?
The >> extraction operator skips leading whitespace such as spaces, tabs and newlines, then reads until the next whitespace. That is why it reads one word or one number at a time and cannot capture text that contains spaces.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicOperators in C++ Your progress: 8 / 81 (10%) · Lesson 9 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free