C++ uses the concept of stream and stream classes to implement I/O operations with console and disk files
The input functions are defined in istream class
The output functions are defined in ostream class.
The IOstream class inherits the properties of istream and ostream classes.
Stream is a class which provides set of functions to perform input and output operations
Stream represents input source and output destination
Output stream class provides output functions
Member function of ostream class, cout.put(ch);// displays the value of variable ch.
Member function of ostream class, write() function displays the entire line cout. write(linesize); Line represents the name of the string to be displayed, size indicates the no.of characters to display. It doesn’t stop displaying the characters when null character is encountered. Example of Output Stream in C++ # include<iostream.h> void main() { cout.put(‘A’); // we can use only one character cout<<’A’; cout<<10; cout<<1.5; cout.write(“Hello C++”,q); cout.write(“Hello c++”,b); cout<<”Hello C++”; }
Width() to specify the required field size for displaying an output. cout.width(w); The output will be printed in the field of w characters wide at the right end of the field.
int a=10;(2 width) printf(“%4d”,a); //(2 for space)
By default, the floating numbers are printed with six digits after the point, We can specify the number of digits to be displayed after the decimal point. cout.precision(d); sets d number of digits to the right of decimal pair.
# include<iostream.h> void main() { int a=10; cout.width(5); cout<<a<<endl; float b=1.234; cout.precission(2); cout<<b<<endl; } o/p : - - - 10 1.23 Note : Numbers are right aligned.
The unused position of the field are filled with white spaces, by default we can fill them by the designs character by using fill() function cout.fill(ch); ch is used to fill the unused positions { int a=10; cout.width(5); cout.fill(‘*’); cout<<a<<endl; }
cout .setf(ios::show point) displays tralling zeros cout.setf (ios:: show pos) displays plus sign before a positive number
# include <iostream.h> # include<conio.h> void main(){ flaot a=10; cout.setf(ios::showpoint); cout<<a<<endl; int b=20; cout.setf(ios:: showpos); cout<<b<<endl; } O/p: 10.000000 -20
This class provide member functions to performs input operations.
Member function of istream class. Fetches a single character including a blank space,tab and new line characters, get() can be used in 2 ways; cin .get(ch);//get a character from keyboard and assigns it to ch. ch=cin.get(); also can be used
Member function of istream class. We can read the text …. oriented getline() cin.getline(line,size); Read character input into the variable line The reading is terminated as soon as either ‘\n’ is encountered of size characters are read.
# include <iostream.h> # include <conio.h> void main() { char anem[10]; clrscr(); cout<<”\n Input name’: cin .get line(name,10); cout<<name; }
# include <iostream.h> #include <conio.h> void main() { char ch; clrscr(); cout<<”\n Input any character including space”; ch=cin.get(); cout<<endl<<ch; }
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.