Home »
C++ programming language
Basic Input & Output using cin and cout
cout [console output] <<
cout<< is used to display message/ value of variable to the console output device.
cout is an object of ostream class, operator << is known as insertaion or put to operator. it sends the value to the output device.
Simple form of cout
cout << "Message to display";
cout << variable_name;
Cascading form of cout
cout << "Message to display"/variable_name << "Message to display"/variable_name << ... ;
Example
Consider the example
// Program to illustrate cout
// Author : includehelp.com
// Date : 13-03-2012
#include <iostream>
using namespace std;
int main()
{
char text[]="Hello Guys!";
int val=100;
cout<<"welcome to includehelp.com \n";
// Using cascading form of cout
cout <<"text: "<<text <<", val: "<<val;
return 0;
}
Output
welcome to includehelp.com
text: Hello Guys!, val: 100
- cout is a static object of ostream class.
- while << (insertion operator)/(put to operator) is an overloaded operator of it.
- you can also write cout.operator<<("includehelp.com"); instead of cout<<"includehelp.com";
cin [console input] >>
cin>> is used to take value of variable from input device (keyboard).
cin is an object of istream class, operator >> is known as extraction or get from operator. it extracts (takes) the value from keyboard.
Simple form of cin
cin >> variable_name1;
cin >> variable_name2;
Cascading form of cin
cin >> variable_name1 >> variable_name2 >> ...;
Example
Consider the example
/* Program to illustrate cin
Author : includehelp.com
Date : 13-03-2012
*/
#include <iostream>
using namespace std;
int main()
{
char name[30];
int marks;
cout << "Enter name: ";
cin >> name;
cout << "Enter marks: ";
cin >> marks;
cout << "Hello " << name << " , You got " << marks << " marks in exam.\n";
return 0;
}
Output
Enter name: Mike
Enter marks: 97
Hello Mike , You got 97 marks in exam.
Consider the example - Using cascading form of cin
/* Program to illustrate cin
Author : includehelp.com
Date : 13-03-2012
*/
#include <iostream>
using namespace std;
int main()
{
char name[30];
int marks;
cout << "Enter you name and marks : ";
cin >> name >> marks;
cout << "Hello " << name << " , You got " << marks << " marks in exam.\n";
return 0;
}
Output
Enter you name and marks : Mike 97
Hello Mike , You got 97 marks in exam.
- cin is a static object of istream class.
- while >> (extraction operator)/(get from operator) is an overloaded operator of it.
- you can also write cin.operator>>(variable_name); instead of cin>>variable_name;