← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 4 of 17

C++ में Input Validation

Input validation यह check करती है कि user से entered data, जैसे cin के जरिए, actually वह matches करता है जो program expect करता है, invalid input से recover करने के लिए cin.fail() और cin.clear()/cin.ignore() इस्तेमाल करते हुए।
Syntax
cpp
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?

cpp
// 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;
}

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()

cpp
// 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;
}

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()

cpp
// 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;
}

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()

cpp
// 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;
}

एक Complete Validation Loop

एक loop को cin.fail(), cin.clear(), और cin.ignore() के साथ combine करना एक robust pattern बनाता है जो genuinely valid input मिलने तक user को दोबारा-दोबारा prompt करता रहता है।

उदाहरण: A Complete Validation Loop

cpp
// 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;
}
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. bad input के बाद cin.clear() call न करना, इसलिए stream एक failed state में रहता है और बाद की reads सब fail होती हैं।
  2. cin.clear() call करना लेकिन cin.ignore() नहीं, इसलिए bad text buffer में रहता है और loop हमेशा के लिए repeat होता है।
  3. यह मान लेना कि cin >> x इसका result check किए बिना हमेशा succeed होता है।

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.