string str, temp;
string c;
cout << "Insert the character that ends the input:" << endl;
getline(cin, c);
cout << "Insert the string:" << endl;
getline(cin, str, c.c_str()[0]);
我應該能夠投入到字符串「測試」的字符串,直到我的數字結束字符,但如果我進入一個新的雙行不認識的結局字符,它不會結束輸入。C++函數getline()並沒有結束輸入
這是輸出:
Insert the character that ends the input:
}
Insert the string:
asdf
}
do it}
damn}
你的代碼和示例輸出是不同的。你能發佈正確的一個嗎 – Pradheep 2013-03-03 10:58:03
你的代碼適合我。但是我會用'char'來代替'c'。 – cnicutar 2013-03-03 10:58:17
'c [0]'而不是'c.c_str()[0]'也可以工作,但我無法重現這種行爲(g ++ -4.7,Ubuntu 12.10)。 – Cubic 2013-03-03 10:59:01