Thread: getline problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    getline problem

    Hey guys,

    I have some code in a program which does the following:

    Code:
    char a[20];
    cout<<"enter a word"<<endl; 
    cin.getline(a, 20);


    But for some reason the getline code gets ignored. I was just wondering if anyone knows whys its getting ignored?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I was able to solve the problem by adding cin.ignore(); before my cin.getline() i don't understand why this works, does anyone else? Thanks

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are you using MSVC? MSVC 6 has a bug in getline() which causes it to not work.

    If you are using MSVC, either get Dev-C++ or use cin>> or write your own function or something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for your reply, I am using MSVC6 is the cin.ignore the correct way to avoid the problem I have come accross?

    Thanks.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    This is discussed in the FAQ here

    If you look around the forum you'll see that this is an issue that
    comes up a lot in C/C++. I'd say everyone gets thrown by it
    at some point unless its pointed out to them first.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The FAQ doesn't say anything about MSVC's bug.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    what i meant was flushing the input buffer, mixing input functions
    generally leaves junk in the buffer that needs to be flushed.
    I probably should have been more specific, figured that 182 hadn't
    encountered it before.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for the replies, is the cin.ignore() the correct way to solve this problem?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What you are describing looks less like the getline bug and more like a typical problem where the input stream contains a newline character left over from a previous input operation. This situation commonly comes in instance such as the following:
    Code:
    int number;
    char a[20];
    
    cout << "Enter a number: ";
    cin >> number;
    
    cout << "Enter a word: ";
    cin.getline(a,sizeof a);
    In the above code fragment, the cin >> number line will read an integer from the user and leave the trailing newline character in the input buffer. The unread newline will then get eaten by the getline below causing the user to think the program skipped/ignored that line of code. The correct way to deal with this is to call cin.ignore prior to any getline command that comes after a cin >> line.

    That said, you should take a look a this page for a list of fixes that can be applied by making some small changes to your headers to make sure you don't actually come up against the getline bug. As explained by that page, the getline bug actually affects the string version of the getline function and not the version you are using.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM