Results 1 to 20 of 20

Thread: getline and cin

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    getline and cin

    I have a problem using cin and getline:
    Code:
    string str;
    getline(cin, str);
    This requires the user to hit enter twice. It seems that there is some buffer storing one string and getline retrieves only what is pushed out of the buffer. This is because if I do a
    cin.putback('\n');
    first, the user needs to hit enter only once after his input, but the retrieved string is only the \n I pushed to the input buffer. Likewise, when I do
    Code:
    string str;
    cout << "Enter a string:" << endl;
    getline(cin, str);
    cout << str << endl;
    cout << "Enter another string:" << endl;
    getline(cin, str);
    cout << str << endl;
    the program first asks for a string. Then it waits two times for input ( the user will probably just hit enter ), then output the first line, then ask for another string. While this time the user needs only input one string, the program will then output not this string, but rather the second one from the first getline call.

    Any ideas how to solve this?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    i've posted a question for this before, search the thread

    i've gotten an answer for it but it didn't work...i've attributed the
    answers that I've gotten to mean the people didn't know
    exactly what my problem was.

    I've never figured out the reason for this and I've just decided
    not to expect user input from a cmd line anymore :P
    Bababooey
    Tatatoothy
    Mamamonkey

  3. #3

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    the strange thing is that when you asked it, it worked for me...

    Usually I don't do console either, but I want to solve this "hard homework" in a thread near this.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    I know i've fixed this problem in the past too......
    just don't remember what I had to do

    I knew if susan begged enough you'd help her with the problem
    shame on you
    Bababooey
    Tatatoothy
    Mamamonkey

  5. #5
    jim mcnamara
    Guest
    Unless I'm missing something - you're trying to use String::getline without a delimiter. I think it won't respond until it sees end of stream. In unix this is ctrl-d. You could make it a carriage return:
    I dunno what the default end of stream is for console mode - ctrl/c
    ? anybody?

    Code:
    String str;
    char delimiter = '\r';  // use carriage return as end of stream
    getline(cin,str, delimiter);

  6. #6

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    no, I'm calling std::getline which differs from std::istream::getline because it can take the string class as argument. It is overloaded to two functions:
    streamtemplate& getline(streamtemplate& strea, stringtemplate& strin, chartype delim);
    where strea is the stream to extract from, strin is the string object to receive the string and delim is the delimiter.
    The second function is an inline that doesn't have a delimiter and just calls the first one with \n as delimiter.

    noble: this forum is my hobby. I'm helping everyone here, even you
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Zaei
    Guest
    I usually implement my own form of getline, using the read() method.

    Z.

  8. #8
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    Im not sure if I understand your problem correctly but I think I might be able to help. I've had problems with getline before too. Try declareing a "dummy" string and using it like this:

    Code:
    string str, dummy;
    cout<<"Enter first string."<<endl;
    getline(cin, str);
    getline(cin, dummy);
    cout<<"Enter second string."<<endl;
    getline(cin, str);
    That fixed my problem with getline before...
    To protect time is to protect everything...

  9. #9

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes that would work but it won't free the user from the need to hit enter more than once.

    Anyway, it's too late now. Thx to all that tried to help.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Did anyone ever find an answer on how to do this?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I just found the solution here: http://support.microsoft.com/default...;en-us;240015& if anyone's interested.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Whoa, deja vu. I thought we'd already determined this was a compiler bug, CB?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by parksie
    Whoa, deja vu. I thought we'd already determined this was a compiler bug, CB?
    You may have. This is a few month old thread that I dug up while seeking out an answer.

    I guess it's just a bug in <string> that required a very easy fix. And it has been fixed in .Net as well.

    I just wish I would have consulted MSDN sooner instead of wasting my time on various forums.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ahhh I see.

    This is one of the problems with C++ at the moment, the quality of Standard Library implementations is still not up to scratch. They're getting pretty close, but nobody's got a fully-working implementation yet. Not MS, not SGI, Sun, GNU, nobody.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Might be a project: the VBF-STL, a complete implementation of the STL, both for windows and POSIX...

    If anyone has got the time...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Not sure. There's already STLport, and the GNU implementation which is currently the most complete.

    It would take years to build up another.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Probably.

    What's STLport?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's another implementation of the STL, for non-compliant compilers and stuff.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    Parksie...........................

    REBOOT!!!!!!!!!!!!!!!

    Bababooey
    Tatatoothy
    Mamamonkey

  20. #20

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    An implementation for compliant compilers would be more interesting - by the time we'd be done even MSVC would hopefully be compliant.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width