std::cin.getline( ) vs. std::cin
Asked Answered
P

6

44

When should std::cin.getline() be used? What does it differ from std::cin?

Pennington answered 20/1, 2011 at 10:18 Comment(0)
B
25

In case with char*, std::cin.getline getting line, instead of std::cin getting first word.

Bertha answered 20/1, 2011 at 10:34 Comment(4)
I don't understand this answer. std::cin doesn't get anything; it's just an object. You have to do something with it (e.g. call a member function like get) before it does anything.Homunculus
char s1[256], s2[256]; std::cin >> s1; cin.getline(s2, 256);Bertha
std::cin >> s1 is a call to a global function istream& operator>> (istream&, char*)Coessential
This answer needs to be edited. The wording and explanation are confusingAlgor
G
47

Let's take std::cin.getline() apart. First, there's std::. This is the namespace in which the standard library lives. It has hundreds of types, functions and objects.

std::cin is such an object. It's the standard character input object, defined in <iostream>. It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from the standard input.

Finally, .getline() is one such method of std::cin (and other similar objects). You tell it how many characters it should get from the object on its left side (std::cin here), and where to put those characters. The precise number of characters can vary: .getline() will stop in three cases: 1. The end of a line is reached 2. There are no characters left in the input (doesn't happen normally on std::cin as you can keep typing) 3. The maximum number of characters is read.

There are other methods and functions that can be used with the std::cin object, e.g.

  std::string s;
  int i;
  std::cin >> s; // Read a single word from std::cin
  std::cin >> i; // Read a single number from std::cin
  std::getline(std::cin, s); // Read an entire line (up to \n) from std::cin
  std::cin.ignore(100); // Ignore the next 100 characters of std::cin
Galloromance answered 20/1, 2011 at 12:38 Comment(0)
B
25

In case with char*, std::cin.getline getting line, instead of std::cin getting first word.

Bertha answered 20/1, 2011 at 10:34 Comment(4)
I don't understand this answer. std::cin doesn't get anything; it's just an object. You have to do something with it (e.g. call a member function like get) before it does anything.Homunculus
char s1[256], s2[256]; std::cin >> s1; cin.getline(s2, 256);Bertha
std::cin >> s1 is a call to a global function istream& operator>> (istream&, char*)Coessential
This answer needs to be edited. The wording and explanation are confusingAlgor
S
10

Did you read any documentation (e.g. http://www.cplusplus.com/reference/string/getline/)?

Basically, std::cin (or more generally, any std::istream) is used directly in order to obtain formatted input, e.g. int x; std::cin >> x;. std::cin.getline() is used simply to fill a raw char * buffer.

Salomon answered 20/1, 2011 at 10:30 Comment(3)
the member getline doesn't get you a std::string, it's used for filling a char* buffer. The getline that gets you a std::string is the free function std::getline.Crews
@Oli. Yes, I saw the documentation for a bit but didn't get the idea. Now, I think it is more clear. Thanks.Pennington
Link is broken.Vaginectomy
P
4

(Very simplefied)My answer is, that std :: cin.getline() can contain spaces, while std :: cin >> can not.

Pervious answered 26/3, 2020 at 7:11 Comment(1)
It seems to be a good for a simple example. Thank you.Enswathe
L
1

As already others have answered (even better) roughly speaking, use getline() to read an entire line (i.e., a string terminating with \n) and cin>>var to read a number compatible with the type of var (integer, float, double etc.) or a single word.

In this answer I want to emphasize a problem that arises when mixing the two methods. When you do:

int a;
string s;
cin>>a;
getline(cin, s)

cin leaves an end of line, \n, character which is then read by getline();. It is possible to overcome this problem by using cin.ignore().

int a;
string s;
cin>>a;
cin.ignore();
getline(cin, s)
Lipoid answered 19/6, 2019 at 9:26 Comment(1)
cin.ignore() will only discard a single character, whereas cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); will discard the rest of the line, including the newline character. It doesn't seem meaningful to use cin.ignore() in this case, because cin>>a will only read a single word. Therefore, you cannot assume that the next character will be the newline character.Lucknow
E
0

The use of std::cin >> std::ws skips the whitespace, in particular the newline, and carries on reading where the actual content is entered.

int a;
string s;
cin>>a;
getline(cin>>ws, s)
Epiphany answered 30/12, 2023 at 23:51 Comment(2)
int a; string s; cin>>a; getline(cin>>ws, s)Epiphany
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Betweenwhiles

© 2022 - 2024 — McMap. All rights reserved.