C++
C++ - cin.ignore() (입력버퍼 초기화)
마루설아
2024. 12. 31. 19:08
#include <iostream>
#include <string>
#define endl "\n"
using namespace std;
int main(void) {
// C++ Init
ios::sync_with_stdio(false);
cin.tie(NULL);
int input;
string str;
cin >> input;
// cin 입력 버퍼 초기화
cin.ignore();
getline(cin, str);
cout << input << endl;
cout << str << endl;
}
cin은 개행문자(\n)를 버퍼에 남겨두고 있기 때문에
getline 명령줄이 무시된다.
cin.ignore()를 하여 버퍼를 초기화하여 사용한다.