Thread: cin.getline

  1. #1
    Unregistered
    Guest

    Angry cin.getline

    Okay this has me scratching my head :

    I can use cin.getline in main fine by e.g.

    char test[10];

    cout << "input : " ;
    cin.getline(test, 10);
    cout << "test is " << test;



    BUT when in a function it skips the input part and moves onto the next bit off code !!!

    Can someone please help me out.

    Thanks

    Blim

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    This is 99.99% probable that you have some chars left over in the streambuf before calling getline. getline takes this as its input. If you use cin>>something then the \n will be left in the stream. try adding cin.ignore(80,'\n') before the cin.getline()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    Thanks stoned_coder, keep puffing that erb for me !

  4. #4
    Unregistered
    Guest
    Don't use cin.
    Use only cin.getline, also for input of numbers.
    Convert the string to int, double ... with atoi or atof.
    Why?
    Let's say you've defined int a;
    with cin >> a; you hope to get an integer.
    But if the user inputs anything else as an integer
    you're in trouble, even cin.ignore() won't be able
    to clear the buffer.

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    But if the user inputs anything else as an integer
    you're in trouble, even cin.ignore() won't be able
    to clear the buffer.
    A combination of cin.clear() and cin.ignore() will clear the buffer and reset the stream.
    zen

  6. #6
    Unregistered
    Guest

    cin.get and cin.getline

    Hey im kind of a newbie at c++ programming and i was wondering...

    What's the difference between cin.get and cin.getline?

    Thanks in advance for the help

  7. #7
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    cin.get(character) gets a character

    cin.getline(*char[], count, termchar) gets a line of characters up to a term character.

    getline(cin,string,termchar) is a different function found in cstring and it gets characters and turns them into an ansi null terminated string. (which really isn't different than a null terminated array of characters... for most aspects)

    All this is to the best of my recollection.... which is a bit fuzzy at the moment.
    Blue

  8. #8
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Actually, if you are using Visual C++, cin.get can function like cin.getline. The only difference is that cin.getline will "eat" the delimeter and cin.get will leave it in the buffer.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  9. #9
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>cin.get can function like cin.getline

    syntax?
    Blue

  10. #10
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    cin.get(buff,10,'\n');

    and it's not just Visual C++, but part of the standard.
    zen

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    Hey I can solve the whole thing with only 1 function.

    char *fgets(char *buf, size_t len, FILE *stream);

    e.g.

    fgets(myarray, sizeof myarray, stdin);

    doesn't leave the \n character in the buffer and doesn't allow buffer overflows.
    It is still C++, thought it is also in the C library.

    one fish two fish
    red fish blue fish

  12. #12
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    doesn't leave the \n character in the buffer and doesn't allow buffer overflows.
    That's what istream::getline() does. It doesn't flush the input buffer after it's done.
    Last edited by zen; 12-21-2001 at 08:29 PM.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM