SlideShare a Scribd company logo
Managing Console I/O Operations
Chap 10
1By:-Gourav Kottawar
Contents
• 10.1 C++ Streams, C++ Stream Classes
• 10.2 Unformatted I/O Operation
• 10.3 Formatted I/O Operation
• 10.4 Managing Output with Manipulators
2By:-Gourav Kottawar
Managing Console I/O
Operations
• C++ uses the concept of stream and
stream classes to implement its I/O
operations with the console and disk
files.
• C++ support all of C’s rich set of I/O
functions.
3By:-Gourav Kottawar
C ++ Stream
• Stream is an interface supplied by the
I/O system of C++ between the
programmer and the actual device
being accessed.
• It will work with devices like
terminals, disks and tape drives.
• A stream is a sequence of bytes.
• It acts either as a source from which
the input data can be obtained or as a
destination to which the output data
can be sent. 4By:-Gourav Kottawar
C ++ Stream
• Input Stream - The source stream that
provides data to the program.
• Output Stream - The destination stream
that receives output from the program.
continue…
Input device
Output device
Program
Input Stream
Output Stream
Extraction from
input stream
Insertion into
output stream
5By:-Gourav Kottawar
C ++ Stream
• The data in the input stream can
come from keyboard or any other
storage device.
• The data in the output stream can go to the
screen or any other storage device.
continue…
Input device
Output device
Program
Input Stream
Output Stream
Extraction from
input stream
Insertion into
output stream
6By:-Gourav Kottawar
C ++ Stream Classes
• The C++ I/O system contains a
hierarchy of classes that are used to
define various streams to deal with
both the console and disk files.
• These classes are called stream
classes.
• These classes are declared in the
header file iostream. 7By:-Gourav Kottawar
C ++ Stream Classes
ios
istream ostreamstreambuf
iostream
istream_withassign iostream_withassign ostream_withassign
continue…
8By:-Gourav Kottawar
C ++ Stream Classes
ios
istream ostreamstreambuf
iostream
istream_withassign iostream_withassign ostream_withassign
continue…
Provides the basic
support for formatted
and unformatted I/O
operations.
Provides the facilities
for formatted and
unformatted input
Provides the
facilities for
formatted output
Provides the facilities
for handling both input
and output streams.
9By:-Gourav Kottawar
Unformatted I/O Operations
Overloaded Operators >> and <<
• The objects cin and cout are used for input
and output of data of various types.
• By overloading the operators >> and <<.
• >> operator is overloaded in the istream
class.
• << operator is overloaded in the ostream
class.
• This is used for input data through
keyboard.
10By:-Gourav Kottawar
Unformatted I/O Operations
Overloaded Operators >> and <<
cin >> variable1 >> varibale2 ... >> variableN
where variable1, variable2, …, variableN are valid C+
+ variable names.
cout << item1 << item2 << … << itemN
Where item1, item2, …,itemN may be variables or
constants of an basic type.
11By:-Gourav Kottawar
Unformatted I/O Operations
put( ) and get( ) Functions
• get( ) and put( ) are member functions of
istream and ostream classes.
• For single character input/output
operations.
• There are two types of get( ) functions:
– get(char*)  Assigns the input character to its argument.
– get(void)  Returns the input character.
• char c; cin.get(c) c = cin.get( );
– put( )  used to output a line of text, character
by character.
• char c; cout.put(‘x’); cout.put(c);
12By:-Gourav Kottawar
#include<iostream.h>
int main()
{
int i =0;
char ch;
cout<<“n Enter data”;
cin.get(ch);
while(ch!=‘n’)
{
cout.put(ch);
i++;
cin.get(ch);
}
cout<<“n Number of characters =“<<I;
}
13By:-Gourav Kottawar
#include<iostream.h>
int main()
{
int i =0;
char ch;
cout<<“n Enter data :”;
cin.get(ch);
while(ch!=‘n’)
{
cout.put(ch);
i++;
cin.get(ch);
}
cout<<“n Number of characters =“<<I;
}
Output :
Enter data : Applying thought
Applying thoughts
Number of characters : 17
14By:-Gourav Kottawar
Unformatted I/O Operations
getline( ) and write( ) Functions
• getline( ) function reads a whole line of text
that ends with a newline character.
• cin.getline(line, size);
• Reading is terminated as soon as either the
newline character ‘n’ is encountered or
size-1 characters are read.
• write( ) function displays an entire line of
text.
• cout.write(line, size);
• write( ) also used to concatenate strings.
15By:-Gourav Kottawar
#include<iostream.h>
int size = 10;
int main()
{
char name[10];
cout<<“n Can I know u r name :”;
cin>>name;
cout<<“Name : “<<name;
cout<<“n Come again :”;
cin.getline(name,size);
cout<<“Name :- “<<name;
}
16By:-Gourav Kottawar
#include<iostream.h>
int main()
{
char *s1=“Object”;
char *s2=“Oriented”;
int a=strlen(s1); int b=strlen(s2);
for(int i=0;i<b;i++)
{
cout.write(s2,i);
cout<<“n”;
}
for(i=b;i>0;i--)
{
cout.write(s2,i);
cout<<“n”;
}
// concatenation
cout.write(s1,a).write(s2,b);
cout<<“n”;
cout.write(s1,10);
}
17By:-Gourav Kottawar
Formatted Console I/O
Operations
C++ supports a number of features that
could be used for formatting the output.
These features include:
–ios class functions and flags.
– Manipulators.
– User-defined output functions.
18By:-Gourav Kottawar
ios member functions
width( )  to specify the required field size for
displaying an output value.
precision( )  to specify the number of digits to be
displayed after the decimal point of a float value.
fill( )  to specify a character that is used to fill the
unused portion of a field.
setf( )  to specify format flags that can control the
form of output display.
unsetf( )  to clear the flags specified.
19By:-Gourav Kottawar
20By:-Gourav Kottawar
21By:-Gourav Kottawar
int main()
{
int a[4] = {1,2,3,4};
int c[4]={10,20,30,40};
cout.width(5);
cout<<“Items “;
cout.width(8);
cout<<“Cost”;
cout.width(15);
cout<“Total “;
int sum=0;
for(i=0;i<4;i++)
{
cout.width(5);
cout<<a[i];
cout.width(8);
cout<<c[i];
int val=a[i]*c[i];
cout.width(15);
cout<<value<<endl;
sum=sum+val;
}
cout<<“n Grand Total “;
cout.width(2);
cout<<sum<<“n”;
}
22By:-Gourav Kottawar
int main()
{
int i[4] = {1,2,3,4};
int c[4]={10,20,30,40};
cout.width(5);
cout<<“Items “;
cout.width(8);
cout<<“Cost”;
cout.width(15);
cout<“Total “;
int sum=0;
for(i=0;i<4;i++)
{
cout.width(5);
cout<<i[i];
cout.width(8);
cout<<c[i];
int val=i[i]*c[i];
cout.width(15);
cout<value>endl;
sum=sum+val;
}
cout<<“n Grand Total “;
cout.width(2);
cout<<sum<<“n”;
}
Output :
Items Cost Total
110 10
220 40
330 90
440 160
Grand total = 300
23By:-Gourav Kottawar
#include<iostream.h>
#include<math.h>
int main()
{
cout.precision(3); // 3 digit precision
cout.width(10);
cout<<“value”;
cout.width(15);
cout<<“Sqrt of value “<<“n;
for(int i=1;i<=5;i++)
{
cout.width(8);
cout<<“n”;
cout.width(13);
cout<<sqrt(i);
cout<<“n”;
cout.precision(5); // 5 digits precision
cout<<“Sqrt(10) = “<<sqrt(10)<<“n”;
cout.precision(0); // default setting
cout<<“Sqrt(10) =“ << sqrt(10) “ (default setting)n”;
}
}
24By:-Gourav Kottawar
Manipulators
Manipulators are special functions that can
be included in the I/O statement to alter
the format parameters of a stream.
To access manipulators, the file iomanip.h
should be included in the program.
– setw( )
– setprecision( )
– setfill( )
– setiosflags( )
– resetiosflags( )
25By:-Gourav Kottawar
width( )  Defining Field Width
To define the width of a field necessary for
the output of an item.
Since it is a member function, we have to use
an object to invoke it.
cout.width(w)
Where w is the field width (number of
columns).
The output will be printed in a field of w
characters wide at the right end of the field.
26By:-Gourav Kottawar
width( )  Defining Field Width
The width( ) function can specify the
field width for only one item – item
that follows immediately.
cout.width(5);
cout << 543 << 12 <<“n”;
cout.width(5);
cout << 543;
cout.width(5);
cout << 12 << “n”;
5 4 3 1 2
5 4 3 1 2
The field should be specified
for each item separately.
27By:-Gourav Kottawar
precision( )  Setting Precision
• Used to specify the number of digits to be
displayed after the decimal point while printing the
floating-point numbers.
• By default, the floating numbers are printed with six
digits after the decimal point.
• cout.precision(d);
– Where d is the number of digits to the right of the decimal
point.
cout.precision(3);
cout << sqrt(2) << endl;
cout << 3.14159 << endl;
cout <<2.50032 << endl;
1.141  truncated
3.142  rounded
2.5 o trailing zeros
28By:-Gourav Kottawar
precision( )  Setting Precision
• Unlike width( ), precision ( ) retains the setting
in effect until it is reset.
• We can also combine the field specification
with the precision setting.
cout.precision(2);
cout.width(5);
cout << 1.2345;
1 . 2 3
29By:-Gourav Kottawar
fill( )  Filling and Padding
• When printing values with larger field width
than required by the values, the unused
positions of the field are filled with white
spaces, by default.
• fill( ) function can be used to fill the unused
positions by any desired character.
cout.fill(ch);
where ch represents the character which is
used for filling the unused positions.
cout.fill(‘ * ’);
cout.wdith(10);
cout << 5250 << endl;
* * * * * 5 05 2*
Like precision( ), fill( ) stays
in effect till we change it.
30By:-Gourav Kottawar
setf( )  Formatting Flags, Bit-
fields
• When the function width( ) is used, the value
will be printed right-justified in the created
width.
• setf( )function is used to print values in left-
justified.
• cout.setf(arg1, arg2)
eg:
cout.fill(‘ * ’);
cout.setf(ios :: left, ios :: adjustfield);
cout.wdith(10);
cout << “TABLE 1” << endl;
T A B L E * *1 *
arg1 is formatting flags
defined in the class ios and
arg2 is bit field constant in
the ios class.
31By:-Gourav Kottawar
setf( )  Formatting Flags,
Bit-fields
• When the function width( ) is used, the value
will be printed right-justified in the created
width.
• setf( )function is used to print values in left-
justified.
• cout.setf(arg1, arg2)
eg:
cout.fill(‘ * ’);
cout.setf(ios :: left, ios :: adjustfield);
cout.wdith(10);
cout << “TABLE 1” << endl;
T A B L E * *1 *
arg1 is formatting flags
defined in the class ios and
arg2 is bit field constant in
the ios class.
The formatting flag
specifies the format action
required for the output.
Bit field specifies the
group to which the
formatting flag belongs.
32By:-Gourav Kottawar
Managing Output with Manipulators
• The header file iomanip provides a set of
functions called manipulators which can be
used to manipulate the output formats.
• Some manipulators are more convenient to
use than the member functions and flags of
ios.
• Two or more manipulators can be used as a
chain in one statement.
cout << manip1 << manip2 << maip3 << item;
cout << manip1 << item1 << manip2 << item2;
33By:-Gourav Kottawar
Managing Output with Manipulators
• Manipulators and their meanings
cout << setw(5) << setprecision(2) << 1.2345
<< setw(10) << setprecision(4) << sqrt(2);
• We can jointly use the manipulators and the ios
functions in a program.
Manipulators Meaning Equivalent
setw(int w) Set the field width to w width( )
setprecision(int d) Set floating point precision to d precision( )
setfill( int c) Set the fill character to c fill( )
34By:-Gourav Kottawar
Manipulators & ios Member
Functions
• The ios member function return the previous
format state which can be used later.
• But the manipulator does not return the
previous format state.
cout.precision(2); // previous state.
int p =cout.precision(4); // current state, p=2.
cout.precision(p); // change to previous state
35By:-Gourav Kottawar
Designing Our Own Manipulators
• We can design our own manipulators for
certain special purposes.
ostream & manipulator ( ostream & output)
{
……… (code)
return output;
}
ostream & unit (ostream & output)
{
output << “inches”;
return output;
}
cout << 36 << unit;  will produce “36 inches”.
36By:-Gourav Kottawar
Designing Our Own Manipulators
We can also create manipulators that could
represent a sequence of operations:
ostream & show (ostream & output)
{
output.setf(ios::showpoint);
output.setf(ios::showpos);
output << setw(10);
return output;
}
This function defines a manipulator called show that turns
on the flags showpoint and showpos declared in the class
ios and sets the field width to 10.
37By:-Gourav Kottawar
Thank You
38By:-Gourav Kottawar
Ad

More Related Content

What's hot (20)

Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
CGC Technical campus,Mohali
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
Kamal Acharya
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
Pallavi Seth
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Turtle graphics
Turtle graphicsTurtle graphics
Turtle graphics
grahamwell
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Himanshu Kaushik
 
C++ programming
C++ programmingC++ programming
C++ programming
Emertxe Information Technologies Pvt Ltd
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
AkshayAggarwal79
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
javaTpoint s
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
Akhil Kaushik
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Csv file read and write
Csv file read and writeCsv file read and write
Csv file read and write
comsats university of science information technology
 
Python ppt
Python pptPython ppt
Python ppt
Anush verma
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
Kamal Acharya
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
Pallavi Seth
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Turtle graphics
Turtle graphicsTurtle graphics
Turtle graphics
grahamwell
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
AkshayAggarwal79
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
javaTpoint s
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 

Viewers also liked (17)

Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Union In language C
Union In language CUnion In language C
Union In language C
Ravi Singh
 
C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
Pedro Hugo Valencia Morales
 
Input output streams
Input output streamsInput output streams
Input output streams
Parthipan Parthi
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
Online
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
gourav kottawar
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
Managing console
Managing consoleManaging console
Managing console
Shiva Saxena
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Application software
Application softwareApplication software
Application software
UOS
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Kumar
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
Ram Sagar Mourya
 
