What getline really do | Sololearn: Learn to code for FREE!


What getline really do

I want to now why we have to do a cin.clear () between a cin and a getline...


11/6/2016 8:02:19 PM


4 Answers

New Answer


You actually need to flush (clean the buffer) between using cin and get line. cin leaves \n in the buffer so get line sees it and stops reading, since it only reads until it sees \n. cin.ignore() is used for that. I don't understand if your input was 2 lol lolipop with or without \n. Both terminal and codeblocks would put 2 in aVariable and end your program.


cin only takes what's before first space. For example, if type in Solo Learn, it'll only register Solo and ignore Learn. getline is used when you want the whole line. It will register everything you type in before enter. So if you use getline, Learn won't be ignored.


yep but if you do int aVariable; string anotherOne; cin>>aVariable; getline (cin,anotherOne); ________________________ input : 2 lol lolipop _________________________ it will only take the second one (lolipop ). why ?


ok thinks