C++ में String Input
In this page:
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
- बची newline clear किए बिना
cin >> xऔरgetlineमिलाना, ताकिgetlineएक empty string return करे। cin >> nameसे एक पूरा नाम पढ़ना और सिर्फ पहला word पाना।std::stringके साथgetlineइस्तेमाल करते समय#include <string>भूल जाना।
cininput पढ़ता है औरcoutoutput print करता है, numbers और new lines समेत।cerrऔरclogerror और log messages लिखते हैं।- Escape sequences,
iomaniplibrary, और string input यह control करने में मदद करते हैं कि data कैसे formatted और पढ़ा जाता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: