Académique Documents
Professionnel Documents
Culture Documents
Introduction
Every program has input after processing it generates the output. It is essential to know how to provide input and how to present result in desired form. For that cin, cout with operators >> and<< is used. But we should know how to control the desired output.
C++ Streams
C++ uses the concept of streams to implement its I/O operations with the console and disk files. Stream is the interface to the programmer which is independent from the actual device used. Stream is a sequence of bytes. It either acts as source or destination.
Types of streams
Source stream:- from which input data to be obtained. Destination Stream:- that receives output from the program.
Data streams
Input Device Input Stream
Program
Output Device
Output stream
Contt
C++ contains several predefined streams cin represents input stream connected to standard input device. cout represents output stream connected to standard output device.
istreamt
ostream
iostream
Class hierarchy
Class name
ios
Contents
Basic facilities used by input,output classes . Declares functions and constants that are necessary for handling I/O.
istream
Inherit properties from ios. Declare input functions get(),getline(), extraction operator>> Inherit properties from ios. Declare input functions put(),write(), extraction operator<<
ostream
iostream
precision()
To specify the number of digits to be displayed after the decimal point of a float value. To specify a character that is fill the unused portion of a field.
fill()
Manipulators
Manipulators Equivalent ios functions
setw()
setprecision() setfill()
width()
precisions() fill()
C++ Manipulators
What is a Manipulator? Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer's choice of display. There are numerous manipulators available in C++. Some of the more commonly used manipulators are
endl Manipulator: This manipulator has the same functionality as the \n' newline.
cout << "Exforsys" << endl; cout << "Training";
setw Manipulator: This manipulator sets the minimum field width on output. The syntax is: setw(x)
#include<iomanip.h> int main() { clrscr(); int x=9; cout<<setw(6)<<"hi"<<setw(10)<<"hello" <<endl<<setw(4)<<"bye"<<setw(10)<<x<< endl; getch(); return 0; }
setfill Manipulator: This is used after setw manipulator. If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.
int main() { clrscr(); int x=9; cout<<setw(6)<<"hi"<<setfill(*) <<setw(10)<<"hello" <<endl<<setw(4)<<"bye"<<setw(10) <<x<<endl; getch(); return 0; }
setprecision Manipulator: The setprecision Manipulator is used with floating point numbers. It is used to set the number of digits printed to the right of the decimal point. This may be used in two forms:
fixed scientific
These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator. The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation. The keyword scientific, before the setprecision manipulator, prints the floating point number in scientific notation.
Stream base
setw()
setprecision() setfill()