std::getline does not work inside a for-loop

后端 未结 9 2202
醉话见心
醉话见心 2020-12-15 10:28

I\'m trying to collect user\'s input in a string variable that accepts whitespaces for a specified amount of time.

Since the usual cin >> str does

9条回答
  •  一个人的身影
    2020-12-15 10:47

    You can directly use getline function in string using delimiter as follows:

    #include 
    using namespace std;
    int main()
    {
        string str;
        getline(cin,str,'#');
        getline(cin,str,'#');
    }
    

    you can input str as many times as you want but one condition applies here is you need to pass '#'(3rd argument) as delimiter i.e. string will accept input till '#' has been pressed regardless of newline character.

提交回复
热议问题