Newb Question gets() vrs cin.geline()

This is a discussion on Newb Question gets() vrs cin.geline() within the C++ Programming forums, part of the General Programming Boards category; I was wondering what the diffrence between the two as far as reading info. cout << "Enter a string: "; ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    6

    Newb Question gets() vrs cin.geline()

    I was wondering what the diffrence between the two as far as reading info.

    cout << "Enter a string: ";
    gets(var);

    or
    cout << "Enter a string: ";
    cin.getsline(var, 40);

    gets seems shorter, but is there a reason to use one over the other?
    Thanks
    Last edited by Tenacious_newb; 04-11-2002 at 10:22 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    I can think of 2 quick reasons

    1. gets does not protect against buffer overflow, getline does

    2. you shouldn't mix and match i/o streams, they may not be synchronised. For instance, gets() followed by getline might read the same information.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    6
    Thanks a lot, good to know, I am working my way through two diffrent books and one of them used gets() exclusivly, and the other doesnt even mention it!
    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Ok. sorry to bother you guys with a newb question.
    By arnis in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2003, 11:23 AM
  4. newb question
    By Jan79 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2003, 09:59 AM
  5. Win app, newb question
    By Blender in forum Windows Programming
    Replies: 9
    Last Post: 02-04-2003, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21