프로그래밍 언어/C++

[C++] Cin 공백포함 띄어쓰기 입력 받는 법

Everybody's Service 2019. 12. 7. 20:22

cout << "Enter QUERY" << endl;
cin.ignore();
getline(cin, sql);

 

SQL문을 입력받는다고 가정하자.

SQL]  SELECT * FROM TABLE1

 

 

만약, 저 위의 함수가 아닌, 

cin >> sql을 쓰면 위의 SQL문장은 다음과 같이 입력된다.

SELECT*FROMTABLE1

 

따라서, 띄어쓰기를 포함해서 입력받고 싶으면,

getline 함수를 써야한다.

 

getline함수로 입력받기전에, cin.ignore() 함수를 불러 줘야 하는데,

 

그 이유는 stackoverflow에 정리되어있다. https://stackoverflow.com/questions/21686762/why-do-we-need-to-use-cin-ignore-before-getlinecin-string

 

Why do we need to use cin.ignore() before getline(cin, string)?

Why do we need to use cin.ignore() before taking input in a string? What is the backhand process? Why does it skip the input in a string (if we call getline function for more variables) if we don'...

stackoverflow.com