简体   繁体   中英

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

Why taking input using getline in c++ program is automatically ignoring first letter of my input? What should i do to solve this problem?

edit 1: I have used getline in other functions of same program along with cin.ignore() but it is working fine there.


    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

If i try to take input without cin.ignore() program is not taking any input..

    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);

The above code is now taking input of rollno,book name and date one by one and not ignoring any character.Thanks everyone for help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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