Input Output in C++: cin, cout और getline आसान भाषा में

Input Output in C++ क्या है?

Input Output in C++: cin, cout और getline आसान भाषा में

C++ सीखते समय सबसे पहले जिन concepts को समझना जरूरी होता है, उनमें Input और Output भी शामिल हैं। कोई भी program तभी useful बनता है जब वह user से data ले सके और processing के बाद उसका result दिखा सके।

मान लीजिए आपने ऐसा program बनाया है जो दो numbers को जोड़ता है। User keyboard से दो numbers enter करेगा, program उन numbers को process करेगा और फिर उनका sum screen पर दिखाएगा। यहाँ numbers देना Input है और sum दिखाना Output है।

C++ में keyboard से Input लेने और screen पर Output दिखाने के लिए मुख्य रूप से cin और cout का इस्तेमाल किया जाता है। इनके साथ >> और << operators का प्रयोग होता है।

UserInput (cin)C++ ProgramOutput (cout)Screen

Input क्या होता है?

जब किसी program को बाहर से कोई data दिया जाता है, तो उसे Input कहते हैं। Input keyboard, file या किसी दूसरे source से लिया जा सकता है। शुरुआती C++ programs में keyboard से Input लेना सबसे common तरीका है।

उदाहरण के लिए, यदि program user से उसका नाम, age और marks पूछता है और user ये information enter करता है, तो यह पूरी information program के लिए Input है।

Example

int age;
cin >> age;

अगर user 18 enter करता है, तो 18 variable age में store हो जाएगा।

Output क्या होता है?

Program द्वारा processing के बाद user को दिखाई जाने वाली information को Output कहा जाता है। C++ में screen पर Output दिखाने के लिए सामान्यतः cout का इस्तेमाल किया जाता है।

Example

int age = 18;
cout << age;

Output:

18

इस उदाहरण में 18 Output है।

C++ में Input Output के लिए कौन-कौन से Objects होते हैं?

C++ में console Input Output के लिए कुछ important stream objects उपलब्ध हैं। Beginner level पर सबसे ज्यादा इस्तेमाल cin और cout का होता है।

Object काम Example
cin Input लेने के लिए cin >> age;
cout Output दिखाने के लिए cout << age;
cerr Error message दिखाने के लिए cerr << "Error";
clog Log information के लिए clog << "Log";

iostream Header File क्या है?

cin और cout का इस्तेमाल करने के लिए C++ program में सामान्यतः iostream header file include की जाती है।

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello C++";
    return 0;
}

यहाँ #include <iostream> program में standard Input और Output से संबंधित सुविधाएं उपलब्ध कराता है।

Important: अगर आप cin और cout का इस्तेमाल कर रहे हैं, तो सामान्य console programs में iostream include करना जरूरी होता है।

C++ में Output कैसे दिखाएं?

Output दिखाने के लिए cout का इस्तेमाल किया जाता है। इसके साथ Insertion Operator (<<) लगाया जाता है।

Simple Output Example

#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to C++";
    return 0;
}

Output:

Welcome to C++

cout से केवल text ही नहीं, बल्कि numbers, variables और calculations के results भी print किए जा सकते हैं।

Variable का Output

int age = 16;

cout << age;

Output:

16

Text और Variable एक साथ Print करना

int age = 16;

cout << "My age is " << age;

Output:

My age is 16

एक ही cout से कई Values कैसे Print करें?

C++ में एक ही cout statement में कई values को << operator की मदद से print किया जा सकता है।

int a = 10;
int b = 20;

cout << "A = " << a << endl;
cout << "B = " << b << endl;

Output:

A = 10
B = 20

C++ में New Line कैसे दें?

Output को नई line में दिखाने के लिए C++ में \n और endl का इस्तेमाल किया जा सकता है।

1. \n का इस्तेमाल

cout << "First Line\n";
cout << "Second Line";

Output:

First Line
Second Line

2. endl का इस्तेमाल

cout << "First Line" << endl;
cout << "Second Line";

