繁体   English   中英

在 c++ 中使用 getline function 输入时忽略第一个字符

[英]Ignoring the first character while using getline function for inputs in c++

为什么在 c++ 程序中使用 getline 进行输入会自动忽略我输入的第一个字母? 我应该怎么做才能解决这个问题?

编辑 1:我在同一程序的其他功能中使用了 getline 以及 cin.ignore() 但它在那里工作正常。


    cout<<"Enter the roll no: ";        
    cin.ignore();
    getline(cin,rollno);
    cout<<"Enter the name of book you want to issue:";  
    cin.ignore();                                       
    getline(cin,input);                 //input= think and row rich
    cout<<"What's the return data";
    cin.ignore();
    getline(cin,input_date);           // input=20-8-2019

    cout<<input<<" "<<input_date<<endl;      //output of inputs = hink and grow rich and 0-8-2019

如果我尝试在没有 cin.ignore() 的情况下接受输入,程序不会接受任何输入..

    cout<<"Enter the roll no: ";
    istream& ignore (streamsize n = 1, int delim = EOF);  //by using this line my code starts working properly.
    cin.ignore();
    getline(cin,rollno);
    cout<<"Enter the name of book you want to issue:";
    getline(cin,input);
    cout<<"What's the return data";
    getline(cin,input_date);

上面的代码现在一个一个地输入rollno,书名和日期,不忽略任何字符。谢谢大家的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM