Read a String From the Console in C

The C++ getline() is a standard library role that is used to read a string 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 graphic symbol is encountered. While doing so the previously stored value in the cord object str will be replaced by the input string if any.
The getline() function can 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: Information technology is a string object, the input is stored in this object later being read from the stream.
  • delim: It is the delimitation graphic symbol which tells the function to end reading further input after reaching this character.

Instance: To demonstrate the utilize of delimiter in the getline() office.

C++

#include  <iostream>

#include  <bits/stdc++.h>

using namespace std;

#ascertain MAX_NAME_LEN 60  // Maximum len of your name can't exist more than 60

#define MAX_ADDRESS_LEN 120  // Maximum len of your accost tin't be more than 120

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

int main () {

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 Metropolis: " ;

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" << '\north' ;

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

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

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

}

Output:

Output

Note: In the above example if the #define MAX_NAME_LEN  6, So in this case if you cantankerous the defined limit then, in this case, your programme volition terminate execution and exit information technology's applicable for every macro that yous have used with getline() function. And you'll get the output as below:

C++

#include  <iostream>

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

using namespace std;

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

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

#define MAX_ABOUT_LEN 250 // Maximum length of your profession tin can't be more than 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 City: " ;

cin.getline (y_address, MAX_ADDRESS_LEN);

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

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

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

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

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

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

}

Output:

Output_2nd

 Hither, it is understandable that the length of the proper noun field was more than the defined limit that'due south why the program stop execution and leave.

1. Syntax:

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

2. The second annunciation is almost the same as that of the get-go ane. The but difference is, the latter have an delimitation character which is by default newline(\due north)character.
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 string object, the input is stored in this object after beingness read from the stream.

Below 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 << "Hello, " << 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 split up a sentence on the basis of a character. Allow's look at an example to empathise how it can be washed.

CPP

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

using namespace std;

int main()

{

string S, T;

getline(cin, S);

stringstream X(South);

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

cout << T << endl;

}

render 0;

}

Input:

Hullo, Faisal Al Mamun. Welcome to GfG!

Output:

Hullo, Faisal Al Mamun. Welcome to GfG!

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

CPP

#include <iostream>

#include <string>

using namespace std;

int main()

{

string proper name;

int id;

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

cin >> id;

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

getline(cin, proper name);

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

cout << "Hello, " << name

<< " welcome to GfG !\n" ;

getline(cin, proper name);

cout << "Hello, " << name

<< " welcome to GfG !\n" ;

return 0;

}

Input:

seven MOHIT KUMAR

Output:

Your id : seven How-do-you-do,  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() function and grapheme array

This article is contributed by Harsh Agarwal and improved by Faisal Al Mamun. If you like GeeksforGeeks and would similar to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-squad@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Delight write comments if you observe anything incorrect, or you want to share more information about the topic discussed above.


bissonnetteliecomand.blogspot.com

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

0 Response to "Read a String From the Console in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel