c++ - std::getline 输入在 C++ 中无法正常工作

标签 c++ newline getline cin

<分区>

Possible Duplicate:
Need help with getline()
getline not asking for input?

我正在编写以下代码:

int main()
{
    int num;
    string str;
    cin>>num;
    int points[num][2];
    for(int i=0;i<num;i++)
    {
        cout<<"\nPoint"<<i<<":";
        getline (cin,str);
        points[i][0]=atoi(&str[0]);
        points[i][1]=atoi(&str[2]);
    }

    for(int i=0;i<num;i++)
    {
        cout<<"\npoint"<<i<<" = "<<points[i][0]<<" "<<points[i][1];
    }

我得到的上述代码的问题是当我输入 num 值作为某个整数然后按 enter,而不是打印...

“第 0 点:”

...等待我输入,它会打印“Point 0:”和“Point 1:”,然后获取 Point 1 的输入。

对于点 0,它会自动将输入作为 0 和 0。

最佳答案

您的程序在以下时间后没有使用换行符:

cin >> num;

传统的做法是这样的:

std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');

numeric_limits<limits> 中定义.

关于c++ - std::getline 输入在 C++ 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7887314/

相关文章:

c++ - 在 OSX El Capitan 中使用 Homebrew 的 Boost::Log 链接错误

c++ - VS 代码 C++ : inaccurate system includePath errors (wchar. h,boost/lambda/lambda.hpp)

javascript - 多行 JavaScript 文本

c++ - 如何使用 getline() 终止

C++ getline(), 第二次运行这个函数不起作用

c++ - boost asio async_read_until : how to raise error from a custom MatchCondition?

c++ - 预加载运算符删除释放大小

c++ - 从 txt 文件中读取以字符分隔的元素

java - 将 xml 解析为 DOM 时保留换行符的十进制值

php - 正则表达式:从部分字符串中删除换行符 (PHP)