ADDITIONAL PROGRAMS:
//Sorting using Vector - STL
#include <conio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int number;
vector <int>vi;
for(int i=0;i<10;i++)
{
cout<<"enter the value["<<i<<"]";
cin>>number;
vi.push_back(number);
}
vector <int>::iterator index;
cout<<"sort in ascending order"<<endl;
sort(vi.begin(),vi.end());
for(index=vi.begin();index<vi.end();index++)
{
cout<<*index<<"\t";
}
cout<<"sort in descending order"<<endl;
sort(vi.rbegin(),vi.rend());
for(index=vi.begin();index<vi.end();index++)
{
cout<<*index<<"\t";
}
getch();
}
/*OUTPUT:
enter the value[0]2
enter the value[1]4
enter the value[2]6
enter the value[3]8
enter the value[4]10
enter the value[5]1
enter the value[6]3
enter the value[7]5
enter the value[8]7
enter the value[9]9
sort in ascending order
1 2 3 4 5 6 7 8 9 10
sort in descending order
10 9 8 7 6 5 4 3 2 1
*/

//Vector sorting using Predicates
#include <algorithm>
#include <vector>
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
vector <int> vi;
vi.push_back(100);
vi.push_back(200);
vi.push_back(300);
sort(vi.begin(),vi.end(),greater<int>());
cout<<vi[0]<<"\t"<<vi[1]<<"\t"<<vi[2]<<endl;
sort(vi.begin(),vi.end(),less<int>());
cout<<vi[0]<<"\t"<<vi[1]<<"\t"<<vi[2]<<endl;

//vector for handling character datatype
vector <char> vc;
/*for(char i='a';i<='z';i=i++)
{
vc.push_back(i);
} */
vc.push_back('a');
vc.push_back('z');
vc.push_back('c');
sort(vc.begin(),vc.end(),greater<char>());
cout<<vc[0]<<"\t"<<vc[1]<<"\t"<<vc[2]<<endl;
sort(vc.begin(),vc.end(),less<char>());
cout<<vc[0]<<"\t"<<vc[1]<<"\t"<<vc[2]<<endl;
getch();
}
/*output
300 200 100
100 200 300
z c a
a c z
*/
Aim:
Write a c++ program to get the students details from the user. Add details, view details, search and modify details and delete the records. Use file streams for processing the data’s.
/////////////////Header Files
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h> //for exit (0) function
/////////////a class declaration to get and show data
class student
{
int studentid;
char name[20];
char fname[20];
public:
void getdata()
{
cout<<"\nEnter The admission no.:\t ";
cin>>studentid;
cout<<"\n\nEnter The Name of The Student:\t ";
gets(name);
cout<<"\n\nEnter Fathe's Name of the Student:\t";
gets(fname);
}
void showdata()
{
cout<<"\nAdmission no. :\t"<<studentid;
cout<<"\nStudent Name :\t ";
puts(name);
cout<<"\nFather's Name :\t";
puts(fname);
}
int searchkey()
{
return studentid;
}
};

//function to write the data of above program in a binary file
void writedata()
{
student obj;
ofstream writefile;
cout<<"==== ADD RECORD =====\n\n";
writefile.open("student.dat",ios::binary|ios::app);
obj.getdata();
writefile.write((char*)&obj,sizeof(obj));
writefile.close();
}

/////////////////////function to display records of file
void displaydata()
{
student obj;
ifstream disp;
cout<<"======== DISPLAY RECORD ======\n\n";
disp.open("student.dat",ios::binary);
while(disp.read((char*)&obj,sizeof(obj)))
{
obj.showdata();
}
disp.close();
}
//Function to search and display the contents from binary file
void search (int n)
{
student obj;
ifstream sear;
cout<<"========== SEARCH RECORD =======\n\n";
sear.open("student.dat",ios::binary);
while(sear.read((char*)&obj,sizeof(obj)))
{
if(obj.searchkey()==n)
obj.showdata();
else
cout<<"check the admission number";
}
sear.close();
}
//////////Function to delete a record
void delrecord(int n)
{
student obj;
ifstream delr;
cout<<"========== DELETEE RECORD =======\n\n";
delr.open("student.dat",ios::binary);
ofstream newfile;
newfile.open("Temp.dat",ios::out|ios::binary);
while(delr.read((char*)&obj,sizeof(obj)))
{
if(obj. searchkey()!=n)
newfile.write((char*)&obj,sizeof(obj));
}
delr.close();
newfile.close();
remove("student.dat");
rename("Temp.dat","student.dat");
}
/////////////////////Function to modify a record
void modirecord(int n)
{
fstream modif;
student obj;
cout<<"========== MODIFY RECORD =======\n\n";
int found=0;
modif.open("student.dat",ios::in|ios::out);
while(modif.read((char*)&obj,sizeof(obj)) && found==0)
{
if(obj. searchkey()==n)
{
obj.showdata();
cout<<"\nEnter The New Details of student";
obj.getdata();
int pos=-1*sizeof(obj);
modif.seekp(pos,ios::cur);
modif.write((char*)&obj,sizeof(obj));
found=1;
}
}
modif.close();
}


int main()
{
int t;
student obj;
gt: //goto destination declaration
clrscr();
cout<<"===== TASK =====";
cout<<"\n\n1. Add student Details ";
cout<<"\n\n2. View Student Details";
cout<<"\n\n3. Search Record";
cout<<"\n\n4. Modify Record";
cout<<"\n\n5. Delete Record";
cout<<"\n\n6. Exit";
cout<<"\n\nChoose your task...\t";
cin>>t;
if(t==1)
{
clrscr();
writedata();
cout<<"\n\npress any key to goto task window...";
getch();
goto gt;
}
else if(t==2)
{
clrscr();
displaydata();
cout<<"\n\npress any key to goto task window...";
getch();

goto gt;
}
else if(t==3)
{
clrscr();
int id;
cout<<"\nEnter Student id:\t";
cin>>id;
search(id);
cout<<"\n\npress any key to goto taks menu....";
getch();
goto gt;
}
else if(t==4)
{
clrscr();
int mr;
cout<<"\nEnter Student id:\t";
cin>>mr;
modirecord(mr);
cout<<"\n\npress any key to goto taks menu....";
getch();
goto gt;
}
else if(t==5)
{
int dr;
cout<<"\nEnter Student id:\t";
cin>>dr;
delrecord(dr);
cout<<"\n\npress any key to goto taks menu....";
getch();
goto gt;
}
else if(t==6)
{
exit(0);
}
else
{
cout<<"\n\ninvalid choice! please type carefully...";
goto gt;
}

return 0;
}
Output:

j