首页 > 解决方案 > 单独地,std::getline() 工作正常,但如果事先有输入,则输出行为异常

问题描述

我想在字符串中输入空格。

例如:"Hello World"

输入:

2
Hello World
Good Morning

期望的输出:

Hello World
Good Morning

但是这段代码不起作用,为什么?

int main()
{
    int q;
    cin >> q;
    string str[q];
    int i = 0;

    while(q--)
    {
        getline(cin, str[i]);
        cout << str[i] << endl;
        i++;
    }
    
    return 0;
}

标签: c++outputgetline

解决方案


推荐阅读