Hi All,

If the user is to input text - alphabetic or numeric, when is it advantageous to use getline rather than cin? It's obviously easier to code something like cin >> favoriteVacationSpot; than something like getline (cin, favoriteVacatioSpot); , so I don't see the advantage of using getline. If anyone can elaborate somewhat on the differences it would be greatly appreciated.

TIA

Recommended Answers

All 7 Replies

Enter text containing whitespace. (And do you want to leave whitespace, including a newline, in the input buffer?)

Oh, and always check return values.

Enter text containing whitespace...

Thanks! I could have sworn that I had written programs already that used cin with multi-alphabetic characters and whitespaces, but, after opening around 50 files or so to check, all of them had cin used in conjunction with char, or some numeric input. BIG guess here is that that's because string inputs hadn't been covered until getline was discussed! ;)

...(And do you want to leave whitespace, including a newline, in the input buffer?)...

...for which I understand the standard procedure is to flush memory with cin.ignore(80, '\n'); - correct?

...Oh, and always check return values.

Unless you're referring to the return statement at the end of a program (" return = 0; "), then I should hope that that process will be covered shortly.

Thanks again Dave! :)

...for which I understand the standard procedure is to flush memory with cin.ignore(80, '\n'); - correct?

Perhaps. I prefer the "read all input as a string and convert (if necessary)".

Unless you're referring to the return statement at the end of a program (" return = 0; "), then I should hope that that process will be covered shortly.

Both operator>> and getline return a value indicating whether the call was successful. Checking to see whether you actually obtained the input you requested is always a good thing before continuing on to use what you may (or may not) have really gotten.

Hi Dave,

First off, thanks for the timely reply posts - very much appreciated! As for your latter two points, now that you've brought them up, I can see how they would be - and will be - good practices to follow. I presume that these procedures will be covered in due time during the course of the class that I'm taking, but if not I'll hunt them down in order to learn them as extracurricular subject matter.

As for your latter two points, now that you've brought them up, I can see how they would be - and will be - good practices to follow. I presume that these procedures will be covered in due time during the course of the class that I'm taking, but if not I'll hunt them down in order to learn them as extracurricular subject matter.

I hope so, but I doubt it. (That's what keeps forums such as these thriving!) ;)

I hesitate to enter this discussion, but I can't help myself given how long it took me to understand the difference between >> and getline() myself, and even now I'm not confident that I have it all correct.

I prefer the "read all input as a string and convert (if necessary)".

This is fine policy, but it begs the point as you can input put a string using either >> or getline() and therefore get tripped up by information remaining in the input buffer by by calling >> before calling getline() even if input is only as a string.

To me, the difference between >> and getline() is behaviour. >> will input information into any variable type for which >> has been defined, skipping leading whitespace and leaving terminating char (whether whitespace or char not valid for given type) in the input buffer, whereas getline() only works for string variables and no other variable type, doesn't skip leading whitespace, and terminates input when a specified char is found in the input buffer (default terminating char is newline char, but it can be any legal char) or, in the case of C style strings, when a specified number of char have been read, and removes the terminating char (assuming that is the terminating event) from the input buffer.

ignore() is a common way to clear the input buffer, and ignoring 80, or some other large number, char will usually work. But there is a more sophisticated way, and that is to ignore as many char as a stream can hold rather than an arbitrary large number. Given the clunky syntax, it's something like this:

std::cin.ignore(std::numeric_limits<std::streamsize>::max());

and it requires listing the limits header file with your project, I generally use an arbitrary large number, like 80, recognizing that at some point some user will input material that will cause my program to fail. But, since I only write programs for my edification, and not for public use, that's okay.

Perhaps. I prefer the "read all input as a string and convert (if necessary)".

This is fine policy, but it begs the point as you can input put a string using either >> or getline() and therefore get tripped up by information remaining in the input buffer by by calling >> before calling getline() even if input is only as a string.

Perhaps I should have said, "read a whole line as a string ..."

So it's always getline . Then there are no leftovers to concern yourself with.

whereas getline() only works for string variables and no other variable type

If the data is numeric, I would imagine that much of the same thing that happens when using operator>> was simply skipped by taking the string as-is, so adding such stuff in explicitly later doesn't bother me. Doing so may also make it easier to handle any input errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.