Header Responsive Ads

Cin Programming (cin C++)

how to get user input in c++


Cin is an input stream object of class iostream. It is defined in the standard library of header file <iostream> which is to be included in the program before the main function to use cin. 

Keyword #include is used to include the header file in the program. Name of the header file in written inside angle brackets (<header file name>).
The below line includes iostream header file in the program.

#include<iostream>

Now in the main function, you can use cin as

std::cin

What is std? It is C++ standard library that contains a collection of functions and classes. To use these functions or classes, the double colon operator (::) is used with the keyword std. As above, cin is followed by std::.

Now the question is that if you want to use cin multiple times in the program then do you need to write std:: every time with cin?

It is simply "NO". 

C++ provides a solution to simplify it by using namespace. Use namespace std before the main function and then write cin without std:: wherever you need.

Awesome!
Yes it is.

Include below line before main(). 

using namespace std;

In C++, each expression is ended with a semicolon (;). If you are not getting it, don't worry. Just leave it. We will be using and explaining it in coming tutorials. 

Now we will write a program to get two numbers from user and display their sum.

Before the main function, we will write the below lines to include iostream header file and standard library in the program.

#include<iostream>
using namespace std;

Now write the main function. What is it?
Main function is the entry point of the program from where the program starts its execution. Just write the below lines.

int main()
{

}

Now we have to write the entire code inside curly brackets { }.
To get two numbers from the user, we will declare two variables of int data type as.

int num1;
int num2;

We can declare multiple variables of the same data type in one line as

int num1, num2;

Now ask the user to enter the first number. How will you show "Enter first number" to the user?

Yes!

To display data on the screen, we will use output stream cout as

cout<<"Enter first number";

Here << is insertion operator. Its direction indicates the flow of data. The text string "Enter first number" will transfer to output stream cout. cout will show it on screen.

The user will see this message from the program and will press any number from the keyboard. 
Which number user has typed? 
As you know the value of any key pressed from the keyboard is transferred to input stream cin. So we will get this value from cin and will assign it to variable num1 as.

cin>>num1;

Here >> extraction operator indicates that value from cin is being assigned to variable num1.

Now to get the second number from the user, we will again write cout to ask use to "Enter second number" as.

cout<<"Enter second number";

The value of the second number will be moved to cin. Now we will assign this value to our second variable num2 as.

cin>>num2;

At this stage, we have taken two numbers from users. The first number is in variable num1 and the second number is in variable num2.

Now just use the mathematical arithmetic operator (+) to add both numbers and display on the screen using cout.

cout<<"Addition of both numbers is ";
cout<<num1+num2;

You can write the above two lines in one line as

cout<<"Addition of both numbers is "<<num1+num2;

Now compile and run the program.
The complete program and its output is attached below:-


cin c++
Output
cin c++ output

Post a Comment

Previous Post Next Post