file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
Haresh Jaiswal
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Union In language C
Union In language CUnion In language C
Union In language C
Ravi Singh
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
Online
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
gourav kottawar
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Application software
Application softwareApplication software
Application software
UOS
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Kumar
 
file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
Haresh Jaiswal
 
Ad

Similar to cpp input & output system basics (20)

Managing console input
Managing console inputManaging console input
Managing console input
rajshreemuthiah
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
gourav kottawar
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPTC++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
BayanbekAmantay
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
KamranAli649587
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
TarekHemdan3
 
C++InputOutput.pptx
C++InputOutput.pptxC++InputOutput.pptx
C++InputOutput.pptx
ansariparveen06
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
 
Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++
sujathavvv
 
Intro to C++
Intro to C++Intro to C++
Intro to C++
Ahmed Farag
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
Mohammad Shaker
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
Haripritha
 
embedded C.pptx
embedded C.pptxembedded C.pptx
embedded C.pptx
mohammedahmed539376
 
C++ Programs
C++ ProgramsC++ Programs
C++ Programs
NarayanlalMenariya
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
WaheedAnwar20
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptx
DeepasCSE
 
APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...
APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...
APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...
zohaib247331
 
introduction to c++concept presentation.pptx
introduction to c++concept presentation.pptxintroduction to c++concept presentation.pptx
introduction to c++concept presentation.pptx
zeenatparveen24
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
gourav kottawar
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPTC++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
C++filehhjglnkn,jhjgnfcbfshbkh.,lm'\;;.PPT
BayanbekAmantay
 
Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++
sujathavvv
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
Haripritha
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptx
DeepasCSE
 
APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...
APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...
APznzaZkmb-kUZ4l6kBr4KiljHkZbgRJ-CE0UrfFTiDbqmDWIG7V3w1LoSuo933888-Arg_FpAn4D...
zohaib247331
 
introduction to c++concept presentation.pptx
introduction to c++concept presentation.pptxintroduction to c++concept presentation.pptx
introduction to c++concept presentation.pptx
zeenatparveen24
 
Ad

More from gourav kottawar (20)

constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cpp
gourav kottawar
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
gourav kottawar
 
basics of c++
basics of c++basics of c++
basics of c++
gourav kottawar
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
gourav kottawar
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
gourav kottawar
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
gourav kottawar
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
basics of c++
basics of c++basics of c++
basics of c++
gourav kottawar
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
gourav kottawar
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
gourav kottawar
 
SQL || overview and detailed information about Sql
SQL || overview and detailed information about SqlSQL || overview and detailed information about Sql
SQL || overview and detailed information about Sql
gourav kottawar
 
SQL querys in detail || Sql query slides
SQL querys in detail || Sql query slidesSQL querys in detail || Sql query slides
SQL querys in detail || Sql query slides
gourav kottawar
 
Rrelational algebra in dbms overview
Rrelational algebra in dbms overviewRrelational algebra in dbms overview
Rrelational algebra in dbms overview
gourav kottawar
 
