C++ getline()The cin is an object which is used to take input from the user but does not allow to take the input in multiple lines. To accept the multiple lines, we use the getline() function. It is a pre-defined function defined in a <string.h> header file used to accept a line or a string from the input stream until the delimiting character is encountered. Syntax of getline() function:There are two ways of representing a function:
The above syntax contains three parameters, i.e., is, str, and delim. Where, is: It is an object of the istream class that defines from where to read the input stream. str: It is a string object in which string is stored. delim: It is the delimiting character.Return value This function returns the input stream object, which is passed as a parameter to the function.
The above syntax contains two parameters, i.e., is and str. This syntax is almost similar to the above syntax; the only difference is that it does not have any delimiting character. Where, is: It is an object of the istream class that defines from where to read the input stream. str: It is a string object in which string is stored. Return value This function also returns the input stream, which is passed as a parameter to the function. Let's understand through an example. First, we will look at an example where we take the user input without using getline() function. In the above code, we take the user input by using the statement cin>>name, i.e., we have not used the getline() function. Output Enter your name : John Miller Hello John In the above output, we gave the name 'John Miller' as user input, but only 'John' was displayed. Therefore, we conclude that cin does not consider the character when the space character is encountered. Let's resolve the above problem by using getline() function. In the above code, we have used the getline() function to accept the character even when the space character is encountered. Output Enter your name : John Miller Hello John Miller In the above output, we can observe that both the words, i.e., John and Miller, are displayed, which means that the getline() function considers the character after the space character also. When we do not want to read the character after space then we use the following code: In the above code, we take the user input by using getline() function, but this time we also add the delimiting character('') in a third parameter. Here, delimiting character is a space character, means the character that appears after space will not be considered. Output Enter your profile : Software Developer Profile is: Software Getline Character ArrayWe can also define the getline() function for character array, but its syntax is different from the previous one. Syntax In the above syntax, there are two parameters; one is char*, and the other is size. Where, char*: It is a character pointer that points to the array. Size: It acts as a delimiter that defines the size of the array means input cannot cross this size. Let's understand through an example. Output Enter your favorite fruit: Watermelon Your favorite fruit is: Watermelon Next TopicC++ vs C# |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India