首页 文章

getline()函数正在跳过输入

提问于
浏览
1

我有以下代码对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;
}

2 回答

  • 0

    以这种方式看你的程序

    int T;
    cin >> T;
    

    控制台输入:5 \ n

    您可能已经注意到了这个问题 . 你认为你得到的是5,但它是一个5分线 .

    控制台输入:名称\ n

    然后你调用getline()

    cin缓冲区不是:名称\ n,它实际上是:\ nName \ 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)一样快,而且更简单 .

  • 1

    问题是,从 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;
        }
    }
    

    有两种方法可以解决这个问题:

    • 如果您一次只关注一条错误消息,可以不管它还是将 if 语句更改为 else if . (对于 for 循环中和之后的 if 语句,您可以添加一个条件来检查 decision[i] 处是否已有字符串 . )

    • 如果要获取每条错误消息,请使用 decision.push_back() 而不是直接分配给 decision[i] .

相关问题