I am starting learning C++ a little while ago. Came to know about header files and preprocessor statements.
I know that std::cin and std::cout are the objects/Function is declared in standard library IOSTREAM. But when taking input as a string and to read the whole line. We have to getline() function or at least the instructor is using it in the video.
Now I have checked on the internet and most of the sites are showing that getline() is defined under STRING file/Library. But the thing is my program is working perfectly fine even without including the string file. So what I am missing.? or doing something wrong. and if you can please also explain how getline function is working. and also please what's the actual difference between using namespace std, and using std:: Thank you
#include <iostream>
int main()
{
std::string str;
std::cout << "Please enter your name: \n";
getline(std::cin, str);
std::cout << "Hello, " << str
<< " welcome to GfG !\n";
return 0;
}
You have to include the header <string>
to use the function std::getline
. It is implementation defined whether the header <iostream>
includes the header <string>
.
In this call using unqualified name getline
getline(std::cin, str);
the compiler applies the argument dependent lookup ADL (the first argument std::cin
is defined in the namespace std::
) and finds the corresponding name std::getline
in the namespace std::
.