Input Output operation in C++

In C++, Input Output operations are performed using two objects -  cin and cout. Note that they are not functions but objects. They use overloaded operators >> and <<. c here indicates console.

cin
cin  is an istream object.
Any  variable like int, float, double or char array is read from console using cin object and  extraction operator(>>). cin can also read user defined objects if their classes overload extraction operator.

e.g.

#include<iostream>
using namespace std;
int main() 
{ 
 int a; float b; int  c; 
 cin>>a; 
 cin>>b>>c;

}
Unlike scanf which has a complex syntax, cin does not use ampersand operator ( & ) and it does not need format specifiers like %d, %f.. To read multiple variables, >> operator is chained, comma is not used.

cout
cout is an object of ostream class and values can be written on console using cout and insertion operator(<<). cout also does not need format specifiers.

e.g.
cout<<"Hello world";
cout<<a<<b<<c;

endl
endl is an io manipulator. It prints the character \n and flushes the stream.

Other IO manipulators
C++ has a large set of io manipulators. Some of the main ones are
  • setw(n) and width(n) - set the width for subsequent fields.
  • left and right - justify the field to left or right. Only work after setw() or width()
  • setfill(ch) - if the field does not fill the width, ch is used to fill empty spaces
  • setprecision(n) - sets the number of digits printed after decimal point for floating point values
  • boolalpha - shows the boolean values as true and false instead of 1 and 0
  • noboolalpha - shows boolean values as 1 and 0
  • skipws and noskipws - by default white spaces are skipped when reading character input. noskipws will not skip white spaces.
  • hex, oct, dec - displays integers in the given numer system
  • setbase(8/10/6) - sets the base for number. Similar to hex, oct and dec


int n = 3;
cout<<"|"<<setw(10)<<n<<endl;
int m = 88;
cout<<hex<<m;//prints 58
bool b = true;
cout<<boolalpha<<b;//prints true
char str[30];
cin>>str;
cout<<"you typed "<<str;//prints only first word of input
cin.ignore(INT_MAX,'\n');
cin.getline(str,29);
cout<<"you typed "<<str;//prints the entire string

By default cin with strings reads only till first white space and next cin will read next word etc. So if you type "Hello world", your output will be "you typed Hello". To read the complete line of input, you can use cin.getline(char_array,size)

Another problem is after reading first word, the buffer has the rest of the string, So subsequent reads with cin will not wait for user input, instead take the value from buffer. Buffer can be flushed using cin.ignore(how_many,till_char);

If a wrong input is entered, an error flag is set which may cause infinite loop

 while(m!=-1)
 {
     cin>>m;    
     cout<<m;
  }

 Here if a character is entered instead of a number, infinite loop is caused. To avoid it, clear the error flag and ignore the offending character.

 while(m!=-1)
 {
     cin>>m;
     if(cin.fail())
    {
         cin.clear();
         cin.ignore(INT_MAX,'\n');
     }
    else{
        cout<<m;
     }
 }
NameSpaces

C++ organizes names into different namespaces in order to avoid name conflicts. All library functions and objects belong to the namespace "std". So to use cin and cout, we need to import this namespace. This is done using

using namespace std;

Or you can use the fully qualified names viz std::cin, std::cout.
::
is called scope resolution operator.

std::cout<<"Hello world";
 
bool
C++ has an additional data type bool. bool stores boolean values true and false. But for i/o operations true is treated as 1 and false is treated as 0.

bool m;
m=true;
cout<<m;//prints 1 here. 



Comments

Popular Posts