🟢 Foundation  ·  Lesson 08

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:

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.

Streams: cout और cin

C++ input और output को streams के ज़रिए संभालता है। cout ("console out") screen पर data भेजता है, और cin ("console in") पढ़ता है कि user क्या टाइप करता है। दोनों <iostream> में रहते हैं।

cout से output

आप cout को insertion operator << से values भेजते हैं, और कई एक साथ chain कर सकते हैं:

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

cin से input

आप extraction operator >> से value को variable में पढ़ते हैं:

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

कई values पढ़ना

एक cin लगातार कई values पढ़ सकता है — user उन्हें spaces या नई 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

पूरी line पढ़ना

cin >> पहले space पर रुक जाता है, तो यह केवल एक शब्द पढ़ता है। पूरी line, spaces सहित, पढ़ने को 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!

आम 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 में पढ़ता है।
  • एक cin spaces से अलग की गई कई 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 कैसे पढ़ें?
Spaces सहित पूरी line को string में पढ़ने को 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 नहीं पकड़ सकता।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.