cin.getline() 正在跳过 C++ 中的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10199897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:44:49  来源:igfitidea点击:

cin.getline() is skipping an input in C++

c++stringgetlinecin

提问by theharshest

If I use the following code, getline doesn't take the last input(for last iteration of "for" loop, it simply skips it) -

如果我使用以下代码,getline 不会接受最后一个输入(对于“for”循环的最后一次迭代,它只是跳过它)-

int main()
{
    int n;
    map<string, set<string> > lst;
    string c,s,c2;

    cin>>n;

    for(int i=0;i<n;i++)
    {
            getline(cin,c); // here it skips the input for last iteration
            stringstream ss;
            ss<<c;

            bool f=1;
            while(ss>>s)
            {
                        if(f)
                        {
                             c2=s;
                             f=0;
                        }
                        else
                             lst[c2].insert(s);           
            }
    }

    for (map<string, set<string> >::const_iterator ci = lst.begin(); ci != lst.end(); ++ci)
    {
                cout<< (*ci).first <<" "<< (*ci).second.size() <<endl;
    }
}

To get rid of it, I put cin.ignore() after getline. Now its taking all the inputs but I'm facing a new issue -

为了摆脱它,我将 cin.ignore() 放在 getline 之后。现在它接受了所有的输入,但我面临着一个新问题——

#include<iostream>
#include<string>
#include<map>
#include<set>
#include<sstream>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    map<string, set<string> > lst;
    string c,s,c2;

    cin>>n;

    for(int i=0;i<n;i++)
    {
            getline(cin,c);
            cin.ignore();
            stringstream ss;
            ss<<c;

            bool f=1;
            while(ss>>s)
            {
                        if(f)
                        {
                             c2=s;
                             f=0;
                        }
                        else
                             lst[c2].insert(s);           
            }
    }

    for (map<string, set<string> >::const_iterator ci = lst.begin(); ci != lst.end(); ++ci)
    {
                cout<< (*ci).first <<" "<< (*ci).second.size() <<endl;
    }
}

The new issue is that while taking c2, first character of string gets removed. For example, if I give "England Jane Doe" as input to getline, in c2 I'll get "ngland".

新问题是在取 c2 时,字符串的第一个字符被删除。例如,如果我将“England Jane Doe”作为 getline 的输入,在 c2 中我将得到“ngland”。

How to get rid of this issue now?

现在如何摆脱这个问题?

回答by Martin York

This:

这个:

cin>>n;

Is reading the number only.
It leaves the trailing '\n'on the stream.

只读取数字。
'\n'在流上留下尾随。

So your first call to getline()is reading an empty line containing just a '\n'

所以你的第一个调用getline()是读取一个只包含一个的空行'\n'

It is best not to mix the use of operator>>and std::getline(). You have to be very careful on whether you have left the newline on the stream. I find it easiest to always read a line at a time from a user. Then parse the line separately.

最好不要混合使用operator>>std::getline()。你必须非常小心你是否在流中留下了换行符。我发现每次从用户那里读取一行是最容易的。然后分别解析该行。

 std::string  numnber;
 std::getline(std::cin, number);

 int n;
 std::stringstream numberline(number);
 numberline >> n;

回答by Ben Cottrell

your cin.ignore()is in the wrong place. getline does not leave the trailing \nnewline character, it's the >>symbol which does that.

cin.ignore()在错误的地方。getline 不会留下尾随的\n换行符,它是>>这样做的符号。

What you probably want is

你可能想要的是

cin>>n;
cin.ignore();

回答by Vikas Kalwani

just write

写就好了

cin.ignore();

after your last ">>" operator. ">>" operator leaves a newline "\n" in the input bitstream which needs to be cleared before taking the inputs.

在最后一个“>>”运算符之后。">>" 操作符在输入比特流中留下一个换行符 "\n",在输入之前需要清除它。

回答by Rajeev

actually the cin buffer has \n remaining from last cin, so getline take this \n character and gets terminated; ignore the content of buffer before getline...

实际上 cin 缓冲区从最后一个 cin 中还剩下 \n,所以 getline 使用这个 \n 字符并被终止;在getline之前忽略缓冲区的内容...

cin.ignore();

cin.ignore();

getling(cin, string_name);

getling(cin, string_name);

//it will work smoothly

//它会顺利运行