Thread: cin.getline() issue... HELP?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    19

    cin.getline() issue... HELP?

    I'm having trouble with a section of the code I am using to get a few words from the user, put them into a buffer, then send that buffer to a string. Not even sure it will work, since it will include whitespace in a string, but I'm trying

    The problem is this: the cout<< line hits, tells me to input a couple of words, then immediately, without getting input from me, moves to the next line, then continues on, completely skipping the cin.getline(). Am I doing something wrong?

    Code:
    char tester[26];
        cout<<"enter a couple words: \n";
        cin.getline (tester, 26);
        test = tester;
        cout<<tester;
        cout<<test;
        system("PAUSE");
    the pause is just there because my prompt window closes automatically at program termination. The cout<< statements are just there to validate what is going in and out of those variables for now. Right now, it does NOTHING. no input, no output. Which makes sense... but why does the code just run right over the getline without running it?

    If it helps any, I am using bloodshed dev C++ 4.9.9.2, on windows XP.

    I have ran another program using getline, on this computer, and it works just fine. Tried copying and pasting, too, then changing the variable names... no luck. it skips it. sooooo... help?

    EDIT: For whatever reason, putting the getline code in TWICE makes it work once. They are identical copies of each other. So, I am beginning to think compiler problem, and debating a reinstall.

    Further update... removing it from that section of the code, moving it higher in the program, and re=pasting it fixed it... I'm still looking for WHY though!

    Anyone have any other ideas before I go to that?
    Last edited by kalor_alros; 09-01-2009 at 01:13 AM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    This problem is almost always caused by mixing getline with operator>>. Operator>> will read upto the first invalid character(usually whitespace), and not read invalid character. This leaves any trailing characters in the buffer. Getline will read the buffer upto and including the first newline character. This means if there are already characters in the buffer, it will read them. And do to the nature of the cin, the buffer will always end with a newline. So generally, if you follow getline after operator>>, you will end up reading the newline that the user entered after the previous input.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    that makes sense. I was using this exact syntax:

    Code:
    char tester[26];
    cin.getline(tester,26);
    would it be a fair assumption to make that when a char is declared, the entire buffer is filled with newline? or that the first character is, at least? and, if so, why would it work putting two in a row, with identical syntax (mind you, it only ran once.), and work with the same setup higher in the program flow?

    If that is the case though, would I be able to avoid that issue by putting a random character into the buffer when i declared it? IE, changing it like this:

    Code:
    char tester[26];
    tester = "a";
    cin.getline(tester, 26);
    since the "a" would be overwritten, but it would take the place of the newline character, would that possibly eliminate it?

    Not arguing, I know my tone can sound like that sometimes, just trying to fully comprehend so that I can avoid it later on.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You misunderstand me. The buffer I was refering to is the buffer (tecnically two buffers) inside cin, that holds the characters you type into the terminal, untill you fetch them with either getline or operator>>.

    It is therefor not tester that's the problem, but the code before it. this simple solution is to call cin.ignore(), possibly with arguements, prior to getline.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    I think I did too... that makes more sense to me. I've just ran into cin.ignore(), and it looks like it might be exactly what I need.

    Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline issue
    By Manze in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2009, 04:10 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM