Hey guys, I have been told that the getline function in C++ is a standard library member but is this true as the #include <stdlib> header file does not have to be included in order for getline to work?

Thanks.

saying something is part of the standard c++ library doesn't mean it is declared in <stdlib> -- and there is no such header file. you probably mean <cstdlib>, which was derived form C language.

getline() is a method of std::string class and (I think) iostream.

#include <iostream>
#include <string>

int main()
{
  std::cout << "Enter someting" << std::endl;
  std::string line;
  std::getline(std::cin,line);
}

or

#include <iostream>

int main()
{
  char line[120];
  std::cout << "Enter someting" << std::endl;
  std::cin.getline(line,sizeof(line));
}

yes getline is apart of the standard library.
you still need to let the compiler know what libs you will be linking with.
I thought getline was in string.h.

yes getline is apart of the standard library.

I thought getline was in string.h.

NO! string.h is C, not C++. its declared as I posted above. C uses gets() and fgets(), not getline(). and both of them are declared in stdio.h, not string.h.

NO! string.h is C, not C++. its declared as I posted above. C uses gets() and fgets(), not getline(). and both of them are declared in stdio.h, not string.h.

I stand corrected... brain fart.... howerver, there does happen to be a stdlib.h.

/***
*stdlib.h - declarations/definitions for commonly used library functions
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* This include file contains the function declarations for commonly
* used library functions which either don't fit somewhere else, or,
* cannot be declared in the normal place for other reasons.
* [ANSI]
*
* [Public]
*
****/

I stand corrected... brain fart.... howerver, there does happen to be a stdlib.h.

Yes there is -- but its C, not C++. and just look at the copyright dates that you posted, that file is at 8-21 years old! The most recent c++ standard was in (I think) 1998.

C++ system headers do not use .h file extensions. The c++ equivalent is <cstdlib>. If your c++ compiler does not support <cstdlib> then you need to toss that piece of trash into the bit bucket and get a more modern compiler, such as Dev-C++ or any of a number of others.

Your compiler may well compile stdlib.h with no problems -- but you run the danger that it will not contain up-to-date functions that are in <cstdlib>. I know VC++ 6.0/7.0/8.0 cstdlib just simply includes stdlib.h, but that by no means other compiler follow that.

>getline() is a method of std::string class and (I think) iostream.
It depends on which getline you're talking about, but taken as a whole, your statement is incorrect across the board. You're mixing up the getline member function provided by std::istream (which uses C-style strings) and the getline function provided for use with std::string in <string>. To summarize, using cin as an object of std::istream:

// getline member function
#include <iostream>
char s[SOME_SIZE];
std::cin.getline ( s, sizeof s );

// getline non-member function
#include <string>
std::string s;
getline ( std::cin, s );

>>your statement is incorrect across the board. You're mixing up the getline member function provided by std::istream (which uses C-style strings) and the getline function provided for use with std::string in <string>.

I didn't mix them up, nor was it incorrect -- I just mentioned them both in the same breath. I didn't say they were interchangeable, and I actually posted sample code that shows the difference, just like you did.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.