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.
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()इस्तेमाल करें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में cin और cout कैसे काम करते हैं?
cout output stream है जो insertion operator << से screen पर print करता है, जबकि cin input stream है जो extraction operator >> से टाइप की गई values को variables में पढ़ता है। दोनों <iostream> header से आते हैं।C++ में << और >> में क्या अंतर है?
<< insertion operator cout जैसे output stream को data भेजता है, जबकि >> extraction operator cin जैसे input stream से data variable में खींचता है। एक सरल याद-तरकीब यह है कि तीर उसी दिशा में इशारा करते हैं जिधर data बहता है।C++ में text की पूरी line कैसे पढ़ें?
getline(cin, str) इस्तेमाल करें, क्योंकि cin >> str पहले space पर रुक जाता है और केवल एक शब्द पढ़ता है। getline Enter दबाने तक पढ़ता है।Number पढ़ने के बाद cin input क्यों छोड़ देता है?
cin >> number number पढ़ने के बाद, आपके दबाए newline input buffer में रह जाता है। तब अगला getline उस बचे newline को खाली line के रूप में पढ़ता है। इसका हल getline call करने से पहले cin.ignore() से बचा हिस्सा साफ़ करना है।cin >> default रूप से क्या छोड़ता है?
>> extraction operator आगे का whitespace जैसे spaces, tabs और newlines छोड़ता है, फिर अगले whitespace तक पढ़ता है। इसीलिए यह एक बार में एक शब्द या एक number पढ़ता है और spaces वाला text नहीं पकड़ सकता।