What is the Difference Between getline and cin

The main difference between getline and cin is that getline is a standard library function in the string header file while cin is an instance of istream class.

C++ is a high level, general purpose programming language developed by Bjarne Stroustrup at Bell Labs. It contains advanced features than C language. Therefore, C++ is a superset of C. C++ provides a standard library, which consists of various header files. Each header file has functions. Programmers can use these functions in their programs. getline is a function in the string header file while cin is an object defined in the istream class.

Key Areas Covered

1. What is getline
    – Definition, Functionality
2. What is cin
    – Definition, Functionality
3. Relationship Between getline and cin
   – Outline the Association
4. Difference Between getline and cin
   – Comparison of Key Differences

Key Terms

C++, cin, getline

Difference Between getline and cin - Comparison Summary

What is getline

getline() is a standard library function in the string header file. It helps to read a string or a line from the input stream. Thus, this function obtains characters from the input stream and appends it to a string object until receiving a newline character. For example, refer the below program.

Difference Between getline and cin

Figure 1: C++ program with getline

In the above program, name is a string. The cout statement asks the user to enter his name. The getline function has two parameters: cin and name. Moreover, cin is an object of istream class. It describes the source of reading the input. The name is the string object. The input is stored in this object after being read from the stream.

What is cin

cin is an instance of istream class. The object is related to the standard input device (keyboard). It works with the stream extraction operator which is >>. Refer the below program.

Main Difference - getline vs cin

Figure 2: C++ program with cin

In the above program, name is a char type variable. The cout indicates the user to enter the name. When the user inputs the name and press enter key, it is stored into the variable name. Finally, the cout displays the name on the console.

Furthermore, the programmer can use stream extractor operator (>>) to obtain more user inputs as follows.

cin >> name >> marks;

It is similar to the following.

cin >> name;

cin>>marks;

Relationship Between getline and cin

  • getline function accepts cin as a parameter.

Difference Between getline and cin

Definition

getline() is a standard library function in C++ and is used to read a string or a line from the input stream while cin is an object in C++ of the class istream that accepts input from the standard input device.

Basis

The main difference between getline and cin is that getline is a function while cin is an object.

Parameters

Moreover, getline accepts parameters, but there are no parameters in cin. Thus, this is another difference between getline and cin.

Conclusion

Both getline and cin help to obtain user inputs. The main difference between getline and cin is that getline is a standard library function in the string header file while cin is an instance of istream class. In breif, getline is a function while cin is an object.  Usually, the common practice is to use cin instead of getline.

References:

1.“Getline (String) in C .” GeeksforGeeks, 30 May 2018, Available here.
2.“Std::Getline (String).” Cplusplus.com, Available here.
3.“C Cin.” Python Strings (With Examples), Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply