Thread: Getting strings with Cin

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Getting strings with Cin

    To my understanding when I

    Code:
    cin >> input
    and the 'input' is a long string with spaces inbetween, ie

    Code:
    "Hello out there."
    Only "Hello" will be stored in 'input' because of the white spaces. How do I get the entire string in 'input'?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    getline
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that if you use cin >> to read in integers or any other values and then use getline to read in the string, you will probably end up reading in an empty line. Make sure to add a cin.ignore() after calls to cin >> to ignore the newline that will stop getline from getting any data.
    Last edited by Daved; 05-07-2007 at 03:30 PM.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Thanks! I was just about to ask about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. using cin and strings
    By Heineken in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 03:13 PM
  4. cin for strings?
    By samGwilliam in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2005, 01:35 PM
  5. cin strings till eof
    By bfedorov11 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2003, 07:27 AM