C++ Getline function

Getline() is used to get the input string from the user in one or more lines until a special character comes (delimiter). It is a predefined function and uses a library in the program, as its definition is present inside the library’s header file.
# <string.h>

This article will explain many basic examples to explain the working of the getline() function.

Prerequisite

We have used the 20.04 version of Ubuntu; you may use the latest one. For the implementation, you need to have a text editor and access the Linux terminal because we will be able to see the output of the source codes on the terminal through the query.

Syntax of the getline () function

The getline() function is explained based on the parameters that are used inside the getline function.

# istream& getline( istream& is, string& str, char delim )

Now, we will explain each parameter of this syntax one by one.

  • Str: This parameter represents the string object where the input value is stored when the value accepts it from the input stream.
  • Delim: This parameter represents the character that shows the delimiter that contains the value up to which the input string is to be displayed.

Example 1

This example uses a simple example of the getline() feature in the program. In simple C++ programs, we need an iostream library in simple C++ programs, but here besides this library, we have used another library that uses getline()  in C++ source code.

# include <string.h>

Coming towards the main program, a string variable is declared here to accept the string message from the user. When we execute the code in the terminal, the user will come to see a message that is displayed in which the system asks the user to enter the string message; this message will be displayed again after some procession. The getline ()  function will accept the sentence entered by the user.

# Getline (cin, message ) ;

The same message will be displayed without any change. We will apply some modifications after the value is entered in future examples.

Save the code in the file with the extension of C++/c.

Execute the command in the Ubuntu terminal. To compile the source code, we need a compiler. For C++ code, we need a G++ compiler to compile the code.

$ g++ -o getline getline.c
$ ./getline

Upon execution, a message is displayed; you need to enter your message. So that the system displays the output.

Now here we are providing the same scenario differently. As we know, just like the getline function, ‘cin’ is also used to get the value from the user. But there are some demerits of using cin in the place of or with the getline function. Here we have used an example, in which we have used cin instead of getline, to see the effect of this change. All other things are the same. For instance, the libraries are the same. The whole program is written inside the main program.

Now save the file and execute it in the Ubuntu terminal. When we compile the code and then execute it, the system will again demand a string message from us. We will write the same string. Using the key ‘ENTER’, you will see that the whole string is not displayed. Only a single portion of the string is displayed. This is because “cin” accepts the value from the whole string until it finds the first space. That’s the reason the output is reduced from the original one. To accept the whole string, use the getline() function. Compile the code and then execute it.

Example 2

In the previous example, as by using “cin”, we have limited the value. But it’s obvious in “cin”; the value is always reduced to the first blank in the given string. But in this example, by using the getline() function, we have limited the value in the input statement by applying a special condition. According to the given condition, we provide an alphabet in the program. On the execution of code, when this alphabet comes in the whole string for the first time, the compiler stops printing the whole string, and a piece of string from the start till that alphabet is displayed on the screen as a resultant value.

# Getline(cin, message, 'I');

In this example, you can see that we have used ‘I’ in the getline() function inside the source code. You can use any other alphabet of your own choice.

Now, after compilation, execute the code on the terminal. From the resultant value, it is clear that we have used ‘I’ in the input string, so the alphabets that come before this alphabet will appear as output.

Example 3

In this example, instead of using the alphabet for displaying criteria, we use the index number here. The compiler will fetch the desired record according to the index number by doing this. The index number that is given in the function will cease the further fetching of the record. For example, we have used the ‘7’ number, so the message will be displayed at position 6 in the index.

In this example, you will also see that there is a slight change in writing the getline() function. In the previous example, we have used ‘cin’ inside the parameters of getline. But this time, we have used a different technique of writing this function.

# Cin.getline( message, 7 );

When we execute the code, we will see that we have entered a name with two words with a space between them. But on applying for the program, we will see that the name is displayed up to the 6th position of the index.

Example 4

This example will display the string so that each word will be displayed on the next line. This example uses a new library instead of others, as the feature of this library already includes the input/ output streaming. And also allows reading and writing in the file. This also involves displaying the values of a string bitwise. Let us know to understand the example. One variable gets the value provided by the user through a getline function. The other variable helps divide the words in the string and move them to the next line.

# Getline (cin, s);

After getting the value, this value then divides the value and places it in another variable, ‘x’. for the display function; we will use a while loop here.

# Stringstream X(s);

According to the loop, the value will be printed until the value is present in the statement. The loop body takes a single word from the complete sentence and cuts it where the space is present or comes first. This condition is given in the loop.

# While (getline(X, T, ' '))

You can see the resultant value by executing the code after compilation.

Conclusion

This article has displayed some elementary examples to explain the getline() function concept. You can use the Linux operating system for elaboration. We have explained several examples to explain the working of this function.  Each example depicts the working of this function differently.



from https://ift.tt/3CSvb6U

Post a Comment

0 Comments