C++ में Input Validation
In this page:
if (std::cin.fail()) {
std::cin.clear(); // reset error flags
std::cin.ignore(10000, '\n'); // discard bad input
}
Input Validate क्यों करें?
User input कभी पूरी तरह trust नहीं किया जा सकता, क्योंकि एक person एक number की जगह letters type कर सकता है या एक field empty छोड़ सकता है, और unvalidated input एक program crash कर सकता है या silently गलत results produce कर सकता है।
उदाहरण: Why Validate Input?
// Include std::cout and std::cin
#include <iostream>
// Include string streams
#include <sstream>
// Program execution starts in main()
int main() {
std::istringstream input("abc");
int number;
input >> number;
// Print to the console with cout
std::cout << "Extraction failed: " << input.fail() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
cin.fail() से Failed Input Detect करना
cin.fail() true return करता है अगर सबसे हाल का extraction operation fail हुआ हो, जैसे जब एक number expect होने पर non-numeric text enter हुआ हो, program को bad input detect और react करने देते हुए।
उदाहरण: Detecting Failed Input with cin.fail()
// Include std::cout and std::cin
#include <iostream>
// Include string streams
#include <sstream>
// Program execution starts in main()
int main() {
std::istringstream input("abc");
int number;
input >> number;
// Check whether input.fail()
if (input.fail()) {
// Print to the console with cout
std::cout << "Invalid input detected" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
cin.clear() से Error State Clear करना
एक बार cin एक failed state में enter हो जाए, यह आगे input process करना बंद कर देता है जब तक cin.clear() इसके internal error flags reset न करे, जो stream फिर से इस्तेमाल होने से पहले एक required step है।
उदाहरण: Clearing the Error State with cin.clear()
// Include std::cout and std::cin
#include <iostream>
// Include string streams
#include <sstream>
// Program execution starts in main()
int main() {
std::istringstream input("abc");
int number;
input >> number;
// Check whether input.fail()
if (input.fail()) {
input.clear();
// Print to the console with cout
std::cout << "Cleared error state" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
cin.ignore() से Bad Input Discard करना
एक failed read के बाद, invalid characters typically input buffer में रहते हैं, और cin.ignore() इन्हें discard करता है (अक्सर अगली newline तक) ताकि अगली read attempt fresh शुरू हो।
उदाहरण: Discarding Bad Input with cin.ignore()
// Include std::cout and std::cin
#include <iostream>
// Include string streams
#include <sstream>
#include <limits>
// Program execution starts in main()
int main() {
std::istringstream input("abc 42");
int number;
input >> number;
input.clear();
input.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
input >> number;
// Print to the console with cout
std::cout << number << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Complete Validation Loop
एक loop को cin.fail(), cin.clear(), और cin.ignore() के साथ combine करना एक robust pattern बनाता है जो genuinely valid input मिलने तक user को दोबारा-दोबारा prompt करता रहता है।
उदाहरण: A Complete Validation Loop
// Include std::cout and std::cin
#include <iostream>
// Include string streams
#include <sstream>
// Program execution starts in main()
int main() {
std::istringstream input("abc 42");
int number;
// Repeat while !(input >> number) is true
while (!(input >> number)) {
input.clear();
input.ignore();
}
// Print to the console with cout
std::cout << "Valid input: " << number << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- bad input के बाद
cin.clear()call न करना, इसलिए stream एक failed state में रहता है और बाद की reads सब fail होती हैं। cin.clear()call करना लेकिनcin.ignore()नहीं, इसलिए bad text buffer में रहता है और loop हमेशा के लिए repeat होता है।- यह मान लेना कि
cin >> xइसका result check किए बिना हमेशा succeed होता है।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced