+ 2
What getline really do
I want to now why we have to do a cin.clear () between a cin and a getline...
4 Antworten
+ 3
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.
+ 1
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.
0
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 ?
0
ok thinks