getline() 函数正在跳过输入

getline() function is skipping inputs

我有以下代码对 nomC 和 nomP 进行一些测试,但是使用 2 个连续的 getline 会导致跳过第一个 (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;
}

这样看你的程序

int T;
cin >> T;

Console input: 5\n

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

Console input: Name\n

然后调用getline()

cin buffer is not: Name\n,

it's actually: \nName\n

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

和第二个,你终于阅读 "Name\n"

有多种方法可以解决此问题。一个在做这个把戏

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

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

但是有一个更好的方法:创建一个函数,returns 只有当它读取了一个非空行时。类似于:

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) 一样快,而且更简单。

问题是,在您从 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 ,无论他们检查的是 nomP 还是 nomC.

// 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]