overview of database concept
overview of database conceptoverview of database concept
overview of database concept
gourav kottawar
 
Relational Model in dbms & sql database
Relational Model in dbms & sql databaseRelational Model in dbms & sql database
Relational Model in dbms & sql database
gourav kottawar
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
gourav kottawar
 
security and privacy in dbms and in sql database
security and privacy in dbms and in sql databasesecurity and privacy in dbms and in sql database
security and privacy in dbms and in sql database
gourav kottawar
 
The system development life cycle (SDLC)
The system development life cycle (SDLC)The system development life cycle (SDLC)
The system development life cycle (SDLC)
gourav kottawar
 
Software documentation
Software documentationSoftware documentation
Software documentation
gourav kottawar
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cpp
gourav kottawar
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
gourav kottawar
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
gourav kottawar
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
gourav kottawar
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
gourav kottawar
 
SQL || overview and detailed information about Sql
SQL || overview and detailed information about SqlSQL || overview and detailed information about Sql
SQL || overview and detailed information about Sql
gourav kottawar
 
SQL querys in detail || Sql query slides
SQL querys in detail || Sql query slidesSQL querys in detail || Sql query slides
SQL querys in detail || Sql query slides
gourav kottawar
 
Rrelational algebra in dbms overview
Rrelational algebra in dbms overviewRrelational algebra in dbms overview
Rrelational algebra in dbms overview
gourav kottawar
 
overview of database concept
overview of database conceptoverview of database concept
overview of database concept
gourav kottawar
 
Relational Model in dbms & sql database
Relational Model in dbms & sql databaseRelational Model in dbms & sql database
Relational Model in dbms & sql database
gourav kottawar
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
gourav kottawar
 
security and privacy in dbms and in sql database
security and privacy in dbms and in sql databasesecurity and privacy in dbms and in sql database
security and privacy in dbms and in sql database
gourav kottawar
 
The system development life cycle (SDLC)
The system development life cycle (SDLC)The system development life cycle (SDLC)
The system development life cycle (SDLC)
gourav kottawar
 

Recently uploaded (20)

Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdfAPM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
Association for Project Management
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 

