English 中文(简体)
st
原标题:std::getline does not work inside a for-loop
  • 时间:2010-01-11 04:29:13
  •  标签:
  • c++
  • getline

I m试图收集用户在一定时间接受白天空间的狭小变量中的投入。

自通常的<代码>cin >>str • 接受白天,因此,我 with住了以下几条:从“带”到植物;str;

我的守则是:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        string local;
        getline(cin, local); // This simply does not work. Just skipped without a reason.
        //............................
    }

    //............................
    return 0;
}

任何想法?

最佳回答

如果你生产<条码>当地<<>/代码>所储存的物品,你可以理解为什么会失败。 (用以下方式标明:P)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        string local;
        getline(cin, local);
        std::cout << "> " << local << std::endl;
    }

    //............................
    return 0;
}

您将在其编号之后立即印出一条新线:>。 然后,它开始投入其余部分。

This is because getline is giving you the empty line left over from inputting your number. (It reads the number, but apparently doesn t remove the , so you re left with a blank line.) You need to get rid of any remaining whitespace first:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cin >> ws; // stream out any whitespace
    for(int i = 0; i < n; i++)
    {
        string local;
        getline(cin, local);
        std::cout << "> " << local << std::endl;
    }

    //............................
    return 0;
}

这一工程是预期的。

专题,或许只针对手头的刀子,但如果你没有密码,则代码往往为more可读;。 它破坏了名称空间的目的。 但我怀疑这只是在这里张贴。

问题回答

Declare a character to get in the carriage return after you have typed in the number.char ws;int n;cin>>n;ws=cin.get(); This will solve the problem.

Using cin>>ws instead of ws=cin.get(),will make first character of your string to be in variable ws,instead of just clearing .

It s quite simple. U jst need to put a cin.get() at the end of the loop.

你们是否进入了袭击? 如果不走一条路,那不会再返回,因为它正在等待线的结束......

我的猜测是,你没有正确读到<条码>n,因此改成零。 由于0不低于0,该休息室从未执行过。

一、导 言

int n;
cin >> n;
std::cerr << "n was read as: " << n << "
"; // <- added instrumentation
for // ...

why this happens : This happens because you have mixed cin and cin.getline. when you enter a value using cin, cin not only captures the value, it also captures the newline. So when we enter 2, cin actually gets the string “2 ”. It then extracts the 2 to variable, leaving the newline stuck in the input stream. Then, when getline() goes to read the input, it sees “ ” is already in the stream, and figures we must have entered an empty string! Definitely not what was intended.

old solution : A good rule of thumb is that after reading a value with cin, remove the newline from the stream. This can be done using the following :

std::cin.ignore(32767,  
 ); // ignore up to 32767 characters until a 
 is removed

A better solution : use this whenever you use std::getline() to read strings

std::getline(std::cin >> std::ws, input); // ignore any leading whitespace characters

st : : : get input input input ignore ignore ignore

source : learncpp website goto section (Use std::getline() to input text)

希望这将有助于

  • Is n properly initialized from input?
  • You don t appear to be doing anything with getline. Is this what you want?
  • getline returns an istream reference. Does the fact that you re dropping it on the ground matter?

你在哪位汇编者试图这样做? 我于2008年5月接受了审判,并进行了罚款。 如果我编纂了有关g++(GCC)3.4.2的同一法典。 它没有适当开展工作。 下面是两个汇编者使用的版本。 在我的环境下,I dont t拥有最新的G++编制者。

int n;
cin >> n;
string local;
getline(cin, local); // don t need this on VC2008. But need it on g++ 3.4.2. 
for (int i = 0; i < n; i++)
{
    getline(cin, local);
    cout << local;
}

重要的问题是,“你在做些什么,使你认为投入是ski的?” 或者,更确切地说,“你认为投入是微调的吗?”

如果你重新穿透,你是否以优化方式汇编(允许重订指示)? 我认为这不是你的问题,但这是一个可能性。

我认为,越有可能有人居住,但处理不正确。 例如,如果你想通过对旧C功能的投入(例如,atoi(>),你将需要提取C风格的体格( local.c_str(<><>)。

您可直接利用网上功能,使用限定词如下:

#include <iostream>
using namespace std;
int main()
{
    string str;
    getline(cin,str, # );
    getline(cin,str, # );
}

你可以像你所希望的那样,提出大量意见,但此处适用的一个条件是,你需要通过限定(第3条论点),即,无论新路线性质如何,扼杀将接受投入,直到第2号。

Before getline(cin, local), just add if(i == 0) { cin.ignore(); }. This will remove the last character ( ) from the string, which is causing this problem and its only needed for the first loop. Otherwise, it will remove last character from the string on every iteration. For example,

i = 0 -> string
i = 1 -> strin
i = 2 -> stri

等等。

仅限使用<代码>cin.sync()。

只是增加斜线,然后才能开展工作。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        string local;
        cin.ignore();
        getline(cin, local);
    }

    return 0;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签