Thread: cin for strings?

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    cin for strings?

    cin >> string;

    If you type a string with spaces, you only get the first word. Why is this?

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    cin reads a string up to (but not including) the first whitespace it encounters. Use cin.getline instead :
    Code:
    cin.getline(mystring, sizeof(mystring), '\n');
    first argument is your string , second speicifies the max input number , third is the delimeter (its '\n' by default so you don't have to include it).

    hope this helps
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Brain Cell
    cin reads a string up to (but not including) the first whitespace it encounters. Use cin.getline instead :
    Code:
    cin.getline(mystring, sizeof(mystring), '\n');
    first argument is your string , second speicifies the max input number , third is the delimeter (its '\n' by default so you don't have to include it).

    hope this helps
    Of course when mystring is char* ...
    Last edited by Micko; 04-03-2005 at 10:25 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by Brain Cell
    cin reads a string up to (but not including) the first whitespace it encounters. Use cin.getline instead :
    Code:
    cin.getline(mystring, sizeof(mystring), '\n');
    first argument is your string , second speicifies the max input number , third is the delimeter (its '\n' by default so you don't have to include it).

    hope this helps
    Bang on, baby!

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Brain Cell
    cin reads a string up to (but not including) the first whitespace it encounters. Use cin.getline instead :
    Code:
    cin.getline(mystring, sizeof(mystring), '\n');
    first argument is your string , second speicifies the max input number , third is the delimeter (its '\n' by default so you don't have to include it).

    hope this helps
    Bang on, baby!
    Actually, not quite, although Brain Cell may have gotten that from something I posted(which was incorrect--sorry!). It's actually the >>operator that is programmed to skip leading whitespace it sees, then start reading in data, and finally stop reading data when it encounters a whitespace character(space, tab, or newline)--not cin. If you learn about file I/O, you will see that the >>operator does the same thing with other streams besides cin.

    (I'll have to try and search for that post of mine, and correct it. )
    Last edited by 7stud; 04-03-2005 at 01:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. Problem Calling Destructor, and prob with cin.
    By imortal in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2003, 09:29 AM