Submitted by:-    Namita Pandey
2011BTechece020
                    Shiva Johari
   Introduction
   Streams & Stream Classes
   Unformatted Input Output Operations
   Formatted Console Input Output Operation
   Formatting Flags, Bit fields and setf()
   Designing Our Own Manipulators
   Managing Console I/O Operations

   INPUT & OUTPUT

   C++ supports a rich set of I/O operations

    C++ uses the concept of stream & stream
    classes
o   A sequence of bytes
o   An interface between program and device
                     INPUT          EXTRACTION
                     STREAM         FROM INPUT
INPUT                               STREAM
DEVICE


                                    PROGRAM



OUTPUT
DEVICE
                                       INSERTION
                    OUTPUT             INTO OUTPUT
                    STREAM             STREAM
 C++ contains a hierarchy of classes that are used to define
various streams



                             ios
  INPUT                                              OUTPUT
                      POINTER

          istream        streambuf         ostream


                         iostream


Istream_withassign   Iostream_withassign   Ostream_withassign
   Overloaded operators >> and <<

   get() and put() functions

 getline() and write() functions
C++ supports a number of features which can
    be used for formatting the output. These
    features include :-

   ios class functions
   Manipulators
    User-defined Manipulators
FUNCTION                        TASK

   Width()          To specify the required field size for
                      displaying the output value
   Precision()      To specify the digits to be displayed
                      after 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
MANIPULATORS          EQUIVALENT IOS FUNCTION

   setw()               width()

   setprecision()       precision()

   setfill()            fill()

   setiosflags()        setf()

   resetiosflags()      unset()
cout.width(5);                 5 4 3 1 2
cout<<543<<12<<“n”;

cout.width(5);`
cout<<543;
cout.width(5);         5 4 3          1 2
cout<<12<<“n”;
#include<iostream.h>             cout.width(8);
                                 cout << cost[i];
int main()
{                                 int value = items[i] *
     int item[4] =               cost[i];
    {10,8,12,15};                 cout.width(15);
     int cost[4] =
                                  cout << value <<“n”;
    {75,100,60,99};
     cout.width(5);
                                  sum =sum + value;
     cout << “ITEMS”;        }
     cout.width(8);               cout << “n Grand
     cout << “COST”;             Total = “;
     cout.width(15);
     cout << value<<“n”;        cout.width(2);
     sum =sum + value;           cout << sum <<“n”;
     int sum = 0;
     for(int i=0; i<4;i++)       return 0;
    {
         cout.width(5);      }
         cout << items[i];
ITEMS    COST   TOTAL VALUE
    10     15            150
    8     100           800
    12     60            720
    15     99           1485
cout.precision(3);     1.141
cout<<sqrt(2)<<“n”;   3.142
cout<<3.14159<<“n”;
cout<<2.50032<<“n”;   2.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.1
3.14
3.143
3.1429
3.14286
3.142857
3.1428571
3.14285707
3.142857075
3.1428570747
cout.fill(‘*’);
cout.width(10);
cout<<5250<<“n”;

            * * * * * * 5 2 5 0
#include<iostream.h>                 cout<<“n Paddling Changednn”;
#include<conio.h>                    cout.fill(‘#’);
void main()                          cout.width(15);
{                                    cout<<12.345678<<“n”;
cout.fill(‘<‘);
                                     return 0;
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(‘>’);
}
<<<<1<<<<<<<<<1
<<<<2<<<<<<<0.5
<<<<3<<<<<<<0.3
>>>>4>>>>>>0.25
>>>>5>>>>>>>0.2
>>>>6>>>>>0.167

Paddling Changed

#########12.346
arg1 - formatting flags defined in the class ios

arg2 - it specifies the group to which the formatting flags belongs
FORMAT REQUIRED FLAG (ARG1)                    BIT-FIELD (ARG2)

  Left-justified output         ios::left
                                                  ios::adjustfield
  Right-justified output       ios::right
                                                  ios::adjustfield
Padding after sign or base   ios::internal
                                                  ios::adjustfield
  Indicator (like +##20)

   Scientific Notation       ios::scientific
                                                  ios::floatfield
  Fixed Point notation         ios::fixed
                                                  ios::floatfield
      Decimal Base              ios::dec
                                                  ios::basefield
#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();

}
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.
ostream & manipulator (ostream & output)
{
……………
…………… (code)
……………

return output;
}
 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
The 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
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
A stream is a sequence of ___________.


a) Bytes

b) Files

c) Manipulators

d) None of the above
Which are the member functions of ios class :

a) precision()

b) width()

c) fill()

d) All the above
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”;

-******1.235e+01               (.A            B.)           -*****1.235e+01




-*****.1235e+02
                                                            -*********1.236
                               (.C            D.)
a)   The __________ operator is overloaded in the istream class



a) Insertion

b) >>

c) <<

d) None of the above
Which Class is needed to be virtual in this case :

  a.) iostream
  b.) ios
  c.) istream or ostream
  d.) no one is required

                              ios
  INPUT                                                  OUTPUT
                       POINTER

          istream          streambuf         ostream


                           iostream


Istream_withassign    Iostream_withassign     Ostream_withassign
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 ??
What is the default precision value ??


 a.) 0                                   b.) 4




c.) 6                                    d.) 3
Managing console

Managing console

  • 1.
    Submitted by:- Namita Pandey 2011BTechece020 Shiva Johari
  • 2.
    Introduction  Streams & Stream Classes  Unformatted Input Output Operations  Formatted Console Input Output Operation  Formatting Flags, Bit fields and setf()  Designing Our Own Manipulators
  • 3.
    Managing Console I/O Operations  INPUT & OUTPUT  C++ supports a rich set of I/O operations  C++ uses the concept of stream & stream classes
  • 4.
    o A sequence of bytes o An interface between program and device INPUT EXTRACTION STREAM FROM INPUT INPUT STREAM DEVICE PROGRAM OUTPUT DEVICE INSERTION OUTPUT INTO OUTPUT STREAM STREAM
  • 5.
     C++ containsa hierarchy of classes that are used to define various streams ios INPUT OUTPUT POINTER istream streambuf ostream iostream Istream_withassign Iostream_withassign Ostream_withassign
  • 6.
    Overloaded operators >> and <<  get() and put() functions  getline() and write() functions
  • 7.
    C++ supports anumber of features which can be used for formatting the output. These features include :-  ios class functions  Manipulators  User-defined Manipulators
  • 8.
    FUNCTION TASK  Width()  To specify the required field size for displaying the output value  Precision()  To specify the digits to be displayed after 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
  • 9.
    MANIPULATORS EQUIVALENT IOS FUNCTION  setw()  width()  setprecision()  precision()  setfill()  fill()  setiosflags()  setf()  resetiosflags()  unset()
  • 10.
    cout.width(5); 5 4 3 1 2 cout<<543<<12<<“n”; cout.width(5);` cout<<543; cout.width(5); 5 4 3 1 2 cout<<12<<“n”;
  • 11.
    #include<iostream.h> cout.width(8); cout << cost[i]; int main() { int value = items[i] * int item[4] = cost[i]; {10,8,12,15}; cout.width(15); int cost[4] = cout << value <<“n”; {75,100,60,99}; cout.width(5); sum =sum + value; cout << “ITEMS”; } cout.width(8); cout << “n Grand cout << “COST”; Total = “; cout.width(15); cout << value<<“n”; cout.width(2); sum =sum + value; cout << sum <<“n”; int sum = 0; for(int i=0; i<4;i++) return 0; { cout.width(5); } cout << items[i];
  • 12.
    ITEMS COST TOTAL VALUE 10 15 150 8 100 800 12 60 720 15 99 1485
  • 13.
    cout.precision(3); 1.141 cout<<sqrt(2)<<“n”; 3.142 cout<<3.14159<<“n”; cout<<2.50032<<“n”; 2.5
  • 14.
    #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”; } }
  • 15.
    Value of pi: 3.1 3.14 3.143 3.1429 3.14286 3.142857 3.1428571 3.14285707 3.142857075 3.1428570747
  • 16.
  • 17.
    #include<iostream.h> cout<<“n Paddling Changednn”; #include<conio.h> cout.fill(‘#’); void main() cout.width(15); { cout<<12.345678<<“n”; cout.fill(‘<‘); return 0; 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(‘>’); }
  • 18.
  • 19.
    arg1 - formattingflags defined in the class ios arg2 - it specifies the group to which the formatting flags belongs
  • 20.
    FORMAT REQUIRED FLAG(ARG1) BIT-FIELD (ARG2) Left-justified output ios::left ios::adjustfield Right-justified output ios::right ios::adjustfield Padding after sign or base ios::internal ios::adjustfield Indicator (like +##20) Scientific Notation ios::scientific ios::floatfield Fixed Point notation ios::fixed ios::floatfield Decimal Base ios::dec ios::basefield
  • 21.
    #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(); }
  • 22.
  • 23.
    #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”; }
  • 24.
    Enter an integervalue : 92 The hexadecimal, octal and decimal representation of 92 is: 5c, 134 and 92 respectively.
  • 25.
    ostream & manipulator(ostream & output) { …………… …………… (code) …………… return output; }
  • 27.
     We havetaken 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
  • 30.
    The function ofistream 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
  • 31.
    The function ofstreambuf 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
  • 32.
    A stream isa sequence of ___________. a) Bytes b) Files c) Manipulators d) None of the above
  • 33.
    Which are themember functions of ios class : a) precision() b) width() c) fill() d) All the above
  • 34.
    What will bethe 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”; -******1.235e+01 (.A B.) -*****1.235e+01 -*****.1235e+02 -*********1.236 (.C D.)
  • 35.
    a) The __________ operator is overloaded in the istream class a) Insertion b) >> c) << d) None of the above
  • 36.
    Which Class isneeded to be virtual in this case : a.) iostream b.) ios c.) istream or ostream d.) no one is required ios INPUT OUTPUT POINTER istream streambuf ostream iostream Istream_withassign Iostream_withassign Ostream_withassign
  • 37.
    Q8. The header fileiomanip can be used in place of iostream ?? Q9. programmer can’t define a manipulator that could represent a set of formatted functions ??
  • 38.
    What is thedefault precision value ?? a.) 0 b.) 4 c.) 6 d.) 3