https://www.youtube.com/watch?v=kl39WiRZV7Q&list=PLgqG2uj21HgkcfVtlr5rPekQl5VWJEnIB&index=9
-cin, get/getline
사용자의 입력을 받아 변수에 저장하는 방법은 cin을 사용하면 된다.
#include <iostream>
using namespace std;
int main(){
char a[];
cin >> a;
cout << a << endl;
}
하지만 공백을 저장할 수 없다. 이때 사용하는 함수가 getline이다. 처음에는 어떤 변수에 저장을 할지, 두번째는 얼마만큼 저장을 할지 설정을 한다.
#include <iostream>
using namespace std;
int main(){
char a[5];
cin.getline(a, 5);
cout << a << endl;
}
c++에서 문자열을 다루는 방법 중 하나인 string을 알아본다. 총 5가지 특징이 있다.
#include <iostream>
using namespace std;
int main(){
string str1;
string str2 = "name";
str1 = str2;
cout << str1 << endl;
}
또한 배열로서 접근도 가능하다.
#include <iostream>
using namespace std;
int main(){
string str1;
string str2 = "name";
str1 = str2;
cout << str1[2] << endl;
}