Home C++ Getline in C++ with examples

Getline in C++ with examples

Before we dive into the tutorial, there is a fundamental question that must be answered. Why take user input? In the real world, many applications give results based on user input. Let’s consider YouTube as an example. A person X visits YouTube and searches for his favorite song. Simultaneously, a person Y visits YouTube and searches for his favorite movie.

The output in both scenarios would be different; thus, user input and handling are crucial parts of any application.

What is cin?

Formally, cin can read the data typed at the keyboard. In reality, most programs are designed to take inputs from the user. This means the program need not modify if the user wants to test his program with different inputs. Literally, cin means console input.

Taking Integer as Input

Dealing with integers is fairly easy. Let’s understand integer inputs through an example.

int x;
cout<<"Enter a value for x";
cin>>x;

The above example declares a variable called x, which is an integer. Then takes console input and saves the value entered by user in the variable x.

Working with Characters and Strings

Before starting with the input taking process of characters and string. Let us first understand the declaration and initialization process for characters and strings.

char letter = 'A';
string name = 'William Shakespeare";

In the first line we have initialized a character type variable called letter and have assigned a value A to it. In the second line we have initialized a string type variable called name and have assigned a name to it.

To output the result of the above lines we write the following code.

cout<<letter<<" "<<name;

The output will be A William Shakespeare.

Taking String as Input

The first question that arises in our mind is, can we use the cin object to take input, or are there specialized functions for strings?

Yes, we can use cin objects to take inputs, but there is a catch. The problem with cin is that when it reads the data, it ignores the leading whitespaces. The example shown below illustrates this problem.

string name;
cin>>name;

If we input William Shakespeare in the console window, the cin object will not read Shakespeare; thus, if we write a cout statement as shown.

cout<<name;

The output will be only William.

Another problem arising if we use the cin object is in the demonstration is shown below.

#include<iostream>
using namespace std;

int main()
{
 string name;
 string city;

 cout << "Enter your name: ";
 cin >> name;
 cout << "Enter the city you live in: ";
 cin >> city;

 cout << "Hello, " << name << endl;
 cout << "You live in " << city << endl;
}
Program Output with Example Input is shown
Enter your name: Mark Twain
Enter the city you live in: Hello, Mark
You live in Twain

We can observe that the user wasn’t given a chance to enter the city name. In the first input, when cin encountered the first space between Mark and Twain, it stopped reading the input and saved the word Mark in the variable name. In the second input statement, the cin object took the leftover characters, i.e., Twain, and stored it in the variable city.

Now, how to avoid this problem?

C++ provides a library function named getline that reads the whole line as input.

Enter: getline()

The getline function reads the whole line, including the leading spaces and the embedded whitespaces. The getline function has the following syntax.

String name;
getline(cin,name);

Here cin is the input stream, and the name is the variable that saves the input. This function solves the above problem, as shown by the following code.

#include<iostream>
using namespace std;

int main()
{
 string name;
 string city;

 cout << "Enter your name: ";
 getline(cin,name);
 cout << "Enter the city you live in: ";
 getline(cin,city);

 cout << "Hello, " << name << endl;
 cout << "You live in " << city << endl;
}

Enter: cin.getline()

Are the functions getline() and cin.getline() different?

Yes, both functions read the whole line of input but are very different and are not interchangeable. cin.getline function keeps reading the input until it has reached a maximum number of characters or until the enter key is pressed. The syntax is shown below:

cin.getline(array,20);

This function takes two arguments, the first one is the array, which stores the string, and the second argument is the size of the array, so the cin will read one character less than the size, keeping in consideration the null terminator.

#include <iostream>
 using namespace std;

 int main()
 {
 const int SIZE = 20;
 char sentence[SIZE];

 cout << "Enter a sentence: ";
 cin.getline(sentence, SIZE);
 cout << "You entered " << sentence << endl;
}

In the above code, the user is asked to input a sentence. This program can read a sentence of up to 20 characters and save it in a variable named sentence, then cout is used to output the result.

The cin.getline function can also be used when reading fstream objects, i.e., filing. If cin.getline function is used, an array of a buffer can be defined with a fixed size, as shown below.

ifstream inFile;
char fileName[51];
cout << "Please enter the name of the file: ";
cin.getline(filename, 51);
inFile.open(fileName);

However, the simple getline function cannot be used here easily as you need a c_str or data member function to extract the C-string objects. The example using the simple getline function is shown below:

ifstream inFile;
string fileName;
cout << "Please enter the name of the file: ";
getline(cin, fileName);
inFile.open(fileName.data());

Here we can see fileName.data has been used to extract C-String object from the string.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More