cin for strings?

This is a discussion on cin for strings? within the C++ Programming forums, part of the General Programming Boards category; cin >> string; If you type a string with spaces, you only get the first word. Why is this?...

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368

    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
    712
    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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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,662
    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, 11:42 AM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21