Input and Output Operations
Input and Output Operations
Data is transferred to/from output/input device in the form of a sequence of bytes called stream. The stream flowing from an
input device like a keyboard to the main memory, it is called the Input Operation.
On the other hand, streams that flow from the main memory to an output device like a screen is called an Output Operation
Standard output Stream (cout)
C++ standard output stream – cout is an object of the ostream class which
has iostream as its parent. Cout is used in with the operator “<<” and is also
called as an insertion operator to output the information or data to an output
device. The display screen is usually the output device to which the cout object is
connected to.
Depending on the data types used, C++ compiler determines the data displayed
and also determines the type of insertion operator to be used for displaying the
data.
The object Cout and the insertion operator support the built-in data types of C+
+, string and pointer values.
We can also use more than one insertion operator along with cout in a single
statement.
For Example,
cout<<” Hello, World!!”<<” Good morning!!”;
When “endl” is being used at the end of cout, it indicates the next line.
For Example,
cout<<” Hello, World!!”<<” Good morning!!”;
Standard Input Stream (cin)
C++ standard input stream – cin is an object of class istream class which is
also a child of iostream class. The cin object along with “>>”, which is also
known as extraction operator is used to read data from the input device.
An Example of an input device to which cin is connected to is a keyboard.
As per the data type, C++ compiler determines the data to be read and also
determines the type of extraction operator to be used for reading and storing
data. Just like cout, we can use more than one extraction operator in a single
cin statement.
For Example,
Cin>>a; #include <iostream>
using namespace std;
int main( )
{
char str[] = "This is C++ basic Input Output";
int number;
cout<<"Enter the number: "; cin>>number;
cout<<"The number entered is: "<<number<<endl;
cout << "Value of str is: " << str << endl;
}