Output:

First Line
Second Line

दोनों तरीकों से नई line प्राप्त की जा सकती है।

C++ में Input कैसे लें?

User से keyboard द्वारा Input लेने के लिए cin का इस्तेमाल किया जाता है। इसके साथ Extraction Operator (>>) लगाया जाता है।

Example

#include <iostream>
using namespace std;

int main()
{
    int age;

    cout << "Enter your age: ";
    cin >> age;

    cout << "Your age is " << age;

    return 0;
}

अगर user 18 enter करता है, तो output कुछ इस तरह होगा:

Enter your age: 18
Your age is 18

एक साथ दो या अधिक Input कैसे लें?

C++ में एक ही cin statement से कई variables का Input लिया जा सकता है।

int a, b;

cout << "Enter two numbers: ";
cin >> a >> b;

cout << "Sum = " << a + b;

अगर user 10 25 enter करता है, तो Output होगा:

Sum = 35

Input values को space देकर या Enter दबाकर अलग-अलग भी दिया जा सकता है।

Different Data Types का Input लेना

C++ में अलग-अलग data types के variables का Input लिया जा सकता है।

int age;
float height;
char grade;
double salary;

cin >> age;
cin >> height;
cin >> grade;
cin >> salary;

यहाँ हर variable के लिए उसका अलग data type इस्तेमाल किया गया है। Program Input को variable के data type के अनुसार समझता है।

String का Input कैसे लें?

String का Input लेते समय beginners को एक common problem आती है। उदाहरण के लिए:

string name;

cin >> name;

अगर user Rahul Kumar enter करता है, तो सामान्य cin >> केवल Rahul को पढ़ सकता है। इसका कारण यह है कि cin >> whitespace, जैसे space या Enter, पर Input को अलग कर देता है।

getline() से पूरी Line का Input

अगर आपको spaces सहित पूरी line पढ़नी है, तो getline() का इस्तेमाल किया जा सकता है।

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;

    cout << "Enter your full name: ";
    getline(cin, name);

    cout << "Hello " << name;

    return 0;
}

अब अगर user Rahul Kumar enter करता है, तो पूरा नाम read किया जा सकता है।

Exam Tip: cin >> सामान्यतः whitespace तक Input पढ़ता है, जबकि getline() spaces सहित पूरी line पढ़ सकता है।

cin के बाद getline() की Problem

कभी-कभी पहले cin से number लेने और उसके बाद getline() इस्तेमाल करने पर getline() खाली line पढ़ लेता है। इसका कारण Input buffer में बचा हुआ newline character हो सकता है।

ऐसी स्थिति में cin.ignore() का इस्तेमाल किया जा सकता है।

int age;
string name;

cout << "Enter age: ";
cin >> age;

cin.ignore();

cout << "Enter full name: ";
getline(cin, name);

cout << "Name: " << name;

cin.ignore() यहाँ Input buffer में मौजूद unwanted character को ignore करने में मदद करता है।

Real-Life Example: Student Details Program

अब एक practical example देखते हैं जिसमें student का name, age और marks लेकर उसकी details screen पर दिखाई जाएंगी।

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    int age;
    float marks;

    cout << "Enter student name: ";
    getline(cin, name);

    cout << "Enter age: ";
    cin >> age;

    cout << "Enter marks: ";
    cin >> marks;

    cout << "\n--- Student Details ---\n";
    cout << "Name: " << name << "\n";
    cout << "Age: " << age << "\n";
    cout << "Marks: " << marks << "\n";

    return 0;
}

यह example बताता है कि एक वास्तविक program में Input और Output किस तरह साथ काम करते हैं।

Input और Output Operators में अंतर

Operator नाम मुख्य उपयोग
<< Insertion Operator cout के साथ Output
>> Extraction Operator cin के साथ Input

इसे याद रखने की आसान trick है: cout से data बाहर जाता है, इसलिए << और cin में data अंदर आता है, इसलिए >>

