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

后端 未结 9 2188
醉话见心
醉话见心 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 11:01

    On which compiler did you try this? I tried on VC2008 and worked fine. If I compiled the same code on g++ (GCC) 3.4.2. It did not work properly. Below is the versions worked in both compilers. I dont't have the latest g++ compiler in my environment.

    int n;
    cin >> n;
    string local;
    getline(cin, local); // don't need this on VC2008. But need it on g++ 3.4.2. 
    for (int i = 0; i < n; i++)
    {
        getline(cin, local);
        cout << local;
    }
    

提交回复
热议问题