getline(function)??

This is a discussion on getline(function)?? within the C++ Programming forums, part of the General Programming Boards category; is this fuction surposed to stop until i enter something until i press enter, cause it just jumps to the ...

  1. #1
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147

    getline(function)??

    is this fuction surposed to stop until i enter something until i press enter, cause it just jumps to the next line of code.

    getline(cin, bookers[number_of_bookers].name, '\n');

    thanks dac,

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    getline(cin????????????????? ,check the syntax

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    try cin.ignore(1); just before the function call ???

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by qqqqxxxx
    getline(cin????????????????? ,check the syntax
    Perfectly valid syntax for the version of getline which works with string objects.


    Quote Originally Posted by dac
    is this fuction surposed to stop until i enter something until i press enter, cause it just jumps to the next line of code.

    getline(cin, bookers[number_of_bookers].name, '\n');

    thanks dac,
    What compiler/IDE do you use. There is a known issue with the string version of the getline function in MSVC 6 that can be resolved by applying the fixes found at this site or for that fix and even more MSVC 6 fixes (highly suggested) you can go here.

    Alternately it might not be that at all, but rather a problem with there being a newline character leftover in the input stream from a previous input operation. The following would demonstrate this problem:
    Code:
    char var1;
    string str1;
    cin >> var1;
    getline(cin,str1,'\n');  // This line will get "skipped"
    The cin >> operation will get a character from the input stream and wait for a newline (which will not be read) to be pressed. When the getline function is reached it reads up until the first newline character and moves on. Since there is already a newline still left in the input stream from the previous cin >> the getline function immediately exits and we move on to the next line of code making it look like the getline call was skipped (really wasn't just looks that way). The solution to this is to clear the input stream before each getline function call that comes after a cin >> type of operation. This is done by the use of the stream's ignore member function (as mentioned by arjunajay):
    Code:
    char var1;
    string str1;
    cin >> var1;
    cin.ignore(numeric_limits<streamsize>.max(),'\n');
    getline(cin,str1,'\n');  // This line will not get "skipped"
    You need to include <limits> for numeric_limits.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by qqqqxxxx
    getline(cin????????????????? ,check the syntax
    syntax check on register 5: http://www.cppreference.com/cppstring/getline.html
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Another common variant to take care of newlines in the input is
    Code:
    if(std::getline(std::cin >> std::ws,line) {
    // Process line
    }
    std::ws is a manipulator that accepts only whitespace. You can think of it as a sort of dummy variable. The only time when you would not want to use it is if leading whitespace is significant. With the above " Hello\n" and "Hello\n" are the same. Normally you would want this.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    1) your syntax is wrong
    2) yuck.

    I'd stick with using ignore after a cin
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The cin >> operation will get a character from the input stream and wait for a newline (which will not be read) to be pressed. When the getline function is reached it reads up until the first newline character and moves on. Since there is already a newline still left in the input stream from the previous cin >> the getline function immediately exits and we move on to the next line of code making it look like the getline call was skipped
    Some further explanation...

    When you use a line like:

    cin>>firstName;

    the program looks for some input to read from cin(cin represents input from the keyboard). Since cin starts off empty, there is no input to read, and the program must wait for the user to enter some input. After the user enters some input, say 'Bob', and hits return, the return causes a newline(\n) to be added to the input, so this is what's in cin:

    Bob\n

    The operation 'cin>>' is defined to stop reading input when it encounters whitespace(spaces, tabs, newlines), so 'Bob' is read from cin, and then cin>> stops reading input, leaving the trailing '\n' in cin. Any unread data in cin remains there and will be read during the next read operation.

    getline() works differently than cin>>. getline() reads in input until after it reads in a \n whereupon it immediately stops. So with a \n remaining in cin, getline() does not have to wait for more user input--there is already input in cin that getline() can read: the \n. Therefore, getline() reads the \n and stops, and execution continues with the next line in your program--making it seem like getline() was skipped.

    Note that if you had used cin>> the second time you read from cin instead of getline(), the result would have been different. The operation 'cin>>' is defined to skip all leading whitespace, so cin>> would have skipped over the \n remaining in the input stream. Since cin would be unable to find any input to read after the \n, cin>> would have to wait for the user to enter more input.

    So, the lesson is: when you use both cin>> and getline() in a program, you have to be aware that the operation 'cin>>' can leave a trailing \n remaining in cin, which will mess up a subsequent getline(). You can solve that problem by erasing any previous input remaining in cin using ignore().
    Last edited by 7stud; 03-10-2006 at 03:39 PM.

Popular pages Recent additions subscribe to a feed

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