Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.

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. 

Learn from the Best in the Industry!

Caltech PGP Full Stack DevelopmentExplore Program
Learn from the Best in the Industry!

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.

Get All Your Questions Answered Here!

Caltech PGP Full Stack DevelopmentExplore Program
Get All Your Questions Answered Here!

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.

Get All Your Questions Answered Here!

Caltech PGP Full Stack DevelopmentExplore Program
Get All Your Questions Answered Here!

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

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.

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

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

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