Trending October 2023 # Learn The Examples Of The Getline( ) Function In C++ # Suggested November 2023 # Top 17 Popular | Saigonspaclinic.com

Trending October 2023 # Learn The Examples Of The Getline( ) Function In C++ # Suggested November 2023 # Top 17 Popular

You are reading the article Learn The Examples Of The Getline( ) Function In C++ updated in October 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Learn The Examples Of The Getline( ) Function In C++

Introduction to C++ getline()

Web development, programming languages, Software testing & others

Syntax of C++ getline() function istream& getline( istream& is, string& str, char delim );

The above is a first representation where it accepts three parameters which are is, str, and delim.

Parameters –

is – is parameter is an istream class object which represents from where to read the input string.

str – str parameter represents a string object where input is to be stored after accepting from the input stream.

delim – delim parameter represents delimiting character until where the input string to be accepted.

The return value of the getline( ) function is the object of the input stream class that is ‘is’ itself which is accepted as a parameter to the function.

istream& getline( istream& is, string& str );

The above is a second representation where it accepts two parameters which are is and str. It does not accept delim parameters and the other two parameters are similar to the first representation.

Working and Examples of the getline( ) function in C++

Next, we write the C++ code to understand the getline( ) function working more clearly with the following example where we use getline( ) function to accept the input from the user, as below –

Example #1

Code:

using namespace std; int main() { string message; cout << “Please enter your message : ” ; getline( cin, message ); cout<< “Your message is = “<<message; return 0; }

Output:

As in the above code, the getline() function accepted the full string even as there are spaces present between the characters.

Next, we rewrite the above C++ code to understand how could be the input accepted if we do not use the getline( ) function. So in the next code, we accept the input using getline( ) function, as below –

Example #2

Code:

using namespace std; int main() { string message; cout << “Please enter your message : ” ; cout<< “Your message is = “<<message; return 0; }

Output:

As in the above code, we try to accept a by using cin object instead of using getline() function and we see in the output that the cin object accepted the input until the first space is found. So to accept entire string or multiple lines of string we need to use getline() function of C++.

Next, we write the C++ code to understand the getline( ) function working delimiter character more clearly with the following example where we use getline( ) function to accept the input until delimiter character is found from the user as below –

Example #3

Code:

using namespace std; int main() { string message; cout << “Please enter your message : ” ; getline( cin, message, ‘ ‘ ); cout<< “Your message is = “<<message; return 0; }

Output:

As in the above code, the getline() function is used to accept an input but now the third parameter delimiter character is passed as space(‘ ‘) and hence the getline() function accepts the input stream until space is found the characters present after space is ignored.

Next, we write the C++ code where we delimiter character will be ‘r’, as below –

Example #4

using namespace std; int main() { string message; cout << “Please enter your message : ” ; getline( cin, message, ‘r’ ); cout<< “Your message is = “<<message; return 0; }

Output:

So in the above code, the getline() function accepted an input until the delimiter character ‘r’ is found.

Next, we write the C++ code to use the getline( ) function for character array with different syntax that is istream& getline(char* , int size) where char* is character pointer points to array and size is delimiter which specifies to accept input in an array until that size reached. Note that this syntax accepts until space or specified sized whichever found first.

Example #5

Code:

using namespace std; int main() { char message[5]; cout << “Please enter your message : ” ; cin.getline( message, 5 ); cout<< “Your message is = “<<message; return 0; }

Output:

Conclusion Recommended Articles

This is a guide to C++ getline(). Here we discuss the Working and Examples of the getline( ) function in C++ along with the syntax. You may also have a look at the following articles to learn more –

You're reading Learn The Examples Of The Getline( ) Function In C++

Update the detailed information about Learn The Examples Of The Getline( ) Function In C++ on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!