difference between cin.get() and cin.getline()
Asked Answered
D

5

15

I am new to programming, and I have some questions on get() and getline() functions in C++.

My understanding for the two functions:

The getline() function reads a whole line, and using the newline character transmitted by the Enter key to mark the end of input. The get() function is much like getline() but rather than read and discard the newline character, get() leaves that character in the input queue.

The book(C++ Primer Plus) that I am reading is suggesting using get() over getline(). My confusion is that isn't getline() safer than get() since it makes sure to end line with '\n'. On the other hand, get() will just hangs the character in the input queue, thus potentially causing problem?

Doucette answered 11/11, 2014 at 19:58 Comment(0)
E
22

There are an equivalent number of advantages and drawbacks, and -essentially- all depends on what you are reading: get() leaves the delimiter in the queue thus letting you able to consider it as part of the next input. getline() discards it, so the next input will be just after it.

If you are talking about the newline character from a console input,it makes perfectly sense to discard it, but if we consider an input from a file, you can use as "delimiter" the beginning of the next field.

What is "good" or "safe" to do, depends on what you are doing.

Ernieernst answered 11/11, 2014 at 20:16 Comment(2)
Thank you for your answer, I have another question regarding to this matter. What if the input characters exceed the character limit for both of these functions, what type of error would it possibly caused?Doucette
@YuZhou The maximum number of characters that can be entered by the user is std::numeric_limits<std::streamsize>::max(). If the stream's buffer runs out of memory then the error flag std::ios_base::badbit will be set.Disintegration
G
17

cin.getline() reads input up to '\n' and stops

cin.get() reads input up to '\n' and keeps '\n' in the stream

For example :

char str1[100];
char str2[100];
cin.getline(str1 , 100);
cin.get(str2 , 100);
cout << str1 << " "<<str2;

input :
1 2
3 4
output 1 2 3 4 // the output expexted

When reverse them
For example :

char str1[100];
char str2[100];
cin.get(str2 , 100);
cin.getline(str1 , 100);
cout << str1 << " "<<str2;

input :
1 2
3 4
output 1 2 // the output unexpexted because cin.getline() read the '\n'

Gauntlett answered 22/3, 2019 at 21:59 Comment(3)
Really nice explanation. The flipping of the code lines to see the different results is an unforgettable example! Learnt a lot today thanks to you!Marybellemarybeth
@congar cin.getline() not only read upto \n and stops but it also discards it from input streamOsy
@congar https://mcmap.net/q/113194/-when-and-why-do-i-need-to-use-cin-ignore-in-c great explanationOsy
H
9

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. Normally getline is used to filter out delimiters in applications where you have a flat file(with thousands of line) and want to extract the output(line by line) using certain delimiter and then do some operation on it.

Harim answered 11/11, 2014 at 20:36 Comment(0)
P
1

The difference between get() and the getline() functions is that the getline() function extracts the delimiting character but does not place it in string. Whereas the get() function does not extract the delimiting character from the input buffer

Pacificism answered 29/9, 2021 at 17:15 Comment(0)
D
-2

cin.get() takes the input of whole line which includes end of line space repeating it will consume the next whole line but getline() is used to get a line from a file line by line.

Downcast answered 28/3, 2018 at 0:56 Comment(1)
You have this exactly backwards. cin.get() reads a single character from the input buffer and ignores white space.Pouch

© 2022 - 2024 — McMap. All rights reserved.