프로그래밍/C,C++

C++ 문자열 공백 포함 입력받기

coty 2019. 7. 29. 03:45

cin.getline(s, n, delim);

공백과 종료(NULL)을 포함하여 n만큼 입력받아 s에 저장

delim문자가 입력되면 종료

@ 인자값

s : 문자열을 입력받으려는 문자배열

n : s에 입력받으려는 최대 문자수(공백과 NULL문자 포함)

delim : 해당 문자가 나타나면 입력 종료('\n')

#include <iostream>
using namespace std;

#define MAX 1000000

int main() {

	char* s = new char[MAX];

	cin.getline(s, MAX, '\n');
	cout << s;

    delete[] s;
}

 

getline(cin, s);

@ 인자값

s : 문자열을 입력받으려는 string변수

 

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

int main()
{
    ios::sync_with_stdio(NULL);
    cin.tellg();

    vector<string> str;
    string s;

    getline(cin, s);

    str.push_back(s);
    cout << str[0] << "\n";    
}