如果输入有不同类型的空白字符,我如何将所有输入作为字符串获取?

问题描述 投票:0回答:1

例如, 输入:

有美好的时光 也有不好的时候 结束

#include <iostream>
#include <string>

using namespace std;
//there are good times
//and there are bad times
//END

int main()
{
    string str;
    getline(cin, str);
    cout << str;
}

一旦所有字符我怎么才能得到它,getline只得到“有美好时光”的部分

c++ input getline
1个回答
0
投票

虽然这可能不是一种有效的方法,但您可以检查您在 while 循环中输入的每一行,并在它与您的结束字符串 (

"END"
) 匹配时停止,就像这样..

#include <iostream>
#include <string>

using namespace std;
//there are good times
//and there are bad times
//END

int main()
{
    string str;
    string currentLine;
    string endLine = "END";
    
    while (getline(cin >> ws, currentLine))
    {
        if (currentLine == endLine)
            break;
        
        str += currentLine + "\n";
    }
    
    cout << str;
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.