Dremendo Tag Line

cin Statement and its Use in C++ Program

Input / Output in C++

In this lesson, we will look at what is cin statement in C++ programming and how it works along with some example.

What is cin Statement

The cin statement is used in the C++ program to accept input from the standard input device (keyboard) and store them in one or more variables.

The cin is similar to cout statement. Instead of printing data on the output device (monitor), it reads data entered from the input device (keyboard).

Let's see some examples for more understanding.

Example 1

C++ program to input a character and store in a character variable.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    char a;
    cout<<"Enter any character ";
    cin>>a;
    cout<<"You have entered "<<a;
    return 0;
}

Output

Enter any character b
You have entered b

Here you can see that in line number 9 we have prompt the user to input any character from the keyboard. On line number 10 we have used cin>> statement to read a character from the keyboard and store it in a character variable a.

Here >> is called the Extraction Operator which is used with cin statement to read input from the keyboard. You can use any variable name you want but remember to follow the variable naming rules that we have discussed earlier. On line number 11 we have printed the value of the character variable a on the screen.

Example 2

C++ program to input and store an integer number, a float number and a double type number in three variables.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int a;
    float b;
    double c;
    cout<<"Enter an integer, a float and a double type numbers\n";
    cin>>a>>b>>c;
    cout<<"You have entered an integer number "<<a<<", a float number "<<b<<" and a double type number "<<b;
    return 0;
}

Output

Enter an integer, a float and a double type numbers
18
54.2365
91.16249
You have entered an integer number 18, a float number 54.24 and a double type number 91.162490
                                

Here you can see that in line number 12 we have used cin>> statement to accept an integer, a float and a double type numbers from the keyboard and store in the variables a,b and c respectively.

video-poster

Test Your Knowledge


You Scored :

Percentage :


Loading