top of page

We can format and manipulate the input output streams using IO manipulators. We can display blank spaces between fields , we can display numbers in hexadecimal or octal, we can even display boolean values as true and false instead of 1 and 0.

Here are the IO manipulators defined in <iostream>

  • boolalpha  noboolalpha   - display boolean values as true and false

  • showbase noshowbase - add prefix of 0x and 0 to hexadecimal and octal values

  • showpoint noshowpoint - display decimal point always for floating point numbers

  • showpos noshowpos - display + sign for non-negative numbers

  • skipws noskipws - skip whitespace for input using cin stream

  • uppercase nouppercase - display uppercase characters with hex or boolean values

  • unitbuf nounitbuf - flush the buffer after each operation or not

  • hex dec oct - display the integers using hexadecimal or decimal or octal

  • left right internal - set placement for fill characters

  • fixed scientific - display the floating value as fixed point or scientific notation

  • ws - used with input stream to consume white spaces

  • ends - add end of stream ('\0') to the output stream and flush it

  • endl - add end of line character to the output stream and flush

  • flush - flush the output stream

The next set of manipulators are defined in <iomanip>

  • setbase - sets the base for integers - can be 10,16 or 8

  • setfill - sets the fill charcter. Default is " "

  • setprecision - sets the precision for floating point values

  • setiosflags - set multiple ios flags

  • resetiosflags - reset ios flags

  • setw - set the width for the next input/ouput field.

Let us look at some examples
 
       int a=20;
       cout<<hex<<a<<endl;//output is 14
       a = 10;
       cout<<hex<<uppercase<<a<<endl;//output is A
       out<<oct<<a<<endl;//output is 12
       cout<<hex<<showbase<<a<<endl;//output is 0xa

       float b=12.0;
       cout<<b<<endl;//output is 12
       cout<<showpoint<<b<<endl;//output is 12.0
       
       b = 12.346789;
       cout<<setprecision(5)<<b<<endl;//output is 12.347
   
       cout<<scientific<<b<<endl;//output is 1.23468e+1
       cout<<showpos<<a;//output is +10

 


get(), put() and getline() functions

 

istream class has two more functions to read other than extraction operator. These are get() and getline().

get() function can be used to read one character from input stream.
    
char ch;
     cin.get(ch);


getline(char *,int len, [char delimiter]) can be used to read a string which contains whitespaces. Extraction operator terminates reading when it encounters whitespace. If there is a need to read complete line, we can use readline() function.

   
     char arr[40];
       cin.getline(arr,40);//read a string terminated by newline character
       char arr2[200];
       cin.getline(arr2,200,'#');//reads a string terminated by # character


Let us look at another program which reads characters from keyboard and display them on screen until # is pressed
 
  char ch;
  do  {
    cin.get(ch);
     cout.put(ch);
  }
  while(ch!='#');

 


 

bottom of page