Input and Output in C++
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.Streams: cout और cin
C++ input और output को streams के ज़रिए संभालता है। cout ("console out") screen पर data भेजता है, और cin ("console in") पढ़ता है कि user क्या टाइप करता है। दोनों <iostream> में रहते हैं।
cout से output
आप cout को insertion operator << से values भेजते हैं, और कई एक साथ chain कर सकते हैं:
int score = 90; cout << "Your score is " << score << " out of 100\n";
Your score is 90 out of 100
cin से input
आप extraction operator >> से value को variable में पढ़ते हैं:
#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
कई values पढ़ना
एक cin लगातार कई values पढ़ सकता है — user उन्हें spaces या नई lines से अलग करता है:
int a, b; cout << "Enter two numbers: "; cin >> a >> b; cout << "Sum = " << a + b << "\n";
Output:
Sum = 10
पूरी line पढ़ना
cin >> पहले space पर रुक जाता है, तो यह केवल एक शब्द पढ़ता है। पूरी line, spaces सहित, पढ़ने को 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!
आम input समस्याएँ
- cin >> के बाद getline खाली line पढ़ता है — पहले
cin.ignore();से buffer साफ़ करें। - पूरे नाम के लिए
cin >> nameकेवल पहला शब्द पकड़ता है। - Number की जगह अक्षर टाइप करना
cinको failed state में छोड़ देता है। <<और>>मिलाना — output<<इस्तेमाल करता है, input>>।
एक program लिखें जो user का नाम (पूरी line) और उम्र (number) पूछे, फिर एक अभिवादन print करे। अगर आप number पहले पढ़ें, तो उम्र और line पढ़ने के बीच cin.ignore() इस्तेमाल करना याद रखें।
सारांश
- C++ input/output streams इस्तेमाल करता है: print करने को
cout, पढ़ने कोcin। <<output को भेजता है;>>variables में पढ़ता है।- एक
cinspaces से अलग की गई कई values पढ़ सकता है। getline(cin, str)spaces सहित पूरी line पढ़ता है।- Number पढ़ने के बाद खाली
getlineठीक करने कोcin.ignore()इस्तेमाल करें।