Hwo to Read Multiple Lines From Cin
The C++ getline() is a standard library function that is used to read a cord or a line from an input stream. It is a part of the <string> header. The getline() office 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 cord object str will exist replaced by the input string if whatever.
The getline() role tin be represented in two ways:
Syntax:
istream& getline(istream& is, string& str, char delim);
2. Parameters:
- is: It is an object of istream class and tells the function about the stream from where to read the input from.
- str: It is a cord object, the input is stored in this object later on existence read from the stream.
- delim: Information technology is the delimitation character which tells the function to stop reading further input afterwards reaching this character.
Example: To demonstrate the use of delimiter in the getline() function.
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#ascertain MAX_NAME_LEN 60 // Maximum len of your name can't be more than than 60
#ascertain MAX_ADDRESS_LEN 120 // Maximum len of your address tin can't be more 120
#define MAX_ABOUT_LEN 250 // Maximum len of your profession tin't exist more than 250
int master () {
char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];
cout << "Enter your proper noun: " ;
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" << '\n' ;
cout << "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 half-dozen, So in this instance if yous cross the defined limit then, in this case, your program will stop execution and go out it's applicable for every macro that you lot have used with getline() role. And you'll get the output as beneath:
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define MAX_NAME_LEN sixty // Maximum length of your name tin can't be more than than threescore
#define MAX_ADDRESS_LEN 120 // Maximum length of your address can't be more 120
#ascertain MAX_ABOUT_LEN 250 // Maximum length of your profession tin can't be 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 City: " ;
cin.getline (y_address, MAX_ADDRESS_LEN);
cout << "Enter your profession (press $ to complete): " ;
cin.getline (about_y, MAX_ABOUT_LEN, '$' );
cout << "\n\nEntered details are:\northward\n" ;
cout << "Proper noun: " << y_name << endl;
cout << "Accost: " << y_address << endl;
cout << "Profession is: " << about_y << endl;
}
Output:
Output_2nd
Here, it is understandable that the length of the name field was more than the divers limit that's why the program cease execution and exit.
one. Syntax:
istream& getline (istream& is, string& str);
2. The 2d declaration is well-nigh the aforementioned as that of the first one. The only deviation is, the latter have an delimitation character which is by default newline(\n)grapheme.
Parameters:
- is: It is an object of istream grade and tells the part about the stream from where to read the input from.
- str: It is a cord object, the input is stored in this object after being read from the stream.
Below program demonstrates the working of the getline() role
Example 1:
CPP
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Please enter your proper noun: \n" ;
getline(cin, str);
cout << "How-do-you-do, " << str
<< " welcome to GfG !\n" ;
return 0;
}
Input:
Harsh Agarwal
Output:
Howdy, Harsh Agarwal welcome to GfG!
Example two: We can use getline() function to separate a judgement on the basis of a character. Allow'southward wait at an case to understand how information technology tin be washed.
CPP
#include <bits/stdc++.h>
using namespace std;
int chief()
{
string S, T;
getline(cin, S);
stringstream X(S);
while (getline(10, T, ' ' )) {
cout << T << endl;
}
return 0;
}
Input:
Hello, Faisal Al Mamun. Welcome to GfG!
Output:
Howdy, Faisal Al Mamun. Welcome to GfG!
Circumspection :This function considers a new line or ('\n') character every bit the delimitation character and new line character is valid input for this office.
Case of how new line tin crusade trouble is given below:
Example:
CPP
#include <iostream>
#include <string>
using namespace std;
int primary()
{
string name;
int id;
cout << "Please enter your id: \northward" ;
cin >> id;
cout << "Please enter your name: \northward" ;
getline(cin, proper name);
cout << "Your id : " << id << "\n" ;
cout << "Hi, " << name
<< " welcome to GfG !\north" ;
getline(cin, name);
cout << "Hi, " << name
<< " welcome to GfG !\n" ;
return 0;
}
Input:
7 MOHIT KUMAR
Output:
Your id : seven Hello, welcome to GfG ! Hello, MOHIT KUMAR welcome to GfG !
Related Articles:
- How to use getline() in C++ when there are bare lines in input?
- getline() function and grapheme array
This article is contributed by Harsh Agarwal and improved by Faisal Al Mamun. If yous similar GeeksforGeeks and would like to contribute, you can as well write an article using write.geeksforgeeks.org or mail your commodity to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks master page and assistance other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
clancynohnerhed93.blogspot.com
Source: https://www.geeksforgeeks.org/getline-string-c/
0 Response to "Hwo to Read Multiple Lines From Cin"
Post a Comment