Notice
Recent Posts
Recent Comments
Link
«   2026/01   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

행복한 개구리

C++ Tutorial - Strings / User Input Strings 본문

C++/공부내용

C++ Tutorial - Strings / User Input Strings

HappyFrog 2022. 6. 13. 23:46

 

 

 


문자열 입력

추출 연산자  >> 를  cin 에 사용하여 유저가 입력한 문자열을 볼 수 있습니다:

 

 

예시

#include <iostream>

using namespace std;

int main() {
	string firstName;
	cout << "Type your first name: ";
	cin >> firstName;	// 키보드에서 firstName을 받아옵니다.
	cout << "Your name is: " << firstName;
}

 

그러나  cin  은 공백(공백, 탭 등)을 끝내는 글자로 여깁니다. 이것은 오직 한 단어만 보여줄 수 있음을 의미합니다. (당신이 여러 단어를 입력했더라도 말이죠.)

 

 

예시

#include <iostream>

using namespace std;

int main() {
	string fullName;
	cout << "Type your full name: ";
	cin >> fullName;
	cout << "Your name is: " << fullName;
}

 

위 예시에서 저는 "bodong bodong"이라고 출력되길 원했는데 "bodong"이라고만 출력되었습니다.

 

그래서 우리는 문자열을 작업할 때 보통   getline()  함수를 사용하여 텍스트를 읽습니다. 이것은  cin 을 첫 매개변수로 취하며 문자열 변수를 두 번째 매개변수로 취합니다.

 

 

예시

#include <iostream>
#include <string>
using namespace std;

int main() {
	string fullName;
	cout << "Type your full name: ";
	getline(cin, fullName);
	cout << "Your name is: " << fullName;
}