If you like my posts please leave a comment.

Monday, July 23, 2012

Input / Output (I/O) IN C++


Streams:

The C++ I/O subsystem is designed to work with a wide variety of devices that includes keyboard, monitor, disks, tape drives etc. It provides a uniform interface that is quite independent of the actual device being used. Thus, the programmer can write a C++ program that can receive input from and send output to any device with out concerning about the characteristics of devices being used. This stream (interface / a sequence of bytes) is defined by the class <iostream>; cin and cout are the two objects of this class. cin is used to receive the input from the keyboard (standard input device) and  cout is used to send the output to the monitor / console (standard output device). These two functions input and output uses overloaded operators ‘<<’ and ‘>>’. ‘>>’ is used with cin. The overloaded operator ‘>>’ normally skips whitespace characters, such as blanks, tabs and new lines in the input stream. ‘<<’ is used with cout.


Fig. Data streams
Now let’s see the hierarchy of various stream classes used in C++ I/O subsystem.
  • ios class (Input/Output Stream) – It is a general input/output class; contains all basic features that are used by all other derived input/output classes. ios class declares constants and functions that are nrequired for hanfling formatted input and output operations.
  • istream (Input Stream) class – It is inherited from class ios. It declares input function such as get(), getline() and read().
  • ostream (Output Stream) class – It is also inherited from class ios. It declares output functions such as put() and write().
  • iostream (Input/Output Stream) – It is the Input/Output stream. It is inherited from istream and ostream and thus containing all the input/output operations. Two objects of this class cin and cout uses the overloaded operators ‘>>’ and ‘<<’.
  • Streambuf (Stream buffer) – It provides interface to physical devices through buffers; it acts as a base for filebuf class used in files.

Input and Output Operators:

The input and output operators in C++ are “>>” and “<<” respectively. they are supported by the cin and cout identifiers . As it is said earlier that, these are the predefined objects that represents standard input and output streams. cin reads data from keyboard and cout gives the output to screen (console) .
Examples:
(See the previous examples)

Cascading of I/O Operators:

For multiple input or output operations for two or more variables, then instead of writing separate statements w can cascade operator << or >> as shown below:

Multiple inputs for two or more variables
cin>>variable1>>variable2>>….. ;

Multiple output from two or more variables
cout<<variable1<<variable2<<…. ;

FNote: variable can be of different data types, but the input data must match the data type of the variable and must be separated by a white space character.
//Program demonstrating cascading of I/O operators
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
float num2;
char ch;
cout<<"Enter for int num,float num2 and char ch: ";
cin>>num>>num2>>ch;
cout<<"num = "<<num<<" num2 = "<<num2<<" ch = "<<ch;
getch();
}
Output:
FNote: All the values are entered separated by a space and the last value is followed by pressing ‘Enter’ key

Manipulators:

These are the operators are to be used with insertion operator << to modify the way data is displayed. The most commonly used stream manipulators are: using endl and setw

endl Manipulator:

The endl is used to output a new line and to flush the output buffer.
For example:
cout<<”My Name is Biswajit Dey”<<endl;
cout<<”Teacher of sub: Computer Sc.”;

Output:
My Name is Biswajit Dey
Teacher of sub: Computer Sc.

Otherwise if you omit the endl manipulator the output would be:
Output:
My Name is Biswajit DeyTeacher of sub: Computer Sc.
Thus endl manipulator is useful to print the output in different lines.

setw() Manipulator:

setw manipulator is used to set the field width. This manipulator is useful in the situation where you want to print the output in the columnar format. To use setw( ) you need to include the header file iomanip.h in your program.
Example:
//using setw manipulator
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
int marks1=66;
float marks2=75.5;
float marks3=80.5;

cout<<"Name:"<<setw(8)<<"Class"<<setw(8)<<"Marks"<<endl
<<setw(8)<<"xyz"<<setw(8)<<"X"<<setw(8)<<marks1<<endl
<<setw(8)<<"pqr"<<setw(8)<<"X"<<setw(8)<<marks2<<endl
<<setw(8)<<"sty"<<setw(8)<<"X"<<setw(8)<<marks3<<endl;
getch();
}
Output:

Note: text is printed in a columnar format.

setprecision() Manipulator:

The setprecision()  is used to set the number of decimal places to be displayed, while printing a floating point number. To use setprecision() you need to include the header file iomanip.h in your program and to accomplish this task it is required to set an ios flag using setf( ) before using setprecision( ).

Example:
cout.setf(ios::fixed);
cout<<setprecision(5)<<24.123456;
Output:
24.12346
Note: number is printed after rounding up the last decimal digit.

IOS flags:

setf() function is the member of ios_base class. It is used to set the stream’s format flags. Options can be used with setf() are as follows:
Options                       Description
left                              left – justify the output
right                            right – justify the output
showpoint                   displays the decimal points and trailing zeros for all floating point numbers, even
                                  if the decimal places are not needed
uppercase                  displays the ‘e’ in Exponent notation as ‘E’
showpos                    displays a leading plus sign before a positive value
scientific                     displays floating point numbers scientific (‘E’) notation
fixed                          displays floating point numbers in normal notations – no trailing zeros and no
                                   scientific notations.

Note: ios flag options are sticky in nature, they used to continue with the subsequent cout (s) used in the program unless a particular flag is unset using unsetf(), it effect continues later in the program. Below a program is given demonstrating the use of setf() and unsetf() follow it well.

 #include<iostream.h>
 #include<iomanip.h>
 #include<conio.h>
 void main()
 {
 clrscr();
 float number1 = 123.456, number2 = 25.50;
 cout<<number1<<setw(16)<<number2<<endl; //Line 1
 cout.setf(ios::fixed);
 cout<<number1<<setw(16)<<number2<<endl<<endl;//Line 2
 cout.setf(ios::showpoint);
 cout<<number1<<setw(16)<<number2<<endl<<endl; //Line 3
 cout.setf(ios::scientific);
 cout<<number1<<setw(16)<<number2<<endl<<endl;//Line 4
 cout.setf(ios::showpos);
 cout<<number1<<setw(16)<<number2<<endl<<endl;//Line 5
 cout.setf(ios::fixed);
 cout<<number1<<setw(16)<<number2<<endl<<endl;//Line 6
 cout.unsetf(ios::showpoint);
 cout.setf(ios::fixed);
 cout<<number1<<setw(16)<<number2<<endl<<endl;/Line 7
 cout<<setprecision(2);
 cout<<number1<<setw(16)<<number2<<endl<<endl;//Line 8
 getch();
 }
Output:

Line 1
 Line 2
 Line 3
 Line 4
 Line 5
 Line 6
 Line 7
Line 8

No comments:

Post a Comment