【问题标题】:getline() function is skipping inputsgetline() 函数正在跳过输入
【发布时间】:2016-02-06 15:49:55
【问题描述】:

我有以下代码对 nomC 和 nomP 进行了一些测试,但是使用 2 个连续的 getlines 会导致跳过第一个 (getline(cin,nomP); ) .. 我该如何解决这个问题? PS:我试过 cin.ignore();和 cin.clear();它没有工作

#include <iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
    int T;
    cin >> T;
    vector<string> decision(T);
    for(int i=0;i<T;i++)
    {
         string nomP,nomC;
        string help="";
        vector<string> desc;
        bool accepted = true;
        getline(cin,nomP);
        getline(cin,nomC);
        while(help!="----")
        { getline(cin,help); desc.push_back(help);}
        if ((nomP.size()<5)|| (nomP.size()>20))
          {decision[i] = nomC+" rejected with error code 1.\n";accepted=false;}
        if (nomP[0]<65|| nomP[0]>90)
            {decision[i] = nomC+" rejected with error code 2.\n";accepted=false;}
        if (nomP[nomP.size()]==32)
            {decision[i] = nomC+" rejected with error code 3.\n";accepted=false;}
        if((nomC.size()<5)|| (nomC.size()>10))
            {decision[i] = nomC+" rejected with error code 4.\n";accepted=false;}
        for(int j=0;j<nomC.size();j++)
        {
            if(((nomC[j]<48)&&(nomC[j]>57))||((nomC[j]<97)&&(nomC[j]>122)))
            {decision[i] = nomC+" rejected with error code 5.\n";accepted=false;break;}
        }
        if (desc.size()>10)
            {decision[i] = nomC+" rejected with error code 6.\n";accepted=false;}
        for(int j=0;j<desc.size();j++)
        {
            if((desc[j].size()<1)||(desc[j].size()>80))
            {decision[i] = nomC+" rejected with error code 7.\n";accepted=false;break;}
        }
        if (accepted)
            decision[i] = nomC+" is OK.\n";
    }
    for (int i=0;i<decision.size();i++)
    {
        cout<< decision[i] << endl;
    }
return 0;
}

【问题讨论】:

  • 您试图将同一行读入两个不同的变量?对 getline() 的每次调用都会从 cin 中读取一行,因此您应该只进行一次调用,然后将 nomP 复制到 nomC。
  • 但我希望将 nomP 和 nomC 作为单独的变量.. 它们应该包含不同的字符串.. 关于如何做到这一点的任何想法??

标签: c++ cin getline


【解决方案1】:

这样看你的程序

int T;
cin >> T;

控制台输入:5\n

您可能已经注意到了这个问题。你认为你得到的是一个 5,但它是一个 5 + 一个换行符。

控制台输入:名称\n

然后你调用 getline()

cin 缓冲区不是:名称\n,

实际上是:\n名称\n

因此,在第一个 getline 中,您正在读取单个“\n”

第二个,你终于读到了“Name\n”

有办法解决这个问题。一个正在做这个技巧

    while (isspace(cin.peek())) cin.ignore(); //dodge spaces, line breaks. 
    getline(cin, nomP);
    getline(cin, nomC);

我只使用 windows,但换行符可能是 \r\n 在另一个操作系统中,这就是为什么做一个 cin.ignore() 可能不够。所以这个技巧仍然有效。

但是有一个更好的方法:创建一个函数,它只在读取到非空行时才返回。比如:

string my_getline()
{
    string result;

    while (!getline(cin, result) || result.empty());

    return result;
}

string nomP = my_getline();
string nomC = my_getline();

使用 RVO,这与执行 getline(cin,nomP) 一样快,而且更简单。

【讨论】:

  • @OmArElGhati 没问题,也许接受我的回答是正确的? :D
  • 虽然我很欣赏这种情绪,但它与 getline 一样快并不完全正确。在您重复调用此函数的情况下,它将在每次调用时分配内存。堆分配非常慢。而 getline 重用缓冲区,因此它仅在一行大于之前的所有行时分配。在非病态输入上,getline 会快得多。
  • 他在每个循环中都擦除了两个字符串(因此释放了内存),并且他同时使用了两个字符串。 getline 如何重用正在使用的缓冲区?对于他的程序来说,它同样快。对于其他实际需要这种优化的情况,当然可以将字符串移出循环,并使用引用调用 my_getline。当然。但是对于简单的任务,保持简单易读更好。
  • @OmArElGhati 错误的答案被接受为正确的答案有点令人遗憾。您是否知道您可以发送多行作为控制台输入,并且像他一样执行 cin.ignore(2459259234) 会使您的代码在这种情况下中断?是的,他可能指出了你最初遇到的更多问题,但你只问了一个非常具体的问题,我给了你正确的答案,而他没有。将来,人们最终会从谷歌或其他地方来到这里,最终会得到错误的答案。但无论如何。
  • 如果他试图在自己的行上输入他的循环整数,并且不想输入任何其他行,这似乎是他的程序的情况,然后清除缓冲区after 不会破坏任何东西。我带着这个想法写了我的答案。您的解决方案通常用于输入数据,我的解决方案专门用于getline()cin 读取数据后返回换行符,仅此而已。
【解决方案2】:

问题是从cin 获得迭代次数后,您并没有清除换行符。因此,当您第一次调用 getline() 时,它会愉快地抓取所有内容,直到您无意中留下的换行符,而不是获得单独的行。

尝试输入以下输入,您会明白我的意思:

2 Fred
Ted
----
Bob
Joel
----

试试这个:

#include <limits>

// ...

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

这应该会在输入循环大小后清除线上的所有内容。


我还想建议在程序中添加提示,至少在故障排除期间是这样。这样更容易看到正在发生的事情。

std::cout << "Input number of iterations: ";
cin >> T;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// ...

std::cout << "Input first name: ";
getline(cin,nomP);
std::cout << "Input second name: ";
getline(cin,nomC);

while(help!="----")
{
    std::cout << "Input separator: ";
    getline(cin,help);
    desc.push_back(help);
}

此外,我想指出您的错误代码设置的两个潜在问题:遇到的最后一条错误消息将覆盖以前的任何错误消息,并且您的所有错误消息都说问题出在nomC,无论是否他们正在检查nomPnomC

// Issues pointed out with C-style comments.
if ((nomP.size()<5)|| (nomP.size()>20))
{
    /* Checks nomP, says error is in nomC. */
    decision[i] = nomC + " rejected with error code 1.\n";
    accepted=false;
}
/* If conditions are met for both this and the previous error message, this overwrites the
   previous error message.
 */
if (nomP[0]<65|| nomP[0]>90)
{
    /* Checks nomP, says error is in nomC. */
    decision[i] = nomC + " rejected with error code 2.\n";
    accepted=false;
}
/* If conditions are met for both this and the previous error message, this overwrites the
   previous error message.
 */
if (nomP[nomP.size()]==32)
{
    /* Checks nomP, says error is in nomC. */
    decision[i] = nomC + " rejected with error code 3.\n";
    accepted=false;
}
/* If conditions are met for both this and the previous error message, this overwrites the
   previous error message.
 */
if((nomC.size()<5)|| (nomC.size()>10))
{
    decision[i] = nomC + " rejected with error code 4.\n";
    accepted=false;
}
for(int j=0;j<nomC.size();j++)
{
    /* If conditions are met for both this and the previous error message, this overwrites
       the previous error message.
     */
    if(((nomC[j]<48) && (nomC[j]>57)) || ((nomC[j]<97) && (nomC[j]>122)))
    {
        decision[i] = nomC + " rejected with error code 5.\n";
        accepted=false;
        break;
    }
}
/* If conditions are met for both this and the previous error message, this overwrites the
   previous error message.
 */
if (desc.size()>10)
{
    /* Checks number of strings in desc, says error is in nomC. */
    decision[i] = nomC + " rejected with error code 6.\n";
    accepted=false;
}
for(int j=0;j<desc.size();j++)
{
    /* If conditions are met for both this and the previous error message, this overwrites
       the previous error message.
     */
    if((desc[j].size()<1) || (desc[j].size()>80))
    {
        /* Checks string in desc, says error is in nomC. */
        decision[i] = nomC + " rejected with error code 7.\n";
        accepted=false;
        break;
    }
}

有两种处理方法:

  1. 如果您一次只关心一条错误消息,您可以不理会它或将if 语句更改为else if 语句。 (对于for 循环中和之后的if 语句,您可以添加一个条件来检查decision[i] 是否已经存在一个字符串。)
  2. 如果您想获取每条错误消息,请使用decision.push_back(),而不是直接分配给decision[i]

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    相关资源
    最近更新 更多