Getline

This is a discussion on Getline within the C++ Programming forums, part of the General Programming Boards category; Hello, I have been looking at a tutorial on another website, and this bit of code is always used: Code: ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    Getline

    Hello, I have been looking at a tutorial on another website, and this bit of code is always used:
    Code:
    getline (cin, //some variable)
    Sorry if thhis isnt very clear, but I don understand why there is a
    Code:
    cin
    inside the function not outside it like this:
    Code:
    cin.getline();
    What are the differnces between these two things?

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,062
    getline has two versions: the member function of cin has a signature like this

    Code:
    // istream& getline (char* s, streamsize n );
    // istream& getline (char* s, streamsize n, char delim );
    You use this one with fixed character buffers like this

    Code:
    char buf[1000];
    cin.getline(buf, 1000);
    I think the version which operates on std::string came later in the C++ standard, so it's declared differently (as a non-member function)

    Code:
    // istream& getline (istream& is, string& str, char delim);
    // istream& getline (istream& is, string& str);
    
    // ...
    
    string s;
    getline(cin, s);
    I would prefer the second version, unless you specifically want to limit lines to a certain length

    istream::getline - C++ Reference
    getline (string) - C++ Reference

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Thank you for the reply. I now understand the second version, but now im confused with the first one... Is that one usually used with C-style strings?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,062
    Most of the time char* indicates a c-style string, but to be sure you should check the reference. Look at two links above.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2011, 06:32 AM
  2. help with getline.
    By Nicheter in forum C Programming
    Replies: 2
    Last Post: 02-11-2008, 02:35 PM
  3. C++ cin.getline help
    By zerlok in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2008, 01:01 AM
  4. cin.getline(something,3);
    By junkeat90 in forum C++ Programming
    Replies: 1
    Last Post: 01-14-2008, 06:57 PM
  5. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 08:47 AM

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