Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 기초
- W3Schools
- 파이썬
- Material
- 재귀
- parameter
- Algorithm
- Basic
- github
- C#
- dfs
- 프로그래밍
- w3school
- 오류
- python
- dynamic
- loop
- Tutorial
- Unreal Engine 5
- UE5
- guide
- c++
- Unity
- Class
- DP
- 문제풀이
- String
- 시작해요 언리얼 2022
- 백준
- Programming
Archives
- Today
- Total
행복한 개구리
C++ Tutorial - Strings / User Input Strings 본문

문자열 입력
추출 연산자 >> 를 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;
}

'C++ > 공부내용' 카테고리의 다른 글
| C++ Tutorial - Math (0) | 2022.06.14 |
|---|---|
| C++ Tutorial - Strings / Omitting Namespace (0) | 2022.06.13 |
| C++ Tutorial - Strings / Access Strings (0) | 2022.06.13 |
| C++ Tutorial - String / String Length (0) | 2022.06.13 |
| C++ Tutorial - Strings / Numbers and Strings (0) | 2022.06.13 |