2014-12-09 2 views
0

다음 코드에서 제목과 본문은 한 단어 뒤에 잘립니다. 나는 문자열이 여러 단어를 가질 수 있다고 생각 했는가? 누군가 이걸 정리할 수 있을까요? 감사합니다내 문자열이 한 단어로 제한되는 이유는 무엇입니까? (C++)

#include <fstream.h> 
#include <iostream.h> 
#include <string> 

using namespace std; 

string data, newtitle, body; 

ofstream outfile; 

int main() 
{ 
    cout << "enter title of note: "; 
    cin >> newtitle; 

    cout << "enter body of note: "; 
    cin >> body; 

    data = newtitle + ".dat"; 

    outfile.open(data.c_str(), ios::out); 
        outfile << body << endl; 
    outfile.close();    

system("pause"); 
} 
+3

'std :: string '은 여러 단어를 포함 할 수 있습니다. 'cin >> newtitle;'은 공백 문자를 찾으면 스캔을 멈 춥니 다. –

+0

당신이 그것을 채우는 방식이 잘못되었다고 생각하지 않고 문자열을 비난하는 신비한 실수! –

답변

1

cin.operator>> 기본적으로 공백 (공백 포함)으로 구분됩니다. getline을 사용하면 전체 입력 행을 얻을 수 있습니다.

cout << "enter title of note: "; 
getline(cin, newtitle); 

cout << "enter body of note: "; 
getline(cin, body); 
+0

문자열에서 문자열로 변환하면 혼란 스러웠습니다. 이제 제 프로그래밍 기술은 21 세기의 나머지 부분으로 돌아갑니다. 감사 – humdood

관련 문제