help about getline

This is a discussion on help about getline within the C++ Programming forums, part of the General Programming Boards category; After inputing some characters, I need to press enter twice to make the characters printed. why? Code: #include <iostream> #include ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    help about getline

    After inputing some characters, I need to press enter twice to make the characters printed. why?
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string text_word;
        while ( getline(cin, text_word) )
            cout<<text_word<<endl;
    	
        return 0;
    }
    Last edited by Antigloss; 07-16-2005 at 09:38 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    hmm... I dont get that problem. Any example?
    What compiler are you using too?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    I am on WinXP, using msvc++6.
    here is my output
    fds4 456fd 5d4f 6 f46d //my input
    //first enter, prints nothing
    fds4 456fd 5d4f 6 f46d //2nd enter, it prints

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    It is probably related to the getline Template Function Reads Extra Character bug in MSVC++6. Applying the fix given in that page (which is re-expressed in "getline" bug fix for Microsoft Visual C++ 6.0) should solve the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    thanks laserlight. I compiled the code using g++, and it works perfectly~

    it also works after i made the modification to msvc
    Last edited by Antigloss; 07-16-2005 at 09:44 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    While you're at it, mucking around with the system header files, might as well take care of the other "issues" with MSVC.
    "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

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Damn MSVC sure has issues
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

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