Getline problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpm27
    New Member
    • Jan 2008
    • 23

    #1

    Getline problem

    I am having problems with the getline() function. While reading through tutorials to solve it, I copied an example code and compiled it and it gave me the same error:
    The code is:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string mystr;
      cout << "What's your name? ";
      getline (cin, mystr);
      cout << "Hello " << mystr << ".\n";
      cout << "What is your favorite team? ";
      getline (cin, mystr);
      cout << "I like " << mystr << " too!\n";
      return 0;
    }
    The ouput and my inputs are:
    Code:
    What's your name? JUAN [I]<enter>[/I]
    SOULI [I]<enter>[/I]
    Hello Juan. [I]<enter>[/I]
    What is your favorite team? THE ISOTOPES [I]<enter>[/I]
    I like SOULI too!
    I think it is clear enough to show the problem. Why is the program is behaving like this? Thank you for your time.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    I see no faults in the posted code. Confirmed by compiling and running the program.

    So...the ball's back in your court. What's happening here? The code you posted doesn't exhibit such flawed behavior.

    Comment

    • rpm27
      New Member
      • Jan 2008
      • 23

      #3
      I retried it again and it is giving me the same fault. I know that the code is too simple to have any hidden faults, so could it be from my OS or compiler?
      I am using Windows XP Home Edition SP2, and my compiler is Microsoft Visual C++ 6.0

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        It's your compiler. Well known bug in Visual C++ 6. Referred to as the getline bug. First result on Google: http://www.cs.drexel.edu/~mcs171/Sp0...tructions.html

        Actually, your compiler is old. It's pre 99 revision of C++, so it isn't very good at modern C++. Visual C++ 2008 is available in an express edition. It's free, so get that. Stick with the latest revisions of Visual C++ or gcc, as both are going to be up to date.

        Comment

        • rpm27
          New Member
          • Jan 2008
          • 23

          #5
          Thank you. I'll do that. :-)

          Comment

          Working...