Difference between getline and get | Sololearn: Learn to code for FREE!

+2

Difference between getline and get

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

3/17/2019 1:14:59 AM

Dan

8 Answers

New Answer

+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.

+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.

+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.

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()?

0

Oh I see. Thanks again, Mike! I really appreciate you putting your time into this.

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.

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

0

thanks deepak sharma but that’s not the only difference though, right?