I hope you are now sure about printing the output using the cout statement. Let us look at the ways to get input.
The cin statement is used to get input from the user. We first need to define a variable in which the given input will be stored. For example:
int a;
cin>>a; //Note the >> sign instead of << which is used for cout .
cout<<a;
//Value of a will be printed.
Multiple inputs can also be stored and printed like:
int a, b ,c;
cin>>a>>b>>c;
cout<<a<<b<<c;
You can also get an input in string or decimal (float) using string and float data types respectively.
For example:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| string name; | |
| int age; | |
| float height; | |
| cout<<"Enter your name:"; | |
| cin>>name; | |
| cout<<"Enter your age:"; | |
| cin>>age; | |
| cout<<"Enter your height:"; | |
| cin>>height; | |
| //let's print them | |
| cout<<"Your name is:"<<name<<endl; | |
| cout<<"Your age is:"<<age<<endl; | |
| cout<<"Your height is:"<<height<<endl; | |
| return 0; | |
| } |
Taking input is so simple. Isn’t it?
Try this question.
Q. Write a C++ program which will input an integer N days and will output total minutes and seconds in those days?
You can check your answer here.

One thought on “Basics of Input & Output”