What's the Diff between getline and cin?

This is a discussion on What's the Diff between getline and cin? within the C++ Programming forums, part of the General Programming Boards category; Like if you getline(name,cin) and cin>>name whats the difference? I heard that getline allows you to input strings with spaces ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    195

    What's the Diff between getline and cin?

    Like if you getline(name,cin) and cin>>name
    whats the difference? I heard that getline allows you
    to input strings with spaces but i tried it with cin
    and it still works.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    cin is a premade object of the istream class. It comes with a number of methods (class functions or function shortcuts--aka operators). Two of the class functions are getline() and >>. >> ignores whitespace, except to use them as terminators for input to the variable. getline() doesn't ignore whitespace, unless you use a whitespace char as the terminating char. There are two forms of getline()--one for straight C++ strings and the other for objects of the STL string class. In order to use getline you need to use the correct version for your type of string.

    null terminated string:
    char first[90];
    cin.getline(first, 89, '~');

    STL string
    string second;
    getline(cin, second, '~');
    Last edited by elad; 05-22-2003 at 07:44 PM.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    what he said in a nutshell...

    you can have spaces with getline, but not with cin.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "if you getline(name,cin) and cin>>name
    whats the difference?"

    First of all, getline(name, cin) will give you an error.

    "I heard that getline allows you
    to input strings with spaces but i tried it with cin
    and it still works."

    Sorry, but you are mistaken. The >> operator ingnores any leading whitespace and stops reading as soon as it encounters whitespace. Therefore, if you are trying to read in something like this:

    See spot run.

    "See" is the only thing that will be read in if you use

    string name;
    cin>>name;

    On the other hand if you use

    getline(cin, name);

    You will read the whole sentence into name.
    Last edited by 7stud; 05-22-2003 at 07:52 PM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Originally posted by Jamsan
    what he said in a nutshell...

    you can have spaces with getline, but not with cin.
    "...not with >>" would be correct. cin is an object.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin and getline() problems....
    By AdampqmabA in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2008, 05:55 PM
  2. vectors and cin, getline?
    By barneygumble742 in forum C++ Programming
    Replies: 9
    Last Post: 07-02-2005, 01:48 AM
  3. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  4. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM
  5. Replies: 5
    Last Post: 03-01-2003, 09:23 PM

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