difference between cin.get() and cin.getline()

55,051

Solution 1

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.

Solution 2

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'

Solution 3

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.

Share:
55,051
Yu Zhou
Author by

Yu Zhou

Updated on September 30, 2021

Comments

  • Yu Zhou
    Yu Zhou over 2 years

    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?

  • Yu Zhou
    Yu Zhou over 9 years
    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?
  • David G
    David G over 9 years
    @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.
  • Apekshik Panigrahi
    Apekshik Panigrahi almost 5 years
    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!
  • Abhishek Mane
    Abhishek Mane about 3 years
    @congar cin.getline() not only read upto \n and stops but it also discards it from input stream
  • Abhishek Mane
    Abhishek Mane about 3 years
    @congar stackoverflow.com/a/56406994/11862989 great explanation