C++ Input Output में होने वाली Common Mistakes

1. iostream Header File भूल जाना

अगर आप cin और cout इस्तेमाल कर रहे हैं तो program में सामान्यतः यह line होनी चाहिए:

#include <iostream>

2. cin में गलत Operator लगाना

गलत:

cin << age;

सही:

cin >> age;

3. cout में गलत Operator लगाना

गलत:

cout >> age;

सही:

cout << age;

4. Variable Declare किए बिना Input लेना

पहले variable declare करें और फिर Input लें।

int age;
cin >> age;

5. String में Space की Problem

अगर text में spaces हैं और आपको पूरी line चाहिए, तो getline() का इस्तेमाल करें।

C++ Input Output के लिए Useful Tips

  • Input लेने से पहले user को बताएं कि उसे क्या enter करना है।
  • हर variable के लिए appropriate data type चुनें।
  • Full name या sentence के लिए getline() उपयोगी है।
  • Output को readable बनाने के लिए \n या endl का इस्तेमाल करें।
  • एक ही cout statement में कई values print की जा सकती हैं।
  • cin और cout के operators को हमेशा ध्यान से लिखें।

Input Output in C++ के Important Key Points

  • cin का इस्तेमाल Input लेने के लिए किया जाता है।
  • cout का इस्तेमाल Output दिखाने के लिए किया जाता है।
  • >> Extraction Operator है।
  • << Insertion Operator है।
  • iostream standard console Input Output के लिए important Header File है।
  • \n नई line के लिए इस्तेमाल किया जा सकता है।
  • endl भी Output को नई line में ले जाता है।
  • getline() spaces सहित पूरी line पढ़ने के लिए उपयोगी है।
  • cin.ignore() कुछ situations में Input buffer की unwanted newline को handle करने में मदद करता है।

Practical Exercises

Concept को अच्छी तरह समझने के लिए इन programs को खुद बनाने की कोशिश करें:

  1. एक program बनाइए जो user का नाम और age लेकर screen पर दिखाए।
  2. दो numbers लेकर उनका Sum, Difference, Multiplication और Division calculate कीजिए।
  3. Student का नाम और तीन subjects के marks लेकर Total और Average calculate कीजिए।
  4. Rectangle की Length और Width लेकर उसका Area calculate कीजिए।
  5. Full Name Input लेकर उसे वापस screen पर print कीजिए।
  6. एक program बनाइए जो Celsius temperature Input लेकर Fahrenheit में convert करे।

Interview Questions on Input Output in C++

Q1. C++ में Input लेने के लिए किस object का इस्तेमाल होता है?

Answer: C++ में standard Input लेने के लिए cin का इस्तेमाल किया जाता है।

Q2. C++ में Output के लिए किस object का इस्तेमाल होता है?

Answer: Output दिखाने के लिए cout का इस्तेमाल किया जाता है।

Q3. cin के साथ कौन सा operator इस्तेमाल होता है?

Answer: >>, जिसे Extraction Operator कहा जाता है।

Q4. cout के साथ कौन सा operator इस्तेमाल होता है?

Answer: <<, जिसे Insertion Operator कहा जाता है।

Q5. Full line Input लेने के लिए क्या इस्तेमाल करते हैं?

Answer: Spaces सहित पूरी line पढ़ने के लिए getline() का इस्तेमाल किया जा सकता है।

Q6. cin और cout के लिए कौन सी Header File इस्तेमाल होती है?

Answer: Standard console Input Output के लिए iostream Header File इस्तेमाल होती है।

MCQs: Input Output in C++

1. C++ में Output के लिए कौन सा object इस्तेमाल होता है?

A. cin
B. cout
C. input
D. scan

Answer: B. cout

2. cin के साथ कौन सा operator इस्तेमाल होता है?

A. <<
B. >>
C. ==
D. =

Answer: B. >>

3. cout के साथ कौन सा operator इस्तेमाल होता है?

A. >>
B. <<
C. ++
D. --

