CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2008
    Posts
    2

    Using getline with cin

    Hi. I am trying to get input as a string and when I do this code segment:
    Code:
        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?

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Using getline with cin

    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Using getline with cin

    Usually this happens because there's already a \n in the stream from a previous cin >>.

  4. #4
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Using getline with cin

    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.
    - Alon

  5. #5
    Join Date
    Apr 2008
    Posts
    2

    Re: Using getline with cin

    The problem was the extra \n from a previous cin. It's working now. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured