Click to See Complete Forum and Search --> : Using getline with cin


abrowning
April 7th, 2008, 09:43 AM
Hi. I am trying to get input as a string and when I do this code segment:

string artist;
cout << "Enter the artist of the album you wish to delete ->";
getline (cin, artist);
it displays the prompt but then "Press any key to continue" and exits the program. iostream.h is included. Am I using getline incorrectly?

Philip Nicoletti
April 7th, 2008, 09:57 AM
You probably are doing a "cin >> somevariable" before that, which
leaves the new-line character in the stream buffer.

See : http://www.codeguru.com/forum/showthread.php?t=439902

Lindley
April 7th, 2008, 09:57 AM
Usually this happens because there's already a \n in the stream from a previous cin >>.

Hermit
April 7th, 2008, 10:03 AM
Also, it needs to be said: there's no such header as iostream.h in the C++ standard library. The correct header is iostream, no extension.

abrowning
April 7th, 2008, 10:05 AM
The problem was the extra \n from a previous cin. It's working now. Thanks!