Thread: difference between getline and cin.getline

  1. #1
    bartinla
    Join Date
    Oct 2004
    Posts
    10

    Question difference between getline and cin.getline

    Hey guys,
    Can someone tell me the difference between cin.getline and getline?
    I'm kinda familiar with cin.getline but not with getline....can you also tell me the exact syntax?
    thanks for your help
    bart

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    cin.getline is a member of ostream, and you use it with C-style strings (ie. arrays of char terminated by a null character). getline is an independent function declared in <string> for use with the std::string class. You would use it like this:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string name;
    
      std::cout<<"Enter your name: ";
      if ( std::getline ( std::cin, name ) )
        std::cout<<"Hello, "<< name <<'!'<<std::endl;
    }
    The nice thing about getline is that you don't need to specify a size because std::string grows dynamically.
    My best code is written with the delete key.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    cin.getline is a member of ostream
    Don't you mean istream?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Don't you mean istream?
    Yes, thank you. Can you guess what my most common bug is when working with C++?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline anamoly??
    By RancidWannaRiot in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2005, 10:44 AM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. getline() vs cin.getline()
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2003, 04:20 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM