Which Statement Will Read an Entire Line of Input Into the Following String Object? String Address;

The C++ getline() is a standard library part that is used to read a string or a line from an input stream. It is a role of the <string> header. The getline() role extracts characters from the input stream and appends it to the string object until the delimiting character is encountered. While doing so the previously stored value in the string object str will be replaced by the input cord if any.
The getline() function tin be represented in ii ways:

Syntax:

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

2. Parameters:

  • is: Information technology is an object of istream class and tells the function nearly the stream from where to read the input from.
  • str: It is a string object, the input is stored in this object afterwards beingness read from the stream.
  • delim: It is the delimitation grapheme which tells the office to cease reading further input after reaching this character.

Example: To demonstrate the utilize of delimiter in the getline() function.

C++

#include  <iostream>

#include  <bits/stdc++.h>

using namespace std;

#ascertain MAX_NAME_LEN sixty  // Maximum len of your proper noun tin can't be more than 60

#define MAX_ADDRESS_LEN 120  // Maximum len of your address can't be more than 120

#define MAX_ABOUT_LEN 250 // Maximum len of your profession tin't exist more than 250

int primary () {

char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];

cout << "Enter your name: " ;

cin.getline (y_name, MAX_NAME_LEN);

cout << "Enter your Urban center: " ;

cin.getline (y_address, MAX_ADDRESS_LEN);

cout << "Enter your profession (press $ to complete): " ;

cin.getline (about_y, MAX_ABOUT_LEN, '$' );

cout << "\nEntered details are:\n" << '\northward' ;

cout << "Proper name: " << y_name << endl;

cout << "Address: " << y_address << endl;

cout << "Profession is: " << about_y << endl;

}

Output:

Output

Note: In the in a higher place example if the #ascertain MAX_NAME_LEN  6, And then in this case if yous cross the divers limit then, in this case, your program will finish execution and go out information technology's applicative for every macro that you have used with getline() function. And you'll get the output as beneath:

C++

#include  <iostream>

#include  <$.25/stdc++.h>

using namespace std;

#define MAX_NAME_LEN 60  // Maximum length of your proper name can't exist more than 60

#define MAX_ADDRESS_LEN 120  // Maximum length of your address tin can't be more than 120

#define MAX_ABOUT_LEN 250 // Maximum length of your profession can't be more than 250

int main () {

char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];

cout << "Enter your name: " ;

cin.getline (y_name, MAX_NAME_LEN);

cout << "Enter your Metropolis: " ;

cin.getline (y_address, MAX_ADDRESS_LEN);

cout << "Enter your profession (printing $ to consummate): " ;

cin.getline (about_y, MAX_ABOUT_LEN, '$' );

cout << "\due north\nEntered details are:\n\due north" ;

cout << "Name: " << y_name << endl;

cout << "Address: " << y_address << endl;

cout << "Profession is: " << about_y << endl;

}

Output:

Output_2nd

 Here, it is understandable that the length of the proper name field was more than than the defined limit that's why the plan stop execution and exit.

one. Syntax:

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

2. The second declaration is almost the same as that of the first one. The only deviation is, the latter have an delimitation character which is by default newline(\north)graphic symbol.
Parameters:

  • is: It is an object of istream class and tells the function nearly the stream from where to read the input from.
  • str: Information technology is a cord object, the input is stored in this object after existence read from the stream.

Beneath program demonstrates the working of the getline() function
Example 1:

CPP

#include <iostream>

#include <string>

using namespace std;

int main()

{

string str;

cout << "Please enter your name: \n" ;

getline(cin, str);

cout << "Hi, " << str

<< " welcome to GfG !\n" ;

return 0;

}

Input:

Harsh Agarwal

Output:

Hello, Harsh Agarwal welcome to GfG!

Example 2: We can use getline() function to dissever a sentence on the basis of a character. Permit'south expect at an example to understand how information technology can be done.

CPP

#include <bits/stdc++.h>

using namespace std;

int chief()

{

cord South, T;

getline(cin, S);

stringstream X(Southward);

while (getline(Ten, T, ' ' )) {

cout << T << endl;

}

return 0;

}

Input:

Hello, Faisal Al Mamun. Welcome to GfG!

Output:

Hello, Faisal Al Mamun. Welcome to GfG!

Circumspection :This function considers a new line or ('\northward') character as the delimitation graphic symbol and new line grapheme is valid input for this office.
Case of how new line can cause problem is given below:
Example:

CPP

#include <iostream>

#include <cord>

using namespace std;

int main()

{

cord name;

int id;

cout << "Please enter your id: \northward" ;

cin >> id;

cout << "Delight enter your proper name: \n" ;

getline(cin, name);

cout << "Your id : " << id << "\northward" ;

cout << "Howdy, " << name

<< " welcome to GfG !\n" ;

getline(cin, proper noun);

cout << "Hullo, " << proper name

<< " welcome to GfG !\northward" ;

return 0;

}

Input:

7 MOHIT KUMAR

Output:

Your id : 7 Hi,  welcome to GfG ! Hello, MOHIT KUMAR welcome to GfG !

Related Articles:

  • How to use getline() in C++ when there are blank lines in input?
  • getline() role and character array

This commodity is contributed past Harsh Agarwal and improved past Faisal Al Mamun. If you like GeeksforGeeks and would like to contribute, you tin too write an commodity using write.geeksforgeeks.org or post your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks chief page and help other Geeks.
Please write comments if y'all find anything incorrect, or yous want to share more than information near the topic discussed in a higher place.

Want to learn from the best curated videos and exercise problems, check out the C++ Foundation Class for Bones to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a linguistic communication to DS Algo and many more,  please refer Complete Interview Preparation Course .


wilsonthor1941.blogspot.com

Source: https://www.geeksforgeeks.org/getline-string-c/

0 Response to "Which Statement Will Read an Entire Line of Input Into the Following String Object? String Address;"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel