Help with getline()

This is a discussion on Help with getline() within the C++ Programming forums, part of the General Programming Boards category; Code: cout << "Enter employee name, followed by return.: "; getline(cin, name); cout << "Enter employee social security number, followed ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Help with getline()

    Code:
    cout << "Enter employee name, followed by return.: ";
    	getline(cin, name);
    	cout << "Enter employee social security number, followed by return.: ";
    When I run a program with the above, it makes me press Enter twice after entering the name. What is wrong with me and my program?

    Thank you,
    Sean Michael Simonsen

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Re: Help with getline()

    Woot! Krak knows how to fix this. Here's how cin.getline() works:

    Code:
    cin.getline(string name, max char, terminating char);
    It would really work like this:
    Code:
    char string[256];
    cin.getline(string,256,'\n');
    You see, it takes 3 parameters. The third one is the terminating character. Since '\n'(new line) is the terminating character, when you press Enter, the string terminates.

  3. #3
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    >> cin.getline(string name, max char, terminating char);

    >> cin.getline(string,256,'\n');

    So if 256 is the maximum amount to write, and the user enters 256 chars, would the 256th char be cropped off and replaced with '/n'?
    I AM WINNER!!!1!111oneoneomne

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You're not using MSVC++ 6 are you? If you are, it's a bug. You can find a fix for it here:
    http://cmpsc.acs.edcc.edu/pbladek/getline_fix.htm

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Originally posted by Panopticon
    >> cin.getline(string name, max char, terminating char);

    >> cin.getline(string,256,'\n');

    So if 256 is the maximum amount to write, and the user enters 256 chars, would the 256th char be cropped off and replaced with '/n'?
    No, they wouldn't. The string terminates if 257+ characters are entered, or if you press '\n'(New line(Enter))

  6. #6
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    257+
    It would make sense if that were 256+ instead. I understand now, thanks.
    I AM WINNER!!!1!111oneoneomne

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Originally posted by Panopticon
    It would make sense if that were 256+ instead. I understand now, thanks.
    Why would it make more sense? You have to count 0 also since that would be the first character.

  8. #8
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Exactly, if you count 0, and the array was declared as having 256 elements, (being [255] as its last element) it still won't make a difference to how much the array can store...

    What are you saying anyway?
    I AM WINNER!!!1!111oneoneomne

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    EDIT: Nevermind, I am going to stay out of it now since I have already confused myself.
    Last edited by Munkey01; 02-04-2003 at 09:42 PM.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    getline reads in at most n-1 characters as it always writes a terminating '\0'. Thus in this case if the user were to enter 257 characters then two characters (plus the newline) would be left in the buffer. You can use cin.gcount() to get the actual number of characters read by the last block read.

    getline(cin,str) reads characters into a std::string str until '\n' (by default) or eof expanding the string as needed.

    Eibro's solution appears to be the correct one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

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