Cin 버퍼를 어떻게 플러시합니까?
C ++에서 cin 버퍼를 어떻게 지우나요?
혹시:
std::cin.ignore(INT_MAX);
이것은까지 모든 것을 읽고 무시 EOF합니다. (또한 읽을 문자 인 두 번째 인수를 제공 할 수도 있습니다 (예 : '\n'한 줄 무시).
또한 : std::cin.clear();스트림 상태를 재설정하기 전에 a :를 수행 할 수도 있습니다.
C 버전보다 C ++ 크기 제약 조건을 선호합니다.
// Ignore to the end of file
cin.ignore(std::numeric_limits<std::streamsize>::max())
// Ignore to the end of line
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
cin.clear();
fflush(stdin);
이것은 콘솔에서 읽을 때 나를 위해 일한 유일한 것입니다. 다른 모든 경우에는 \ n 부족으로 인해 무기한으로 읽거나 버퍼에 무언가가 남아 있습니다.
편집 : 이전 솔루션이 상황을 악화 시켰다는 것을 알았습니다. 그러나 이것은 작동합니다.
cin.getline(temp, STRLEN);
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
이에 대한 두 가지 해결책을 찾았습니다.
첫 번째이자 가장 간단한 방법은 다음과 같이 사용하는 것 std::getline()입니다.
std::getline(std::cin, yourString);
... 개행에 도달하면 입력 스트림을 버립니다. 이 기능에 대한 자세한 내용은 여기를 참조하십시오 .
스트림을 직접 버리는 또 다른 옵션은 다음과 같습니다.
#include <limits>
// Possibly some other code here
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
행운을 빕니다!
int i;
cout << "Please enter an integer value: ";
// cin >> i; leaves '\n' among possible other junk in the buffer.
// '\n' also happens to be the default delim character for getline() below.
cin >> i;
if (cin.fail())
{
cout << "\ncin failed - substituting: i=1;\n\n";
i = 1;
}
cin.clear(); cin.ignore(INT_MAX,'\n');
cout << "The value you entered is: " << i << " and its double is " << i*2 << ".\n\n";
string myString;
cout << "What's your full name? (spaces inclded) \n";
getline (cin, myString);
cout << "\nHello '" << myString << "'.\n\n\n";
나는 선호한다:
cin.clear();
fflush(stdin);
cin.ignore가 그것을 자르지 않는 예가 있지만 지금은 생각할 수 없습니다. (Mingw와 함께) 그것을 사용해야 할 때가 오래 전이었습니다.
그러나 fflush (stdin)는 표준에 따라 정의되지 않은 동작입니다. fflush ()는 출력 스트림만을 의미합니다. fflush (stdin) 는 C 표준에 대한 확장 으로 Windows (적어도 GCC 및 MS 컴파일러 포함)에서 예상대로 작동하는 것 같습니다 .
따라서이를 사용하면 코드를 이식 할 수 없습니다.
fflush (stdin) 사용을 참조하십시오 .
Also, see http://ubuntuforums.org/showpost.php?s=9129c7bd6e5c8fd67eb332126b59b54c&p=452568&postcount=1 for an alternative.
How about:
cin.ignore(cin.rdbuf()->in_avail());
Another possible (manual) solution is
cin.clear();
while (cin.get() != '\n')
{
continue;
}
I cannot use fflush or cin.flush() with CLion so this came handy.
Easiest way:
cin.seekg(0,ios::end);
cin.clear();
It just positions the cin pointer at the end of the stdin stream and cin.clear() clears all error flags such as the EOF flag.
The following should work:
cin.flush();
On some systems it's not available and then you can use:
cin.ignore(INT_MAX);
#include <stdio_ext.h>
and then use function
__fpurge(stdin)
cin.get() seems to flush it automatically oddly enough (probably not preferred though, since this is confusing and probably temperamental).
참고URL : https://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer
'IT박스' 카테고리의 다른 글
| 한 줄의 코드로 파일 열기 및 닫기 (0) | 2020.08.11 |
|---|---|
| DataGridView 컨트롤의 열 크기를 자동으로 조정하고 사용자가 동일한 그리드의 열 크기를 조정할 수 있도록하는 방법은 무엇입니까? (0) | 2020.08.11 |
| 자바 : Transformer에서 생성 한 XML 들여 쓰기 방법 (0) | 2020.08.11 |
| Swing GUI에서 validate (), revalidate () 및 invalidate ()의 차이점 (0) | 2020.08.10 |
| Entity Framework 코드 우선-Fluent Api 대 데이터 주석의 장단점 (0) | 2020.08.10 |