문제

명령 줄에서 입력으로 줄을 얻으려고합니다. 내 문제는 전체 라인을 얻지 못하지만 공간에 의해 토큰 화되고 있다는 것입니다.

그래서 내가 얻는 대신 "나는 수학을 많이 좋아한다"와 같은 무언가를 입력했다면

"you enterend: I like Math a lot"

나는 follwoing을 얻는다 :

EDITING MODE: Enter a command
i like Math a lot
you entered i

EDITING MODE: Enter a command
you entered like

EDITING MODE: Enter a command
you entered Math

EDITING MODE: Enter a command
you entered a

EDITING MODE: Enter a command
you entered lot


void enterEditingMode(){
    editingMode = TRUE;
    static string CMD = "\nEDITING MODE: Enter a command\n";
    string input;
    while(editingMode == TRUE){
        cout << CMD;
        cin >> input;
        //we assume input is always correct
        // here we need to parse the instruction
        cout << "you entered " << input <<endl;
도움이 되었습니까?

해결책

std::getline 한 번에 입력 한 줄을 읽는 표준 방법입니다.

다음과 같이 사용할 수 있습니다.

std::getline(std::cin, string);

암시 적 변환이있는 입력 스트림에 대한 참조를 반환합니다. void* 따라서 다음과 같은 성공을 쉽게 확인할 수 있습니다.

if (std::getline(std::cin, string))
{
    // successfully read a line...
}

다른 팁

cin.getline(input);

보다 http://www.cplusplus.com/reference/iostream/istream/getline/ 더 많은 정보를 위해서.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top