std :: getline输入在C ++中无法正常工作 [英] std::getline input not working properly in C++

查看:180
本文介绍了std :: getline输入在C ++中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
需要getline()帮助
getline是否不要求输入?

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键而不是打印时...

The problem with the above code that I am getting is when I enter the num value as some integer and then press enter, instead of printing...

点0:"

...,等我输入时,将打印"Point 0:"和"Point 1:",然后输入Point 1的输入.

...and waiting for me to enter it prints "Point 0:" and "Point 1:" and then takes the input for Point 1.

对于点0,它会自动将输入设为0和0.

For point 0 it automatically takes input as 0 and 0.

推荐答案

下面是约翰·卫斯理王子的回复,请尝试

Following on from Prince John Wesley's reply, try

    cin >> num >> endl;

先刷新流缓冲区,然后再使用它.

to flush the stream buffer before using it again.

这篇关于std :: getline输入在C ++中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