cpp input & output system basics

  • 1. Managing Console I/O Operations Chap 10 1By:-Gourav Kottawar
  • 2. Contents • 10.1 C++ Streams, C++ Stream Classes • 10.2 Unformatted I/O Operation • 10.3 Formatted I/O Operation • 10.4 Managing Output with Manipulators 2By:-Gourav Kottawar
  • 3. Managing Console I/O Operations • C++ uses the concept of stream and stream classes to implement its I/O operations with the console and disk files. • C++ support all of C’s rich set of I/O functions. 3By:-Gourav Kottawar
  • 4. C ++ Stream • Stream is an interface supplied by the I/O system of C++ between the programmer and the actual device being accessed. • It will work with devices like terminals, disks and tape drives. • A stream is a sequence of bytes. • It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent. 4By:-Gourav Kottawar
  • 5. C ++ Stream • Input Stream - The source stream that provides data to the program. • Output Stream - The destination stream that receives output from the program. continue… Input device Output device Program Input Stream Output Stream Extraction from input stream Insertion into output stream 5By:-Gourav Kottawar
  • 6. C ++ Stream • The data in the input stream can come from keyboard or any other storage device. • The data in the output stream can go to the screen or any other storage device. continue… Input device Output device Program Input Stream Output Stream Extraction from input stream Insertion into output stream 6By:-Gourav Kottawar
  • 7. C ++ Stream Classes • The C++ I/O system contains a hierarchy of classes that are used to define various streams to deal with both the console and disk files. • These classes are called stream classes. • These classes are declared in the header file iostream. 7By:-Gourav Kottawar
  • 8. C ++ Stream Classes ios istream ostreamstreambuf iostream istream_withassign iostream_withassign ostream_withassign continue… 8By:-Gourav Kottawar
  • 9. C ++ Stream Classes ios istream ostreamstreambuf iostream istream_withassign iostream_withassign ostream_withassign continue… Provides the basic support for formatted and unformatted I/O operations. Provides the facilities for formatted and unformatted input Provides the facilities for formatted output Provides the facilities for handling both input and output streams. 9By:-Gourav Kottawar
  • 10. Unformatted I/O Operations Overloaded Operators >> and << • The objects cin and cout are used for input and output of data of various types. • By overloading the operators >> and <<. • >> operator is overloaded in the istream class. • << operator is overloaded in the ostream class. • This is used for input data through keyboard. 10By:-Gourav Kottawar
  • 11. Unformatted I/O Operations Overloaded Operators >> and << cin >> variable1 >> varibale2 ... >> variableN where variable1, variable2, …, variableN are valid C+ + variable names. cout << item1 << item2 << … << itemN Where item1, item2, …,itemN may be variables or constants of an basic type. 11By:-Gourav Kottawar
  • 12. Unformatted I/O Operations put( ) and get( ) Functions • get( ) and put( ) are member functions of istream and ostream classes. • For single character input/output operations. • There are two types of get( ) functions: – get(char*)  Assigns the input character to its argument. – get(void)  Returns the input character. • char c; cin.get(c) c = cin.get( ); – put( )  used to output a line of text, character by character. • char c; cout.put(‘x’); cout.put(c); 12By:-Gourav Kottawar
  • 13. #include<iostream.h> int main() { int i =0; char ch; cout<<“n Enter data”; cin.get(ch); while(ch!=‘n’) { cout.put(ch); i++; cin.get(ch); } cout<<“n Number of characters =“<<I; } 13By:-Gourav Kottawar
  • 14. #include<iostream.h> int main() { int i =0; char ch; cout<<“n Enter data :”; cin.get(ch); while(ch!=‘n’) { cout.put(ch); i++; cin.get(ch); } cout<<“n Number of characters =“<<I; } Output : Enter data : Applying thought Applying thoughts Number of characters : 17 14By:-Gourav Kottawar
  • 15. Unformatted I/O Operations getline( ) and write( ) Functions • getline( ) function reads a whole line of text that ends with a newline character. • cin.getline(line, size); • Reading is terminated as soon as either the newline character ‘n’ is encountered or size-1 characters are read. • write( ) function displays an entire line of text. • cout.write(line, size); • write( ) also used to concatenate strings. 15By:-Gourav Kottawar
  • 16. #include<iostream.h> int size = 10; int main() { char name[10]; cout<<“n Can I know u r name :”; cin>>name; cout<<“Name : “<<name; cout<<“n Come again :”; cin.getline(name,size); cout<<“Name :- “<<name; } 16By:-Gourav Kottawar
  • 17. #include<iostream.h> int main() { char *s1=“Object”; char *s2=“Oriented”; int a=strlen(s1); int b=strlen(s2); for(int i=0;i<b;i++) { cout.write(s2,i); cout<<“n”; } for(i=b;i>0;i--) { cout.write(s2,i); cout<<“n”; } // concatenation cout.write(s1,a).write(s2,b); cout<<“n”; cout.write(s1,10); } 17By:-Gourav Kottawar
  • 18. Formatted Console I/O Operations C++ supports a number of features that could be used for formatting the output. These features include: –ios class functions and flags. – Manipulators. – User-defined output functions. 18By:-Gourav Kottawar
  • 19. ios member functions width( )  to specify the required field size for displaying an output value. precision( )  to specify the number of digits to be displayed after the decimal point of a float value. fill( )  to specify a character that is used to fill the unused portion of a field. setf( )  to specify format flags that can control the form of output display. unsetf( )  to clear the flags specified. 19By:-Gourav Kottawar
  • 22. int main() { int a[4] = {1,2,3,4}; int c[4]={10,20,30,40}; cout.width(5); cout<<“Items “; cout.width(8); cout<<“Cost”; cout.width(15); cout<“Total “; int sum=0; for(i=0;i<4;i++) { cout.width(5); cout<<a[i]; cout.width(8); cout<<c[i]; int val=a[i]*c[i]; cout.width(15); cout<<value<<endl; sum=sum+val; } cout<<“n Grand Total “; cout.width(2); cout<<sum<<“n”; } 22By:-Gourav Kottawar
  • 23. int main() { int i[4] = {1,2,3,4}; int c[4]={10,20,30,40}; cout.width(5); cout<<“Items “; cout.width(8); cout<<“Cost”; cout.width(15); cout<“Total “; int sum=0; for(i=0;i<4;i++) { cout.width(5); cout<<i[i]; cout.width(8); cout<<c[i]; int val=i[i]*c[i]; cout.width(15); cout<value>endl; sum=sum+val; } cout<<“n Grand Total “; cout.width(2); cout<<sum<<“n”; } Output : Items Cost Total 110 10 220 40 330 90 440 160 Grand total = 300 23By:-Gourav Kottawar
  • 24. #include<iostream.h> #include<math.h> int main() { cout.precision(3); // 3 digit precision cout.width(10); cout<<“value”; cout.width(15); cout<<“Sqrt of value “<<“n; for(int i=1;i<=5;i++) { cout.width(8); cout<<“n”; cout.width(13); cout<<sqrt(i); cout<<“n”; cout.precision(5); // 5 digits precision cout<<“Sqrt(10) = “<<sqrt(10)<<“n”; cout.precision(0); // default setting cout<<“Sqrt(10) =“ << sqrt(10) “ (default setting)n”; } } 24By:-Gourav Kottawar
  • 25. Manipulators Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. To access manipulators, the file iomanip.h should be included in the program. – setw( ) – setprecision( ) – setfill( ) – setiosflags( ) – resetiosflags( ) 25By:-Gourav Kottawar
  • 26. width( )  Defining Field Width To define the width of a field necessary for the output of an item. Since it is a member function, we have to use an object to invoke it. cout.width(w) Where w is the field width (number of columns). The output will be printed in a field of w characters wide at the right end of the field. 26By:-Gourav Kottawar
  • 27. width( )  Defining Field Width The width( ) function can specify the field width for only one item – item that follows immediately. cout.width(5); cout << 543 << 12 <<“n”; cout.width(5); cout << 543; cout.width(5); cout << 12 << “n”; 5 4 3 1 2 5 4 3 1 2 The field should be specified for each item separately. 27By:-Gourav Kottawar
  • 28. precision( )  Setting Precision • Used to specify the number of digits to be displayed after the decimal point while printing the floating-point numbers. • By default, the floating numbers are printed with six digits after the decimal point. • cout.precision(d); – Where d is the number of digits to the right of the decimal point. cout.precision(3); cout << sqrt(2) << endl; cout << 3.14159 << endl; cout <<2.50032 << endl; 1.141  truncated 3.142  rounded 2.5 o trailing zeros 28By:-Gourav Kottawar
  • 29. precision( )  Setting Precision • Unlike width( ), precision ( ) retains the setting in effect until it is reset. • We can also combine the field specification with the precision setting. cout.precision(2); cout.width(5); cout << 1.2345; 1 . 2 3 29By:-Gourav Kottawar
  • 30. fill( )  Filling and Padding • When printing values with larger field width than required by the values, the unused positions of the field are filled with white spaces, by default. • fill( ) function can be used to fill the unused positions by any desired character. cout.fill(ch); where ch represents the character which is used for filling the unused positions. cout.fill(‘ * ’); cout.wdith(10); cout << 5250 << endl; * * * * * 5 05 2* Like precision( ), fill( ) stays in effect till we change it. 30By:-Gourav Kottawar
  • 31. setf( )  Formatting Flags, Bit- fields • When the function width( ) is used, the value will be printed right-justified in the created width. • setf( )function is used to print values in left- justified. • cout.setf(arg1, arg2) eg: cout.fill(‘ * ’); cout.setf(ios :: left, ios :: adjustfield); cout.wdith(10); cout << “TABLE 1” << endl; T A B L E * *1 * arg1 is formatting flags defined in the class ios and arg2 is bit field constant in the ios class. 31By:-Gourav Kottawar
  • 32. setf( )  Formatting Flags, Bit-fields • When the function width( ) is used, the value will be printed right-justified in the created width. • setf( )function is used to print values in left- justified. • cout.setf(arg1, arg2) eg: cout.fill(‘ * ’); cout.setf(ios :: left, ios :: adjustfield); cout.wdith(10); cout << “TABLE 1” << endl; T A B L E * *1 * arg1 is formatting flags defined in the class ios and arg2 is bit field constant in the ios class. The formatting flag specifies the format action required for the output. Bit field specifies the group to which the formatting flag belongs. 32By:-Gourav Kottawar
  • 33. Managing Output with Manipulators • The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats. • Some manipulators are more convenient to use than the member functions and flags of ios. • Two or more manipulators can be used as a chain in one statement. cout << manip1 << manip2 << maip3 << item; cout << manip1 << item1 << manip2 << item2; 33By:-Gourav Kottawar
  • 34. Managing Output with Manipulators • Manipulators and their meanings cout << setw(5) << setprecision(2) << 1.2345 << setw(10) << setprecision(4) << sqrt(2); • We can jointly use the manipulators and the ios functions in a program. Manipulators Meaning Equivalent setw(int w) Set the field width to w width( ) setprecision(int d) Set floating point precision to d precision( ) setfill( int c) Set the fill character to c fill( ) 34By:-Gourav Kottawar
  • 35. Manipulators & ios Member Functions • The ios member function return the previous format state which can be used later. • But the manipulator does not return the previous format state. cout.precision(2); // previous state. int p =cout.precision(4); // current state, p=2. cout.precision(p); // change to previous state 35By:-Gourav Kottawar
  • 36. Designing Our Own Manipulators • We can design our own manipulators for certain special purposes. ostream & manipulator ( ostream & output) { ……… (code) return output; } ostream & unit (ostream & output) { output << “inches”; return output; } cout << 36 << unit;  will produce “36 inches”. 36By:-Gourav Kottawar
  • 37. Designing Our Own Manipulators We can also create manipulators that could represent a sequence of operations: ostream & show (ostream & output) { output.setf(ios::showpoint); output.setf(ios::showpos); output << setw(10); return output; } This function defines a manipulator called show that turns on the flags showpoint and showpos declared in the class ios and sets the field width to 10. 37By:-Gourav Kottawar