Introduction :
We can read a string line or a user input line in many ways in C++. In this tutorial, we will show you how to read a line using getline() function. getline() has different variants. We will show you these variants with an example.
In C++, you will face two different getline functions, one is a global function and the other one is defined in istream. Don’t get confused with them. They are both different and you can use any one of them to read a user input string.
In this blog post, we will show you both of these getline() functions.
std::getline (string) :
This is the global getline function. It is used to get the line from a stream to a string. It has two different variants :
istream& getline (istream& is, string& str, char delim) :
-
is is the stream from where the function will read the contents.
-
str is the string where it will store the final string.
-
delim is the delimiter or a character that will tell the function to stop reading once it gets this delimiter.
istream& getline (istream& is, string& str) :
This is similar to the above one. The only difference is that it doesn’t have the third parameter delim. It will read the characters from the input stream is and update the final string str. It will only stop if a new line character or ‘\n’ is reached.
Let me show you an example :
Example std::getline(string) :
#include <iostream>
using namespace std;
//1
int main()
{
//2
string str;
cout << "Enter a string : ";
//3
getline(cin, str);
cout << str << endl;
return 0;
}
Sample Output :
Enter a string : Hello World
Hello World
Explanation :
The commented numbers in the above program denote the step numbers below :
-
The main() method is the starting point of a program. It will start the execution with this method.
-
Create one string variable str to read the user input string. Ask the user to enter the string using cout.
-
Get the user input string using the getline method. Here, you can see that we are passing cin as the first argument and str as the second argument.
Finally, print the str string to the user.
std::istream::getline :
The name of this method is the same as the previous one. The only difference is that it is defined in the input stream. We can call it on an input stream object. Following are the two variants of this method :
istream& getline (char* s, streamsize n ) :
Here, s is a pointer to an array of characters and the extracted characters are stored as C string. n is the count of characters we want to read.
It will read the characters from the stream and write them to s.
istream& getline (char* s, streamsize n, char delim ) :
Similar to the above one, it has one extra parameter delim. delim is the stop delimiter. For the first one, this value is a newline character or the process will stop only if a newline or ‘\n’ is found.
Example :
#include <iostream>
using namespace std;
//1
int main()
{
//2
char str[256];
cout << "Enter a string : ";
//3
cin.getline(str,256);
cout << str << endl;
return 0;
}
You can see here that the output is the same as the previous one.
Conclusion :
We have learned that we can read the user input strings in two different ways in C++. The first one uses C++ string and the second one uses classic C strings or pointer to a character array. Most of the time, we use the first one as string is used mainly in C++, not character array. Try to run the examples shown above and drop one comment below if you have any questions.