Difference between getline and get | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Difference between getline and get

What is difference between just plain cin >>, cin.get(), and getline() in c++?

17th Mar 2019, 1:14 AM
Dan
Dan - avatar
8 Answers
+ 2
Using cin.getline() reads a whole line, up to, but not including the Enter key. Using cin.get() is the same, except it keeps the newline character. Using just cin only takes characters to the first white space.
17th Mar 2019, 2:00 AM
Mike
Mike - avatar
+ 2
I wasn’t sure so did a quick search and found this..... cin leaves the newline character in the stream. Adding cin.ignore() to the next line clears/ignores the newline from the stream. http://www.cplusplus.com/forum/beginner/9148/ So if you just use cin and enter something without a space, the newline will be included in the input. The cin.ignore() will then remove the newline character.
17th Mar 2019, 12:55 PM
Mike
Mike - avatar
+ 1
I’m currently studying c++ as well so don’t mind helping out. I’ve known perl for 18 years, got into php 10 years ago, and started being interested in c a few years ago. Decided recently to start studying it before I get too old to start.
17th Mar 2019, 5:52 PM
Mike
Mike - avatar
0
Mike Thank you. My professor is very quick with his slides and if often bewildered by his presentation. If you don’t mind, could you also explain the cin.ignore method and what types of arguments we could pass into cin.ignore(), cin.getline(), and cin.get()?
17th Mar 2019, 4:10 AM
Dan
Dan - avatar
0
Oh I see. Thanks again, Mike! I really appreciate you putting your time into this.
17th Mar 2019, 1:42 PM
Dan
Dan - avatar
0
That’s wonderful Mike. I am currently a freshmen in college taking the first computer science course and I am starting to like c++ even though it gets weird at times. And that is when I go to online communities like this to get help and fill in the gaps.
18th Mar 2019, 12:27 AM
Dan
Dan - avatar
0
What is the difference between gets and getline in C++? get() extracts char by char from a stream and returns its value (casted to an integer) whereas getline() is used to get a line from a file line by line. that is gets does the work letter to letter and whereas in the case of getline does it in line by line format
18th Mar 2019, 9:29 AM
deepak sharma
deepak sharma - avatar
0
thanks deepak sharma but that’s not the only difference though, right?
19th Mar 2019, 11:08 AM
Dan
Dan - avatar