The "c" in C++ cin refers to "character" and "in" means "input". Thus, cin means "character input". The C++ cin object belongs to the istream class. It accepts input from a standard input device, such as a keyboard, and is linked to stdin, the regular C input source. 

For reading inputs, the extraction operator(>>) is combined with the object cin. The data is extracted from the object cin, which is entered using the keyboard by the extraction operator.

  • normal input-output stream (iostream): iostream stands for standard input-output stream. This file includes definitions for objects, such as cin, cout, cerr, and others.
  • input output manipulators (iomanip): iomanip stands for input output manipulators. Stream manipulation is accomplished using the methods declared in these files. Setw, setprecision, and other terms are specified in this file.
  • fstream: The main purpose of this header file is to define the file stream. This header file is used to manage data that is read from a file as input or written to a file as output.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

In C++ cin, the keywords cout and cin are often used to print outputs and take inputs, respectively. These are the simplest methods for receiving data and printing output.

Example

// C++ program to demonstrate the

// cin object

#include <iostream>

using namespace std;

// Driver Code

int main()

{

string s;

// Take input using cin

cin >> s;

// Print output

cout << s;

return 0;

}

Output

Cin_Object_in_Cpp_1.

The C++ cin is an istream class predefined object. It is linked to a standard input device, i.e., a keyboard. To read input from a console, the cin is used in combination with the stream extraction operator (>>). 

Let's look at a basic standard input stream (cin) example:

Example

#include <iostream>  

using namespace std;  

int main( ) {  

  int age;  

   cout << "Enter your height: ";  

   cin >> height;  

   cout << "Your age is: " << height << endl;  

}  

Output

Cin_Object_in_Cpp_2.

Syntax of cin

The keyboard is usually used as a computer's input device. The c++ cin statement is an instance of the class iostream and is used to read data from a standard input device, which is usually a keyboard.

For reading inputs, the extraction operator(>>) is combined with the object cin. The data (entered using the keyboard) is extracted from the object cin by the extraction operator.

Example

#include <iostream>

using namespace std;

int main()

{

int age;

cout << "Enter your age: ";

cin >> age;

cout << "\nYour age is: " << age;

return 0;

}

Output

Cin_Object_in_Cpp_3

The C++ cin function simply displays basic values on the computer, but the standard library's input/output features offer several other ways to communicate with the user. 

In this segment, we will briefly overview a few of the most useful features.

To perform input and output operations in sequential media, such as the computer, keyboard, or file, c++ cin uses a convenient abstraction called streams. A stream is a container where a program can insert or remove characters. Knowing about the stream's related media or internal requirements is not necessary to perform these operations.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Cin With Extraction Operator

To accept multiple inputs, the extraction operator may be used multiple times. 

Example 

#include <iostream>

using namespace std;

int main()

{

            int x, y, z;           

            /* For single input */

            cout << "Enter a number: ";

            cin >> x;           

            /* For multiple inputs*/

            cout << "Enter 2 numbers: ";

            cin >> y >> z;           

            cout << "Sum = " << (x+y+z);

            return 0;

}

Output

Cin_Object_in_Cpp_4

Cin With Member Functions

Other member functions, such as getline() and read(), can also be used with the cin item.

  • cin.get(char &ch): Reads a character from the input and stores it in ch.
  • cin.getline(char *buffer, int length): Reads a stream of characters into the string buffer, stopping when it reaches length-1 characters, an end-of-line character ('n'), or the file's end.
  • cin.read(char *buffer, int n): Reads n bytes from the stream into the buffer (or until the end of the file).

cin.ignore, and cin.eof

Example

#include <iostream>

using namespace std;

int main()

{

char name[20], address[50];

cout << "Name: ";

cin.getline(name, 20);

cout << "Address: ";

cin.getline(address, 50);

cout << endl << "You entered " << endl;

cout << "Name = " << name << endl;

cout << "Address = " << address << endl;

return 0;

}

Output

Cin_Object_in_Cpp_5

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

How to Read Input From User Using C++ Cin

Only cout and c++ cin (the standard output and input streams) will be discussed in depth here.

cerr and clog are both output streams, so they essentially work the same way as cout, with the exception that they identify streams for specific purposes: error messages and logging; and, in many cases, in most environment setups, they actually do the same thing: they print on screen.

  • Read Integer From User

#include <iostream>

using namespace std;

int main() {

   int x;

   cout << "Enter a number : ";

   cin >> x;

   cout << "You entered a number : " << x << endl;

}

Output

Cin_Object_in_Cpp_6

  • Read String From User

#include <iostream>

using namespace std;

int main() {

   string name;

   cout << "Enter a string : ";

   cin >> name;

   cout << "You entered a string : " << name << endl;

}

Output

Cin_Object_in_Cpp_7

  • Read Double From User

#include <iostream>

using namespace std;

int main() {

   double d;

   cout << "Enter a double value : ";

   cin >> d;

   cout << "You entered : " << d << endl;

}

Output

Cin_Object_in_Cpp_8.

  • Read Boolean From User

#include <iostream>

using namespace std;

int main() {

   int x;

   cout << "Enter a number : ";

   cin >> x;

   cout << "You entered a number : " << x << endl;

Output

Cin_Object_in_Cpp_9.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion 

The first thing that one learns about when starting with the c++ programming language is cin. In this article, we discussed how to use the cin object in C++ with the help of examples. 

Simplilearn offers several professional courses in C++ and other programming languages that can help you understand concepts better. Explore the Full Stack Web Development Certification Course by Simplilearn to learn modern coding techniques with bootcamp-level intensity and accelerate your career as a software developer. 

Simplilearn also offers SkillUp courses in several domains, from data science and business analytics to software development; AI and machine learning are some of the best available picks currently. You get complete access to the course material, and the program provides 24X7 expert support for all your doubts. Enroll today to enhance your skills and advance your career!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.