MANAGING CONSOLE I/O OPERATIONS Submitted by:- Namita Pandey 2011BTechece020
May 22, 2015
MANAGING CONSOLE I/OOPERATIONS
Submitted by:- Namita Pandey 2011BTechece020 Shiva Johari 2011BTechcse015
Pritam Kalwaniya 2011BTechcse008
OUTLINE
Introduction
Streams & Stream Classes
Unformatted Input Output Operations
Formatted Console Input Output Operation
Formatting Flags, Bit fields and setf()
Designing Our Own Manipulators
INTRODUCTION
Managing Console I/O Operations
INPUT & OUTPUT
C++ supports a rich set of I/O operations
C++ uses the concept of stream & stream classes
STREAMS ???o A sequence of bytes o An interface between program and device
INPUTDEVICE
INSERTION INTO OUTPUTSTREAM
EXTRACTION FROM INPUTSTREAM
PROGRAM
OUTPUTDEVICE
INPUTSTREAM
OUTPUTSTREAM
STREAM CLASSES ??? C++ contains a hierarchy of classes that are used to define various streams
ios
Ostream_withassign
iostream
ostreamstreambuf
Iostream_withassign
istream
Istream_withassign
INPUTPOINTER
OUTPUT
UNFORMATTED I/O OPERATIONS
Overloaded operators >> and <<
get() and put() functions
getline() and write() functions
FORMATTED I/O OPERATIONS
C++ supports a number of features which can be used for formatting the output. These features include :-
ios class functions Manipulators User-defined Manipulators
ios format functions
FUNCTION
Width()
Precision()
Fill()
Setf()
Unsetf()
TASK
To specify the required field size for displaying the output value
To specify the digits to be displayed after decimal point of a float value
To specify a character that is used to fill the unused portion of a field
To specify format flags that can control the form of output display
To clear the flags specified
MANIPULATORS
MANIPULATORS
setw()
setprecision()
setfill()
setiosflags()
resetiosflags()
EQUIVALENT IOS FUNCTION
width()
precision()
fill()
setf()
unset()
Setting Width : width()
cout.width(w);
cout.width(5);cout<<543<<12<<“\n”;
cout.width(5);`cout<<543;cout.width(5);cout<<12<<“\n”;
4 35 21
5 4 3 21
#include<iostream.h>
int main(){ int item[4] =
{10,8,12,15}; int cost[4] =
{75,100,60,99}; cout.width(5); cout << “ITEMS”; cout.width(8); cout << “COST”; cout.width(15); cout << value<<“\
n”; sum =sum + value; int sum = 0; for(int i=0; i<4;i++) { cout.width(5);
cout << items[i];
cout.width(8); cout << cost[i];
int value = items[i] * cost[i];
cout.width(15); cout << value <<“\
n”; sum =sum + value;} cout << “\n Grand
Total = “;
cout.width(2); cout << sum <<“\
n”;
return 0;
}
Output
ITEMS COST TOTAL VALUE 10 15 150 8 100 800 12 60 720 15 99 1485
Setting Precision : precision()
cout.precision(n);
cout.precision(3);cout<<sqrt(2)<<“\n”;cout<<3.14159<<“\n”;cout<<2.50032<<“\n”;
1.1413.1422.5
#include<iostream.h>#include<conio.h>void main(){
float pi=22.0/7.0;int I;cout<<“Value of pi :\n “;for(i=1;i<=10;i++){
cout.width(i+1);cout.precision(i);cout<<pi<<“\n”;
}}
Value of pi :3.13.143.1433.14293.142863.1428573.14285713.142857073.1428570753.1428570747
OUTPUT
cout.fill(ch);
cout.fill(‘*’);cout.width(10);cout<<5250<<“\n”;
* * ** * * 0525
Filling & Paddling : fill()
#include<iostream.h>#include<conio.h>void main(){cout.fill(‘<‘);
cout.precision(3);for(int n=1;n<=6;n++){
cout.width(5);cout<<n;cout.width(10);cout<<1.0/
float(n)<<“\n”;if(n==3) cout.fill(‘>’);
}
cout<<“\n Paddling Changed\n\n”;cout.fill(‘#’);cout.width(15);cout<<12.345678<<“\n”;
return 0;}
<<<<1<<<<<<<<<1<<<<2<<<<<<<0.5<<<<3<<<<<<<0.3>>>>4>>>>>>0.25>>>>5>>>>>>>0.2>>>>6>>>>>0.167
Paddling Changed
#########12.346
OUTPUT
Formatting Flags, Bit-fields and setf()
cout.setf(arg1,arg2)
arg1 - formatting flags defined in the class ios
arg2 - it specifies the group to which the formatting flags belongs
Flags & Bit-fields for setf() function
Left-justified output
Right-justified output
Padding after sign or base Indicator (like
+##20)
Scientific Notation
Fixed Point notation
Decimal Base
ios::left
ios::right
ios::internal
ios::scientific
ios::fixed
ios::dec
ios::adjustfield
ios::adjustfield
ios::adjustfield
ios::floatfield
ios::floatfield
ios::basefield
FORMAT REQUIRED FLAG (ARG1) BIT-FIELD (ARG2)
#include<conio.h>#include<iostream.h>main(){
cout.setf(ios::fixed, ios::floatfield); float x=1234.67 ; cout<<x<<endl;
cout.setf(ios::scientific, ios::floatfield); x=.123467 ; cout<<x;
getch();
}
OUTPUT
1234.668234
1.234672e-01
#include<iostream.h>#include<conio.h>void main(){
int num;cout<<“enter an integer value”;cin>>num;
cout<<“The hexadecimal, octal and decimal representation is : ”;
cout.setf(ios::hex, ios::basefield)cout<<num<<“, “;
cout.setf(ios::oct, ios::basefield) cout<<num<<“, “;
cout.setf(ios::dec, ios::basefield)cout<<“ and “<<num<<“
respectively”;}
Enter an integer value : 92
The hexadecimal, octal and decimal representation of 92 is: 5c, 134 and 92 respectively.
OUTPUT
DESIGNING OUR OWN MANIPULATORS
ostream & manipulator (ostream & output){………………………… (code)……………
return output;}
So, what is the main conclusion ??
BIBLIOGRAPHY_____________
We have taken all the basic ideas about the concepts from the book “OBJECT ORIENTED TECHNIQUES” – by E. Balagurusamy
Images are made in Ms- Paint
& every thing is accompanied by ideas of our own
Any Queries ???
QUI
Z
TIM
E
Q.1The function of istream class is to :
a) inherit the properties of ios
b)Declares input functions such as get(), getline(), read() etc.
c)Contains overloaded extraction operator >>
d)All of the above
Q2.The function of streambuf is to :
a) provides an interface to physical devices through buffers
b)Can’t act as a base for filebuf class used for ios files
c) Declares constants and functions that are necessary for handling formatted i/p and o/p operations
d)None of the above
Q3.A stream is a sequence of ___________.
a) Bytes
b) Files
c) Manipulators
d) None of the above
Q4.Which are the member functions of ios class :
a)precision()
b)width()
c)fill()
d)All the above
Q5.What will be the output of following :
cout.fill(‘*’);cout.precision(3);cout.setf(ios::internal, ios::adjustfield);cout.setf(ios::scientific, ios::floatfield);cout.width(15);
cout<<-12.34567<<“\n”;
(.A
D.)
B.)
(.C
-*****1.235e+01
-*****.1235e+02-*********1.236
-******1.235e+01
Q6.a) The __________ operator is overloaded in the istream
class
a) Insertion
b) >>
c) <<
d) None of the above
Q7.Which Class is needed to be virtual in this case :
ios
Ostream_withassign
iostream
ostreamstreambuf
Iostream_withassign
istream
Istream_withassign
INPUTPOINTER
OUTPUT
a.) iostreamb.) iosc.) istream or ostreamd.) no one is required
True / False
Q8.The header file iomanip can be used in place of iostream ??
Q9. programmer can’t define a manipulator that could represent a set of formatted functions ??
Q10.What is the default precision value ??
a.) 0 b.) 4
c.) 6 d.) 3
THANKSA
LOT