C++ program to read string using cin.getline()
C++ program to read string using cin.getline()
C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from the input stream and adds them to the given string object until it determines that the character is delimiting. If any, the value stored in string object str shall be replaced with the current string when doing so. The function getline() can be reproduced in two ways:
- Syntax:
istream& getline (istream& is, string& str, char delim);
Parameters:
Delim: This is the delimitation character that tells the function to stop reading further input after the character is reached.
Str: It is a string object, and the input is stored within this object after reading from the stream.
Is: It is an istream class object, and tells the stream function from where to read the input.
Return Value: This function returns the same input stream as the parameter which is accepted.
- Syntax:
istream& getline (istream& is, string& str);
The second statement is closely parallel to that of the first. The only difference is that this latter doesn't recognize any delimitation character.
Parameters:
Is: is an istream type object and informs the stream function where the data should be read from.
Str: It is a string entity, and after reading from the source, the input is stored within that object.
Return Value: This function will return a certain input stream as the parameter which is accepted.
Name, address, about a person and print in different lines will be read in this program, the name will contain spaces and dots, the address will contain space, commas, and other special characters, as well as mixed characters, will be included in this program. Using the cin.getline() function, we can read all data, and print using cout.
cin.getline() example in C++
/*C++ program to read string using cin.getline().*/ #include <iostream> using namespace std; //macro definitions for maximum length of variables #define MAX_NAME_LENGTH 100 #define MAX_ADDRESS_LENGTH 200 #define MAX_ABOUT_LENGTH 400 using namespace std; int main() { char name[MAX_NAME_LENGTH],address[MAX_ADDRESS_LENGTH],about[MAX_ABOUT_LENGTH]; cout << "Enter Your name: "; cin.getline(name,MAX_NAME_LENGTH); cout << "Enter Your address: "; cin.getline(address,MAX_ADDRESS_LENGTH); cout << "Enter about yourself (press # to complete): "; cin.getline(about,MAX_ABOUT_LENGTH,'#'); //# is a delimiter cout << "\nEntered details are:"; cout << "Name: " << name << endl; cout << "Address: " << address << endl; cout << "About: " << about << endl; return 0; }
Output:

Example 2: We can use the function getline() to split a sentence according to a character. Let's look at an example to see how that can be done.
// C++ program to understand the use of getline() function #include <bits/stdc++.h> using namespace std; int main() { string P, K; getline(cin, P); stringstream N(P); while (getline(N, K, ' ')) { cout << K << endl; } return 0; }
Output:

Caution: This function considers a new line or ('\n') character to be valid input for this function as the delimitation character and new line character.
Example2:
// C++ program to demonstrate // anomaly of delimitation of // getline() function #include <iostream> #include <string> using namespace std; int main() { string name; int id; // Taking id as input cout << "Enter your id: \n"; cin >> id; // Takes the empty character as input cout << "Enter your name: \n"; getline(cin, name); // Prints id cout << "Enter Your id : " << id << "\n"; // Prints nothing in name field // as "\n" is considered a valid string cout << "Hi, " << name << " welcome to Tutorialandexample !\n"; // Again Taking string as input getline(cin, name); cout << "Hi " << name << " welcome to Tutorialandexample !\n"; return 0; }
Output:
