cin.getline()在C ++中跳过一个输入 [英] cin.getline() is skipping an input in C++

查看:421
本文介绍了cin.getline()在C ++中跳过一个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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;
    }
}

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

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

#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;
    }
}

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

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".

如何摆脱这个问题?

推荐答案

cin>>n;

仅读取数字。

它留下的尾部<$ c $

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

因此,您第一次调用 getline()正在读取只包含'\\\
'

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

的空行混合使用 operator>> std :: getline()。你必须非常小心,你是否已离开换行。我发现最容易总是从用户读取一行。

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.

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

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

这篇关于cin.getline()在C ++中跳过一个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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