← Back to C++ Course | Chapter 2: Input & Output | Lesson 8 of 8

C++ में String Input

String input यह है कि आपका program type करने वाले व्यक्ति से words या पूरे sentences कैसे पढ़ता है। cin एक बार में एक word पढ़ता है, जबकि getline एक पूरी line पकड़ता है।
Syntax
cpp
std::string variable_name;

std::cin >> variable_name;             // reads one word

std::getline(std::cin, variable_name);  // reads the whole line

cin के साथ String Input

cin >> variable सिर्फ पहले whitespace character तक input पढ़ता है — एक space, tab, या newline — जो इसे एक single word पढ़ने के लिए well suited बनाता है लेकिन एक read में एक पूरा sentence capture करने में असमर्थ।

अगर user "John Smith" type करे, cin >> name सिर्फ "John" capture करता है।

उदाहरण: String Input with cin

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("John Smith");
	std::string name;
	cin >> name; // stops at the first space
	std::cout << name << std::endl; // prints "John" only
	return 0;
}

getline से Full Line Input

getline(cin, variable) spaces समेत एक पूरी line पढ़ता है, सिर्फ तब रुकता है जब user Enter दबाए, जो इसे सही tool बनाता है जब भी input में कई words हो सकते हैं — पूरे names, addresses, या sentences।

cin >> के उलट, यह कभी line के अंदर whitespace skip नहीं करता।

उदाहरण: Full Line Input with getline

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("John Smith");
	std::string fullName;
	std::getline(cin, fullName); // reads the whole line, spaces included
	std::cout << fullName << std::endl;
	return 0;
}

cin और getline मिलाना

same program में cin >> और getline मिलाना एक classic pitfall है: cin >> input buffer में trailing newline character छोड़ देता है, और तुरंत बाद की एक getline call नए input का इंतज़ार करने की बजाय उस बची newline को एक empty line की तरह तुरंत पढ़ लेती है।

इनके बीच cin.ignore() call करना वह बची newline clear करता है ताकि getline उम्मीद के मुताबिक behave करे।

उदाहरण: Mixing cin and getline

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("25\nJohn Smith\n");
	int age;
	cin >> age;
	cin.ignore(); // clears the leftover newline before getline
	std::string name;
	std::getline(cin, name);
	std::cout << age << " " << name << std::endl;
	return 0;
}

String Input Limit करना

एक fixed-size C-style char array में पढ़ते समय, एक unbounded read buffer overflow कर सकता है अगर user array के hold कर सकने से ज़्यादा characters type करे, nearby memory corrupt करते हुए।

cin.width() या setw manipulator इस्तेमाल करना cap करता है कि कितने characters पढ़े जाते हैं, source पर उस overflow को रोकते हुए।

उदाहरण: Limiting String Input

cpp
#include <iostream>
#include <iomanip>

int main() {
	char buffer[6];
	std::cin.width(6); // caps how many characters are read into buffer
	std::cout << "Buffer capped to 6 characters" << std::endl;
	return 0;
}

Custom Delimited Strings पढ़ना

getline default newline की बजाय रुकने के लिए एक custom delimiter character specify करने वाला एक optional तीसरा argument accept करता है — उदाहरण के लिए, getline(cin, value, ',') अगले comma तक पढ़ता है।

यह इसे अपनी खुद की splitting logic लिखे बिना console से सीधे simple comma-separated input parse करने के लिए useful बनाता है।

उदाहरण: Reading Custom Delimited Strings

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("apple,banana,cherry");
	std::string value;
	std::getline(cin, value, ','); // stops at the comma, not newline
	std::cout << value << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बची newline clear किए बिना cin >> x और getline मिलाना, ताकि getline एक empty string return करे।
  2. cin >> name से एक पूरा नाम पढ़ना और सिर्फ पहला word पाना।
  3. std::string के साथ getline इस्तेमाल करते समय #include <string> भूल जाना।
चैप्टर सारांश
  • cin input पढ़ता है और cout output print करता है, numbers और new lines समेत।
  • cerr और clog error और log messages लिखते हैं।
  • Escape sequences, iomanip library, और string input यह control करने में मदद करते हैं कि data कैसे formatted और पढ़ा जाता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.