Put():-
a member of ostream class can be used to print a character.
Get():-
a member of istream class can be used to accept a character. Character I/O with get() and put()
#include <iostream.h>
#include <conio.h>
void main()
{
char c;
clrscr();
cout<<"INPUT TEXT \n";
cin.get(c);
cout.put(c);
getch();
}
getline() and write() functions:-
The getline() function
It reads a whole line of text that ends with a newline character.This function can be invokedby using the object cin as follows:
Syntax:-
cin.getline(line,size);
Reading Strings With getline()
#include <iostream.h>
#include <conio.h>
void main()
{
char nm[20];
clrscr();
cout<<"Enter your name";
cin.getline(nm,20);
cout<<"Name is "<<nm;
getch();
}
The write() function
It displays an entire line and has the following form:
cout.write(line,size);
The first argument line represents the name of the string to be displayed and the second argument size indicates the number of characters automatically when the null character is encountered. If the size is greater than the length of line, then it displays beyond the bound of line.
Displaying String With write()
#include<iostream.h>
#include<conio.h>
void main()
{
char nm[20];
clrscr();
cout<<"Enter your name";
cin.getline(nm,20);
cout<<"Name is ";
cout.write(nm,7);
getch();
}
--ios class function and flags.
--manipulators.
--User-defined output functions.
The ios class contains a large number of member functions that would help us to format the output in a number of ways. The most importany ones among them are listed in table
ios format functions
ios class function and flags.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.setf(ios::left,ios::adjustfield);
cout.width(10);
cout<<"EMP NO";
cout.fill('*');
cout.width(10);
cout<<"EMP NAME";
cout.fill(' ');
cout.width(10);
cout<<"EMP SALARY";
cout<<endl;
cout.unsetf(ios::left);
cout.width(10);
cout<<100;
cout.fill('*');
cout.width(10);
cout<<"Rama";
cout.fill(' ');
cout.width(10);
cout.precision(2);
cout.setf(ios::scientific,ios::floatfield);
cout<<156.2323;
cout.width(10);
cout<<endl;
cout<<101;
cout.fill('*');
cout.width(10);
cout<<"Uma";
cout.fill(' ');
cout.width(10);
cout.precision(2);
cout.unsetf(ios::scientific);
cout<<156.2323;
getch();
}
Manipulators
These are special functions that can be included in the I/O statements to alter the format parameter of stream. To access these manipulators, the file iomanip should be included in the program.
Manipulators
Defining Field Width:width()
The width() function is used to define the width of a field necessary for the output of an item.As it is a member function object is required to invoke it like
Syntax:-
cout.width(w);
Setting Precision: precision():-
By default ,the floating numbers are printed with six digits after the decimal points. However ,we can specify the number of digits to be displayed after the decimal point while printing the floating point numbers.
This can be done by using the precision () member function as follows:
Syntax:-
cout.precision(d);
where d is the number of digits to the right of decimal point.for example the statements.
FILLING AND PADDING :fill()
The unused portion of field width are filled with white spaces, by default. The fill() function can be used to fill the unused positions by any desired character.It is used in the following form:
cout.fill(ch);
Where ch represents the character which is used for filling the unused positions.
FORMATTING FLAGS,Bit Fields and setf():-
The setf() a member function of the ios class, can provide answers left justified.The setf() function can be used as follows:
Syntax:-
cout.setf(arg1.arg2) ;
The arg1 is one of the formatting flags defined in the class ios.The formatting flag specifies the format action required for the output.Another ios constant,arg2,known as bit field specifies the group to which the formatting flag belongs.
for example:
cout.setf(ios::left,ios::adjustfield);
cout.setf(ios::scientific,ios::floatfield);
manipulators example
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
cout<<setiosflags(ios::left);
cout<<setw(10);
cout<<"EMP NO";
cout<<setfill('*');
cout<<setw(10);
cout<<"EMP NAME";
cout<<setfill(' ');
cout<<setw(10);
cout<<"EMP SALARY";
cout<<endl;
cout<<resetiosflags(ios::left);
cout<<setw(10);
cout<<100;
cout<<setfill('*');
cout<<setw(10);
cout<<"Rama";
cout<<setfill(' ');
cout<<setw(10);
cout<<setprecision(2);
cout<<setiosflags(ios::scientific);
cout<<156.2323;
cout<<setw(10);
cout<<endl;
cout<<101;
cout<<setfill('*');
cout<<setw(10);
cout<<"Uma";
cout<<setfill(' ');
cout<<setw(10);
cout<<setprecision(2);
cout<<resetiosflags(ios::scientific);
cout<<156.2323;
getch();
}
ostream & manipulator (ostream & output)
{
……………
…………….(code)
……………..
return output;
}
User defined manipulators
#include <iostream.h>
#include <iomanip.h>
ostream ¤cy (ostream & output)
{
output<< “Rs”;
return output;
}
ostream & form (ostream &output)
{
output.set(ios::showpos);
output.setf(ios::showpoint);
output.fill(‘*’);
output.precision(2);
output<<setiosflags(ios::fixed)<<setw(10);
return output;
}
void main()
{
cout<<currency<<form<<7864.5;
}
Enumeration
An enumeration is a user-defined type whose value is restricted to one of several explicitly named constants(enumerators). Enumeration are defined using keyword: enum.
Enumeration example
#include <iostream.h>
enum seasons
{
spring,
summer,
autumn,
winter
};
void main()
{
seasons s;
s = spring;
cout << "spring = " << s << endl;
s = summer;
cout << "summer = " << s << endl;
s = autumn;
cout << "autumn = " << s << endl;
s = winter;
cout << "winter = " << s<< endl;
}
a member of ostream class can be used to print a character.
Get():-
a member of istream class can be used to accept a character. Character I/O with get() and put()
#include <iostream.h>
#include <conio.h>
void main()
{
char c;
clrscr();
cout<<"INPUT TEXT \n";
cin.get(c);
cout.put(c);
getch();
}
getline() and write() functions:-
The getline() function
It reads a whole line of text that ends with a newline character.This function can be invokedby using the object cin as follows:
Syntax:-
cin.getline(line,size);
Reading Strings With getline()
#include <iostream.h>
#include <conio.h>
void main()
{
char nm[20];
clrscr();
cout<<"Enter your name";
cin.getline(nm,20);
cout<<"Name is "<<nm;
getch();
}
The write() function
It displays an entire line and has the following form:
cout.write(line,size);
The first argument line represents the name of the string to be displayed and the second argument size indicates the number of characters automatically when the null character is encountered. If the size is greater than the length of line, then it displays beyond the bound of line.
Displaying String With write()
#include<iostream.h>
#include<conio.h>
void main()
{
char nm[20];
clrscr();
cout<<"Enter your name";
cin.getline(nm,20);
cout<<"Name is ";
cout.write(nm,7);
getch();
}
Formatted Console I/O Operations
C++ supports a number of features that could be used for formatting the output.These features include:--ios class function and flags.
--manipulators.
--User-defined output functions.
The ios class contains a large number of member functions that would help us to format the output in a number of ways. The most importany ones among them are listed in table
ios format functions
Function | Task |
---|---|
Width() | To specify the required field size for displaying an output value |
Precision() | To specify the number of digits to be displayed after the decimal point of 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(such as left-justification and right-justification) |
Unsetf() | To clear the flags specified |
ios class function and flags.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.setf(ios::left,ios::adjustfield);
cout.width(10);
cout<<"EMP NO";
cout.fill('*');
cout.width(10);
cout<<"EMP NAME";
cout.fill(' ');
cout.width(10);
cout<<"EMP SALARY";
cout<<endl;
cout.unsetf(ios::left);
cout.width(10);
cout<<100;
cout.fill('*');
cout.width(10);
cout<<"Rama";
cout.fill(' ');
cout.width(10);
cout.precision(2);
cout.setf(ios::scientific,ios::floatfield);
cout<<156.2323;
cout.width(10);
cout<<endl;
cout<<101;
cout.fill('*');
cout.width(10);
cout<<"Uma";
cout.fill(' ');
cout.width(10);
cout.precision(2);
cout.unsetf(ios::scientific);
cout<<156.2323;
getch();
}
Manipulators
These are special functions that can be included in the I/O statements to alter the format parameter of stream. To access these manipulators, the file iomanip should be included in the program.
Manipulators
Manipuators | Equivalent ios function |
---|---|
setw() | width() |
setprecision() | precision() |
setfill() | fill() |
setiosflags() | setf() |
resetiosflags() | unsetf() |
Defining Field Width:width()
The width() function is used to define the width of a field necessary for the output of an item.As it is a member function object is required to invoke it like
Syntax:-
cout.width(w);
Setting Precision: precision():-
By default ,the floating numbers are printed with six digits after the decimal points. However ,we can specify the number of digits to be displayed after the decimal point while printing the floating point numbers.
This can be done by using the precision () member function as follows:
Syntax:-
cout.precision(d);
where d is the number of digits to the right of decimal point.for example the statements.
FILLING AND PADDING :fill()
The unused portion of field width are filled with white spaces, by default. The fill() function can be used to fill the unused positions by any desired character.It is used in the following form:
cout.fill(ch);
Where ch represents the character which is used for filling the unused positions.
FORMATTING FLAGS,Bit Fields and setf():-
The setf() a member function of the ios class, can provide answers left justified.The setf() function can be used as follows:
Syntax:-
cout.setf(arg1.arg2) ;
The arg1 is one of the formatting flags defined in the class ios.The formatting flag specifies the format action required for the output.Another ios constant,arg2,known as bit field specifies the group to which the formatting flag belongs.
for example:
cout.setf(ios::left,ios::adjustfield);
cout.setf(ios::scientific,ios::floatfield);
manipulators example
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
cout<<setiosflags(ios::left);
cout<<setw(10);
cout<<"EMP NO";
cout<<setfill('*');
cout<<setw(10);
cout<<"EMP NAME";
cout<<setfill(' ');
cout<<setw(10);
cout<<"EMP SALARY";
cout<<endl;
cout<<resetiosflags(ios::left);
cout<<setw(10);
cout<<100;
cout<<setfill('*');
cout<<setw(10);
cout<<"Rama";
cout<<setfill(' ');
cout<<setw(10);
cout<<setprecision(2);
cout<<setiosflags(ios::scientific);
cout<<156.2323;
cout<<setw(10);
cout<<endl;
cout<<101;
cout<<setfill('*');
cout<<setw(10);
cout<<"Uma";
cout<<setfill(' ');
cout<<setw(10);
cout<<setprecision(2);
cout<<resetiosflags(ios::scientific);
cout<<156.2323;
getch();
}
Designing our own manipulators:-
The general form for creating a manipulator without any argument isostream & manipulator (ostream & output)
{
……………
…………….(code)
……………..
return output;
}
User defined manipulators
#include <iostream.h>
#include <iomanip.h>
ostream ¤cy (ostream & output)
{
output<< “Rs”;
return output;
}
ostream & form (ostream &output)
{
output.set(ios::showpos);
output.setf(ios::showpoint);
output.fill(‘*’);
output.precision(2);
output<<setiosflags(ios::fixed)<<setw(10);
return output;
}
void main()
{
cout<<currency<<form<<7864.5;
}
Enumeration
An enumeration is a user-defined type whose value is restricted to one of several explicitly named constants(enumerators). Enumeration are defined using keyword: enum.
Enumeration example
#include <iostream.h>
enum seasons
{
spring,
summer,
autumn,
winter
};
void main()
{
seasons s;
s = spring;
cout << "spring = " << s << endl;
s = summer;
cout << "summer = " << s << endl;
s = autumn;
cout << "autumn = " << s << endl;
s = winter;
cout << "winter = " << s<< endl;
}