Manipulators are special stream functions that are used to change certain characteristics of the input and output. Manipulators are used to formatting the data that are displaying on screen. Include
header file for using these manipulators in your C++ program.
The following are the important manipulator used in a C++.
1. endl: The endl is an output manipulator that is used to generate a carriage return. It works like “\n”
2. Setbase(): The setbase() manipulator is used to convert the base of one numeric value into another base.
3. Setw (): It is used to specify the minimum number of character positions, a variable will consume on the command prompt. The general syntax of the setw manipulator function is setw (int width)
4. Setfill(): It is used to specify a different character to fill the unused space.
5. setprecision(): It is used with floating point numbers. You can set the number of digits printed to the right of the decimal point.
Example:
#include<iostream.h>
#include<iomanip.h>
void main (void)
{
int value;
float a,b,c;
a = 350;
b = 150;
c = a/b;
cout <<"Enter decimal number for hexadecimal and octal conversion"<< endl;
cin >> value;
cout <<"Hexadecimal base ="<< hex << value << endl;
cout <<"Octal base ="<< oct << value << endl;
cout << setfill ('*');
cout << setw (5) << a << setw (5) << b << endl;
cout << setw (6) << a << setw (6) << b << endl;
cout <<"Use of setprecision"<<endl;
cout <<fixed<< setprecision (2) << c << endl;
}
Output:
Enter decimal number for hexadecimal and octal conversion.
50
Hexadecimal base = 32
Octal base = 62
**350**150
***350***150
Use of setprecision
2.33A company has following details of their employees in the file ‘emp.dat”.
a) Emp ID
b) Emp Name
c) Emp Address
d) EmpDept (Admin/Sales/Production/IT)
e) Emp phone
f) Emp Age
Write a program to read the above file. Create new files such as Adm.dat, Sal.dat, Pro.dat, IT.dat respectively, to store the employee details according to their department.
Solution:
In D drive create a folder, named as Database. When you execute this program, and insert record, file will be created automatically in database folder.
charempfile[30] = "D:\\Database\\Employee.txt";
charITfile[20] = "D:\\Database\\IT.txt";
charAdminfile[25] = "D:\\Database\\Admin.txt";
charProdfile[30] = "D:\\Database\\Production.txt";
charSalesfile[30] = "D:\\Database\\Sales.txt";
classemp
{
int empid;
char name[30];
char address[60];
int age;
public:
chardept[15];
void get();
char *getdept()
{
returndept;
}
};
voidemp::get()
{
cout<<"\nENTER EMPLOYEE ID:"<<endl;
cin>>empid;
cout<<"\nENTER NAME:"<<endl;
cin>>name;
cout<<"\nENTER ADDRESS:"<<endl;
cin>>address;
cout<<"\nENTER DEPARTMENT:(ADMIN/SALES/IT/PRODUCTION):"<<endl;
cin>>dept;
cout<<"\nENTER AGE:"<<endl;
cin>>age;
}
void insert()
{
emp e;
ofstreamfout; //ofstream is a class, fout is its object. It can be used ONLY to write onto the file
fout.open(empfile,ios::out | ios::binary | ios::app|ios::_Nocreate); //file is open in the binary, append and nocreate mode.
clrscr();
if (fout.fail())
{
cout<<"n UNABLE TO OPEN THE FILE!"<<endl;
goto err;
}
e.get(); // accepting the details from the end user
fout.write((char *)&e,sizeof(e)); //writing onto the file with fout object
if(fout.tellp()%sizeof(e)==0)
{
cout<<"\nRECORD INSERTED!"<<endl;
}
else
{
cout<<"\nINSERTION FAILED!"<<endl;
goto err;
}
err:
fout.close();
}
void sort() // This function will insert the record according to department in respective file.
{
emp e;
ofstreamadm,sal,pro,it; //all files have been created for writing mode
ifstream fin; // fin object belongs to the ifstream class, it is used to read the file contents only
adm.open(Adminfile, ios::out | ios::binary | ios::app);
sal.open(Salesfile,ios::out | ios::binary | ios::app);
pro.open(Prodfile,ios::out | ios::binary | ios::app);
it.open(ITfile,ios::out | ios::binary | ios::app);
fin.open(empfile,ios::in | ios::binary);
while(fin.read((char *)&e,sizeof(e))) //reading the file contents till it reaches end of file
{
if(stricmp(e.getdept(),"Admin")==0)
{
adm.write((char *)&e,sizeof(e));
cout<<"RECORD INSERTED INTO ADMIN FILE!"<<endl;
}
elseif(stricmp(e.getdept(),"Sales")==0)
{
sal.write((char *)&e,sizeof(e));
cout<<"RECORD INSERTED INTO SALES FILE!"<<endl;
}
elseif(stricmp(e.getdept(),"IT")==0)
{
it.write((char *)&e,sizeof(e));
cout<<"RECORD INSERTED INTO IT FILE!"<<endl;
}
elseif(stricmp(e.getdept(),"Production")==0)
{
pro.write((char *)&e,sizeof(e));
cout<<"RECORD INSERTED INTO PRODUCTION FILE!"<<endl;
}
else
cout<<"\n INSERT CORRECTLY!"<<endl;
}
fin.close();
adm.close();
sal.close();
it.close();
pro.close();
}
void main()
{
int n;
clrscr();
cout<<"How many records you want to insert"<<endl;
cin>>n;
for(int i=0; i<n; i++)
{
insert();
}
sort();
getch();
}