C++ Read One Line Split by Space

The C++ getline() is a standard library part that is used to read a cord or a line from an input stream. It is a part of the <string> header. The getline() part extracts characters from the input stream and appends information technology 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 string if any.
The getline() office can be represented in two means:

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 after beingness read from the stream.
  • delim: It is the delimitation character which tells the function to stop reading further input later reaching this character.

Example: To demonstrate the use of delimiter in the getline() role.

C++

#include  <iostream>

#include  <bits/stdc++.h>

using namespace std;

#define MAX_NAME_LEN lx  // Maximum len of your proper noun can't be more than than lx

#ascertain MAX_ADDRESS_LEN 120  // Maximum len of your accost can't be more than than 120

#define MAX_ABOUT_LEN 250 // Maximum len of your profession tin't be 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 consummate): " ;

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

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

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

cout << "Accost: " << y_address << endl;

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

}

Output:

Output

Annotation: In the above example if the #define MAX_NAME_LEN  six, So in this case if you cross the defined limit then, in this case, your program volition finish execution and go out it's applicable for every macro that you lot have used with getline() function. And you'll get the output equally below:

C++

#include  <iostream>

#include  <bits/stdc++.h>

using namespace std;

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

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

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

int master () {

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

cout << "Enter your proper 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 << "\due north\nEntered details are:\north\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 noun field was more the defined limit that's why the programme stop execution and get out.

1. Syntax:

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

2. The second declaration is almost the same as that of the kickoff i. The only difference is, the latter have an delimitation character which is by default newline(\n)graphic symbol.
Parameters:

  • is: Information technology is an object of istream class and tells the function virtually the stream from where to read the input from.
  • str: Information technology is a string object, the input is stored in this object afterwards being read from the stream.

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

CPP

#include <iostream>

#include <string>

using namespace std;

int principal()

{

cord str;

cout << "Delight enter your name: \northward" ;

getline(cin, str);

cout << "Hello, " << str

<< " welcome to GfG !\n" ;

return 0;

}

Input:

Harsh Agarwal

Output:

Hello, Harsh Agarwal welcome to GfG!

Example two: We tin employ getline() part to split a sentence on the ground of a character. Let'due south look at an case to understand how information technology tin exist washed.

CPP

#include <bits/stdc++.h>

using namespace std;

int main()

{

cord S, T;

getline(cin, South);

stringstream 10(S);

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

cout << T << endl;

}

return 0;

}

Input:

Hello, Faisal Al Mamun. Welcome to GfG!

Output:

Hello, Faisal Al Mamun. Welcome to GfG!

Caution :This function considers a new line or ('\n') character as the delimitation character and new line character is valid input for this role.
Instance of how new line can cause problem is given below:
Example:

CPP

#include <iostream>

#include <string>

using namespace std;

int main()

{

string name;

int id;

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

cin >> id;

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

getline(cin, proper name);

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

cout << "Hello, " << name

<< " welcome to GfG !\north" ;

getline(cin, name);

cout << "Hello, " << name

<< " welcome to GfG !\north" ;

render 0;

}

Input:

7 MOHIT KUMAR

Output:

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

Related Articles:

  • How to employ getline() in C++ when at that place are blank lines in input?
  • getline() function and character array

This article is contributed past Harsh Agarwal and improved past Faisal Al Mamun. If y'all similar GeeksforGeeks and would similar to contribute, you tin besides write an article using write.geeksforgeeks.org or post your commodity to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and aid other Geeks.
Delight write comments if you notice anything incorrect, or you desire to share more data nearly the topic discussed in a higher place.

Want to acquire from the best curated videos and exercise problems, cheque out the C++ Foundation Grade for Basic to Advanced C++ and C++ STL Form for foundation plus STL.  To complete your training from learning a language to DS Algo and many more than,  delight refer Complete Interview Preparation Course .


nasseralowely44.blogspot.com

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

0 Response to "C++ Read One Line Split by Space"

Отправить комментарий

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel