docs(10_Wiki): Dev 폴더 누락분 반영 — Topic_Programming으로 통합
이전 재구성 작업에서 Dev/ 폴더가 누락되었던 것을 반영. - Dev/Topic_Programming(중첩 폴더, 78개)은 Dev 자체 최상위 폴더들 (Architecture/Conventions/Engineering_Intelligence 등)과 완전 중복이라 제거. - Dev 최상위 엔지니어링 지식 폴더(Architecture/Conventions/Engineering_Intelligence/ Failure_Library/Generalized_Principles/Language/Pattern_Catalog/Platform_Guides/ Subsystems, 77개)는 이미 Topic_Programming/Topic_Programming에 더 최신 버전이 존재해 중복 제거(고유 콘텐츠 1개는 예외 처리하여 이동 보존). - Dev의 W3Schools 언어 튜토리얼 폴더(Topic_C/CPP/CSS/CSharp/HOWTO/HTML/Java/ JavaScript/PHP/Python/SQL/W3CSS, 1201개)는 전부 Topic_Programming 하위로 이동. - 에이전트 운영 상태(.astra/docs)는 그대로 유지, 콘텐츠 폴더만 정리. - Topic_Programming 최종 문서 수: 2784 → 3985.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: cpp-input-validation
|
||||
title: "C++ Input Validation"
|
||||
category: "Programming_Language"
|
||||
status: "draft"
|
||||
verification_status: "conceptual"
|
||||
canonical_id: ""
|
||||
aliases: ["cin.clear cin.ignore", "validate integer input", "C++ 입력 검증"]
|
||||
duplicate_of: ""
|
||||
source_trust_level: "B"
|
||||
confidence_score: 0.87
|
||||
created_at: 2026-07-04
|
||||
updated_at: 2026-07-04
|
||||
review_reason: ""
|
||||
merge_history: []
|
||||
tags: ["cpp", "programming-language", "w3schools", "input-validation", "cin"]
|
||||
raw_sources: ["https://www.w3schools.com/cpp/cpp_input_validation.asp"]
|
||||
applied_in: []
|
||||
github_commit: ""
|
||||
---
|
||||
|
||||
# [[CPP Input Validation]]
|
||||
|
||||
## 🎯 한 줄 통찰 (One-line insight)
|
||||
`while (!(cin >> number))` works because `cin >>` ITSELF evaluates to a boolean-like failure state when the parse fails — but a FAILED `cin >>` doesn't just fail cleanly, it leaves the STREAM in an error state AND the bad characters still sitting in the input buffer, which is why the fix requires TWO separate repair steps (`cin.clear()` to reset the error flag, `cin.ignore(10000, '\n')` to discard the leftover bad input) — a two-part cleanup ritual with no equivalent in C's simpler `fgets()`+`sscanf()` validation approach. [S1]
|
||||
|
||||
## 🧠 핵심 개념 (Core concepts)
|
||||
- **`cin >> number` as a validity check** — the expression itself is falsy when the input can't be parsed as the target type, usable directly in a `while (!(cin >> number))` loop. [S1]
|
||||
- **`cin.clear()`** — resets the stream's internal error flags after a failed read, WITHOUT which the stream stays "stuck" in a failed state. [S1]
|
||||
- **`cin.ignore(10000, '\n')`** — discards up to 10000 leftover characters (or until a newline), removing the bad input that caused the failure so the next read isn't corrupted. [S1]
|
||||
- **Range validation via do-while** — same pattern as C, repeatedly prompting until the value falls within bounds. [S1]
|
||||
- **Empty-text validation via `.empty()`** — `string.empty()` checks directly whether a string is blank, cleaner than C's `strlen(name) == 0` check. [S1]
|
||||
|
||||
## 📖 세부 내용 (Details)
|
||||
- Full integer-validation loop with the required two-step cleanup: `int number; while (!(cin >> number)) { cout << "Invalid input. Try again: "; cin.clear(); cin.ignore(10000, '\n'); }`. [S1]
|
||||
- Range-bounded input: `int number; do { cin >> number; } while (number < 1 || number > 5);`. [S1]
|
||||
- Empty-text rejection using `.empty()`: `string name; do { getline(cin, name); } while (name.empty());`. [S1]
|
||||
|
||||
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
||||
- **cin 실패 시 두 단계 복구가 필요함**: 파싱에 실패하면 스트림이 에러 상태에 갇히고 잘못된 입력이 버퍼에 남으므로, cin.clear()로 에러 플래그를 초기화하고 cin.ignore()로 남은 잘못된 입력을 제거하는 두 단계가 모두 필요하다는 점이 C의 fgets+sscanf 방식보다 복잡한 절차로 확인됨. [S1]
|
||||
|
||||
## 🛠️ 적용 사례 (Applied in summary)
|
||||
문자를 입력했을 때 "Invalid input. Try again"을 출력하고 다시 물어보는 정수 입력 검증 루프가 원문에서 직접 실전 활용 사례로 제시됨. [S1]
|
||||
|
||||
## 💻 코드 패턴 (Code patterns)
|
||||
Validating integer input with the required two-step stream cleanup after a failed parse (C++):
|
||||
```cpp
|
||||
int number;
|
||||
cout << "Enter a number: ";
|
||||
while (!(cin >> number)) {
|
||||
cout << "Invalid input. Try again: ";
|
||||
cin.clear(); // Reset input errors
|
||||
cin.ignore(10000, '\n'); // Remove bad input
|
||||
}
|
||||
cout << "You entered: " << number;
|
||||
```
|
||||
|
||||
## ✅ 검증 상태 및 신뢰도
|
||||
- **상태:** draft
|
||||
- **검증 단계:** conceptual
|
||||
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
||||
- **신뢰 점수:** 0.87
|
||||
- **중복 검사 결과:** 신규 생성 (New discovery)
|
||||
|
||||
## 🔗 지식 그래프 (Knowledge Graph)
|
||||
- **상위/루트:** [[C++ Tutorial]]
|
||||
- **관련 개념:** [[CPP Exceptions]], [[CPP User Input]], [[CPP Debugging]]
|
||||
- **참조 맥락:** 입력 검증 — 디버깅(Debugging) 챕터로 이어짐.
|
||||
|
||||
## 📚 출처 (Sources)
|
||||
- [S1] W3Schools — C++ Input Validation — https://www.w3schools.com/cpp/cpp_input_validation.asp
|
||||
|
||||
## 📝 변경 이력 (Change history)
|
||||
- 2026-07-04: Initial draft synthesized from the W3Schools "C++ Input Validation" page (Astra wiki-curation, P-Reinforce v3.1 format).
|
||||
Reference in New Issue
Block a user