Answer: B. <<

4. Standard Input Output के लिए कौन सी Header File इस्तेमाल होती है?

A. string
B. math
C. iostream
D. input

Answer: C. iostream

5. Spaces सहित पूरी line पढ़ने के लिए कौन सा function उपयोगी है?

A. scanf()
B. getline()
C. print()
D. read()

Answer: B. getline()

6. नई line के लिए कौन सा escape sequence इस्तेमाल होता है?

A. \t
B. \n
C. \b
D. \r

Answer: B. \n

Quick Quiz

Question: नीचे दिए गए program का Output क्या होगा?

int a = 10;
int b = 20;

cout << "Total = " << a + b;

Answer:

Total = 30

Explanation: यहाँ a की value 10 और b की value 20 है। इसलिए a + b का result 30 होगा और cout उसे screen पर print करेगा।

Short Notes: Input Output in C++

  • Input: Program को दिया गया data Input कहलाता है।
  • Output: Program द्वारा दिखाया गया result Output कहलाता है।
  • cin: Standard Input लेने के लिए।
  • cout: Standard Output दिखाने के लिए।
  • >>: Extraction Operator.
  • <<: Insertion Operator.
  • getline(): Spaces सहित पूरी line पढ़ने के लिए।
  • \n: नई line के लिए Escape Sequence.
  • iostream: Standard Input Output के लिए Header File.

FAQs: Input Output in C++

1. C++ में Input और Output क्या होता है?

Program को बाहर से दिया गया data Input कहलाता है और processing के बाद program द्वारा दिखाई गई information Output कहलाती है। Keyboard से number enter करना Input और उसका result screen पर देखना Output है।

2. C++ में cin और cout क्या हैं?

cin standard Input लेने के लिए और cout standard Output दिखाने के लिए इस्तेमाल होने वाले stream objects हैं।

3. C++ में cin के साथ >> operator क्यों इस्तेमाल होता है?

>> Extraction Operator है। इसका इस्तेमाल Input stream से data लेकर उसे variable में store करने के लिए किया जाता है। उदाहरण: cin >> age;

4. C++ में cout के साथ << operator क्यों इस्तेमाल होता है?

<< Insertion Operator है। इसका इस्तेमाल data को Output stream में भेजने के लिए किया जाता है। उदाहरण: cout << age;

5. C++ में पूरी line का Input कैसे लें?

Spaces सहित पूरी line का Input लेने के लिए getline() का इस्तेमाल किया जा सकता है। यह full name, address या sentence जैसे text के लिए उपयोगी है।

6. cin और getline() में क्या अंतर है?

cin >> सामान्यतः whitespace तक data पढ़ता है। इसलिए space वाले text में यह पहला हिस्सा पढ़ सकता है। वहीं getline() पूरी line को spaces सहित पढ़ सकता है।

7. C++ में नई line कैसे print करें?

नई line के लिए \n या endl का इस्तेमाल किया जा सकता है। उदाहरण: cout << "Hello\nWorld";

8. क्या एक ही cin statement में कई Input लिए जा सकते हैं?

हाँ। उदाहरण के लिए cin >> a >> b >> c; से तीन variables का Input एक ही statement में लिया जा सकता है।

निष्कर्ष

C++ में Input और Output programming की basic concepts में से एक है। जब आपको cin, cout, >>, <<, getline() और \n का सही इस्तेमाल समझ आ जाता है, तो शुरुआती C++ programs लिखना काफी आसान हो जाता है।

इस topic को केवल पढ़ने के बजाय दिए गए examples को अपने compiler में run करके देखना ज्यादा useful रहेगा। खासकर cin और getline() के बीच का difference खुद program बनाकर test करें। इससे Input Output का concept लंबे समय तक याद रहेगा।

अगर इस topic से जुड़ा कोई सवाल या programming problem है, तो उसे comment में लिख सकते हैं। आपके सवाल से दूसरे students को भी सीखने में मदद मिल सकती है।

टिप्पणियाँ