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:
int score = 90; cout << "Your score is " << score << " out of 100\n";
Your score is 90 out of 100
Input with cin
You read a value into a variable with the extraction operator >>:
#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;
}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:
int a, b; cout << "Enter two numbers: "; cin >> a >> b; cout << "Sum = " << a + b << "\n";
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:
#include <iostream>
using namespace std;
int main() {
string fullName;
cout << "Enter your full name: ";
getline(cin, fullName);
cout << "Hello, " << fullName << "!\n";
return 0;
}Output:
Hello, Amit Kumar!
Common input pitfalls
- getline after cin >> reads an empty line — clear the buffer first with
cin.ignore();. - Using
cin >> namefor a full name only captures the first word. - Typing letters when a number is expected leaves
cinin a failed state. - Mixing up
<<and>>— output uses<<, input uses>>.
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:
coutto print,cinto read. <<sends to output;>>reads into variables.- One
cincan read several values separated by spaces. getline(cin, str)reads a full line including spaces.- Use
cin.ignore()to fix an emptygetlineafter 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++?
<< 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++?
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?
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?
>> 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.