getline with cin problem -- multiple returns needed

Discussion in 'C++' started by Chris, Oct 19, 2006.

  1. Chris

    Chris Guest

    Running into a problem on Windows. This code

    std::string randomStuff;
    std::getline(std::cin, randomStuff);

    works on unix, but on windows, it requires the user to hit the enter
    key *twice* before it continues past the getline. I tried changing the
    getline to

    std::getline(std::cin, randomStuff, '\n');

    but that didn't help. Any ideas?
    Chris, Oct 19, 2006
    #1
    1. Advertising

  2. Chris

    Marcus Kwok Guest

    Chris <> wrote:
    > Running into a problem on Windows. This code
    >
    > std::string randomStuff;
    > std::getline(std::cin, randomStuff);
    >
    > works on unix, but on windows, it requires the user to hit the enter
    > key *twice* before it continues past the getline. I tried changing the
    > getline to
    >
    > std::getline(std::cin, randomStuff, '\n');
    >
    > but that didn't help. Any ideas?


    Please post a complete and minimal program that demonstrates your
    problem. For what it's worth, this program works fine on my system
    (Windows XP SP2, compiled with VS .NET 2003 SP1):


    #include <iostream>
    #include <string>

    int main()
    {
    std::cout << "Enter some text:\n";

    std::string randomStuff;
    std::getline(std::cin, randomStuff);

    std::cout << "\nYou entered:\n\"" << randomStuff << "\"\n";
    }

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Oct 19, 2006
    #2
    1. Advertising

  3. Chris

    David Harmon Guest

    On 19 Oct 2006 08:09:33 -0700 in comp.lang.c++, "Chris"
    <> wrote,
    >Running into a problem on Windows. This code
    >
    > std::string randomStuff;
    > std::getline(std::cin, randomStuff);
    >
    >works on unix, but on windows, it requires the user to hit the enter
    >key *twice* before it continues past the getline.


    I don't believe you. It is something else you haven't shown us that
    causes your problem.

    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[5.8] How do I post a question about code that doesn't work
    correctly?". It is always good to check the FAQ before posting.
    You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
    David Harmon, Oct 19, 2006
    #3
  4. Chris

    P.J. Plauger Guest

    "David Harmon" <> wrote in message
    news:...

    > On 19 Oct 2006 08:09:33 -0700 in comp.lang.c++, "Chris"
    > <> wrote,
    >>Running into a problem on Windows. This code
    >>
    >> std::string randomStuff;
    >> std::getline(std::cin, randomStuff);
    >>
    >>works on unix, but on windows, it requires the user to hit the enter
    >>key *twice* before it continues past the getline.

    >
    > I don't believe you. It is something else you haven't shown us that
    > causes your problem.


    He might be right, if he's using an unpatched V6. See:

    http://www.dinkumware.com/vc_fixes.html

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
    P.J. Plauger, Oct 19, 2006
    #4
  5. Chris

    Chris Guest

    David Harmon wrote:
    > On 19 Oct 2006 08:09:33 -0700 in comp.lang.c++, "Chris"
    > <> wrote,
    > >Running into a problem on Windows. This code
    > >
    > > std::string randomStuff;
    > > std::getline(std::cin, randomStuff);
    > >
    > >works on unix, but on windows, it requires the user to hit the enter
    > >key *twice* before it continues past the getline.

    >
    > I don't believe you. It is something else you haven't shown us that
    > causes your problem.


    I assure you, my pants are not on fire.
    Chris, Oct 19, 2006
    #5
  6. Chris

    Chris Guest

    P.J. Plauger wrote:
    > "David Harmon" <> wrote in message
    > news:...
    >
    > > On 19 Oct 2006 08:09:33 -0700 in comp.lang.c++, "Chris"
    > > <> wrote,
    > >>Running into a problem on Windows. This code
    > >>
    > >> std::string randomStuff;
    > >> std::getline(std::cin, randomStuff);
    > >>
    > >>works on unix, but on windows, it requires the user to hit the enter
    > >>key *twice* before it continues past the getline.

    > >
    > > I don't believe you. It is something else you haven't shown us that
    > > causes your problem.

    >
    > He might be right, if he's using an unpatched V6. See:
    >
    > http://www.dinkumware.com/vc_fixes.html
    >
    > P.J. Plauger
    > Dinkumware, Ltd.
    > http://www.dinkumware.com


    This sounds very likely. We are using VC6. Will investigate further.
    Chris, Oct 19, 2006
    #6
  7. Chris

    Jim Langston Guest

    "Chris" <> wrote in message
    news:...
    >
    > P.J. Plauger wrote:
    >> "David Harmon" <> wrote in message
    >> news:...
    >>
    >> > On 19 Oct 2006 08:09:33 -0700 in comp.lang.c++, "Chris"
    >> > <> wrote,
    >> >>Running into a problem on Windows. This code
    >> >>
    >> >> std::string randomStuff;
    >> >> std::getline(std::cin, randomStuff);
    >> >>
    >> >>works on unix, but on windows, it requires the user to hit the enter
    >> >>key *twice* before it continues past the getline.
    >> >
    >> > I don't believe you. It is something else you haven't shown us that
    >> > causes your problem.

    >>
    >> He might be right, if he's using an unpatched V6. See:
    >>
    >> http://www.dinkumware.com/vc_fixes.html
    >>
    >> P.J. Plauger
    >> Dinkumware, Ltd.
    >> http://www.dinkumware.com

    >
    > This sounds very likely. We are using VC6. Will investigate further.


    If you are still using VC6 run, don't walk, to the nearest computer store
    and pick up a newer compiler (or download a free version).

    I used to use VC6 and it just has so many problems because I understand it
    preceeded the current standard.
    Jim Langston, Oct 19, 2006
    #7
  8. Chris

    Chris Guest

    Jim Langston wrote:
    > "Chris" <> wrote in message
    > news:...
    > >
    > > P.J. Plauger wrote:
    > >> "David Harmon" <> wrote in message
    > >> news:...
    > >>
    > >> > On 19 Oct 2006 08:09:33 -0700 in comp.lang.c++, "Chris"
    > >> > <> wrote,
    > >> >>Running into a problem on Windows. This code
    > >> >>
    > >> >> std::string randomStuff;
    > >> >> std::getline(std::cin, randomStuff);
    > >> >>
    > >> >>works on unix, but on windows, it requires the user to hit the enter
    > >> >>key *twice* before it continues past the getline.
    > >> >
    > >> > I don't believe you. It is something else you haven't shown us that
    > >> > causes your problem.
    > >>
    > >> He might be right, if he's using an unpatched V6. See:
    > >>
    > >> http://www.dinkumware.com/vc_fixes.html
    > >>
    > >> P.J. Plauger
    > >> Dinkumware, Ltd.
    > >> http://www.dinkumware.com

    > >
    > > This sounds very likely. We are using VC6. Will investigate further.

    >
    > If you are still using VC6 run, don't walk, to the nearest computer store
    > and pick up a newer compiler (or download a free version).
    >
    > I used to use VC6 and it just has so many problems because I understand it
    > preceeded the current standard.


    Unfortunately, this is the "official compiler" for our product at work.
    Companies I work at seem to get frozen on old compilers because they
    are scared of upgrading and having to regression test.
    Chris, Oct 20, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Poof
    Replies:
    1
    Views:
    372
  2. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,661
    Alex Vinokur
    Mar 6, 2005
  3. Markus

    cin.getline() problem

    Markus, Sep 1, 2006, in forum: C++
    Replies:
    11
    Views:
    785
  4. Devendra_Vidhale

    newbie problem with cin and getline

    Devendra_Vidhale, Jul 17, 2007, in forum: C++
    Replies:
    20
    Views:
    1,142
    James Kanze
    Jul 19, 2007
  5. bintom

    Problem with cin.getline()

    bintom, May 29, 2010, in forum: C++
    Replies:
    4
    Views:
    1,313
Loading...

Share This Page