Basics of Input & Output

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:


#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;
}

view raw

InputOutput.cpp

hosted with ❤ by GitHub

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

Leave a comment