➢ Input/ Output in C++ :-
C++ comes with libraries which provides us many ways for performing input and output. In C++.
input and output is performed in the form of sequence of bytes or more commonly known as streams.
● Input Stream: If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input.
● Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device(display screen ) then this process is called output.
● Header files available in C++ for Input – Output operation are:
• iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout, cerr etc.
• fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.
wo keywords cout and cin are used very often for taking inputs and printing outputs. These two are the most basic methods of taking input and output in C++. For using cin and cout we must include the header file iostream in our program.
In this article we will mainly discuss about the objects defined in the header file iostream like cin and cout.
1) Standard output stream (cout):-
Usually the standard output device is the display screen. cout is the instance of the ostream class. cout is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator (<<).
● Example :-
#include <iostream>
using namespace std;
int main( )
{
char sample[] = "Hello";
cout << sample << " welcome to cpp";
return 0;
}
● Output :-
Hello welcome to cpp
2) standard input stream (cin):-
Usually the input device is the keyboard. cin is the instance of the class istream and is used to read input from the standard input device which is usually keyboard.
The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keboard.
● Example :-
#include<iostream>
#include<conio>
int main()
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "\nYour age is: "<<age;
return 0;
}
● Output:-
Enter your age:18
Your age is:18
Comments
Post a Comment