0

在 xcode 4.3 中,我已将项目设置为使用 c++11:我将语音 c++ 语言方言更改为 c++11,并将 c++ 标准库更改为“libc++(支持 c++11 的 LLVM c++ 标准库)”。
然后我尝试编译并执行这个简单的代码:

#include <iostream>

using namespace std;

int main (int argc, char** argv) 
{
    char buffer[100];
    cin.getline(buffer,100);
    cout << buffer << endl;
    return 0;
}

问题是它要求输入两次。例如,我输入“hello”,流仍然打开,等待另一个字符串。如果我输入另一个字符串,那么它会打印出“hello”。
如果我不使用 c++11,则不会出现此问题。
有谁知道如何解决这个问题?我想在不使用 std::string 的情况下输入最多 100 个字符。

4

1 回答 1

4

这是 libc++ 中的一个错误。我很抱歉。它固定在山狮身上。您可以通过使用来解决它getline(istream&, string&)

#include <iostream>
#include <string>

using namespace std;

int main (int argc, char** argv) 
{
    std::string buffer;
    getline(cin, buffer);
    cout << buffer << endl;
    return 0;
}
于 2012-07-05T13:57:53.320 回答