//Menu driven program
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class student{
int roll;
char name[20];
float marks;
public:
void getdata();
void display();
int retroll()
{
return roll;
}
void update(float m)
{
marks=m;
}
};
void student::getdata()
{
cout<<"\nRoll no : ";
cin>>roll;
cout<<"\nName : ";
gets(name);
cout<<"\nMarks : ";
cin>>marks;
}
void student::display()
{
cout<<"\nRoll no : "<<roll<<"\nName : "<<name<<"\nMarks : "<<marks<<endl;
}
void create()
{
fstream file;
file.open("stud.dat",ios::app|ios::binary);
student s;
char c;
do
{
s.getdata();
file.write((char*)&s,sizeof(s));
cout<<"\nWish to enter more (y/n)? : ";
cin>>c;
} while(c=='y'||c=='Y');
file.close();
}
void display_data()
{
fstream file;
file.open("stud.dat",ios::in | ios::binary);
student s;
while(file.read((char*)&s,sizeof(s)))
s.display();
file.close();
}
void Delete()
{
int grn;
cout<<"Enter roll no to be deleted : ";
cin>>grn;
fstream file,temp;
student s;
file.open("stud.dat",ios::in|ios::binary);
temp.open("temp.dat",ios::out|ios::binary);
while(file.read((char*)&s,sizeof(s)))
{
if(s.retroll()!=grn)
temp.write((char*)&s,sizeof(s));
}
file.close();
temp.close();
remove("stud.dat");
rename("temp.dat","stud.dat");
}
void modify()
{
int grn,found=0;
float nmarks;
cout<<"\nEnter roll number : ";
cin>>grn;
fstream file;
student s;
file.open("stud.dat",ios::in|ios::binary|ios::out);
while(file.read((char*)&s,sizeof(s)))
{
if(s.retroll()==grn)
{
found=1;
cout<<"\nEnter new marks of student : ";
cin>>nmarks;
break;
}
}
if(found==0)
{
cout<<"\nRecord does not exist!!";
getch();
file.close();
return;
}
s.update(nmarks);
int p=file.tellg();
int t=p-sizeof(s);
file.seekg(t,ios::beg);
file.write((char*)&s,sizeof(s));
s.display();
file.close();
}
void menu()
{
cout<<"\t\tMAIN MENU\n\n"
<<"\tPress 1 : To add record\n"
<<"\tPress 2 : To display record\n"
<<"\tPress 3 : To delete record\n"
<<"\tPress 4 : To modify data\n"
<<"\tPress 5 : To exit\n";
}
void main()
{
int opt;
float nmarks;
do
{
clrscr();
menu();
cout<<"\n\tEnter your option : ";
cin>>opt;
switch(opt)
{
case 1:clrscr();
cout<<"\nFill following details :\n";
create();
break;
case 2:clrscr();
cout<<"\nDetails are :\n " ;
display_data();
getch();
break;
case 3:clrscr();
Delete();
cout<<"\nNew file is :\n";
display_data();
getch();
break;
case 4:clrscr();
modify();
getch();
break;
case 5:cout<<"\n\tExit!!";
break;
default:cout<<"\n\tInvalid option!!";
getch();
}
} while(opt!=5);
getch();
}
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class student{
int roll;
char name[20];
float marks;
public:
void getdata();
void display();
int retroll()
{
return roll;
}
void update(float m)
{
marks=m;
}
};
void student::getdata()
{
cout<<"\nRoll no : ";
cin>>roll;
cout<<"\nName : ";
gets(name);
cout<<"\nMarks : ";
cin>>marks;
}
void student::display()
{
cout<<"\nRoll no : "<<roll<<"\nName : "<<name<<"\nMarks : "<<marks<<endl;
}
void create()
{
fstream file;
file.open("stud.dat",ios::app|ios::binary);
student s;
char c;
do
{
s.getdata();
file.write((char*)&s,sizeof(s));
cout<<"\nWish to enter more (y/n)? : ";
cin>>c;
} while(c=='y'||c=='Y');
file.close();
}
void display_data()
{
fstream file;
file.open("stud.dat",ios::in | ios::binary);
student s;
while(file.read((char*)&s,sizeof(s)))
s.display();
file.close();
}
void Delete()
{
int grn;
cout<<"Enter roll no to be deleted : ";
cin>>grn;
fstream file,temp;
student s;
file.open("stud.dat",ios::in|ios::binary);
temp.open("temp.dat",ios::out|ios::binary);
while(file.read((char*)&s,sizeof(s)))
{
if(s.retroll()!=grn)
temp.write((char*)&s,sizeof(s));
}
file.close();
temp.close();
remove("stud.dat");
rename("temp.dat","stud.dat");
}
void modify()
{
int grn,found=0;
float nmarks;
cout<<"\nEnter roll number : ";
cin>>grn;
fstream file;
student s;
file.open("stud.dat",ios::in|ios::binary|ios::out);
while(file.read((char*)&s,sizeof(s)))
{
if(s.retroll()==grn)
{
found=1;
cout<<"\nEnter new marks of student : ";
cin>>nmarks;
break;
}
}
if(found==0)
{
cout<<"\nRecord does not exist!!";
getch();
file.close();
return;
}
s.update(nmarks);
int p=file.tellg();
int t=p-sizeof(s);
file.seekg(t,ios::beg);
file.write((char*)&s,sizeof(s));
s.display();
file.close();
}
void menu()
{
cout<<"\t\tMAIN MENU\n\n"
<<"\tPress 1 : To add record\n"
<<"\tPress 2 : To display record\n"
<<"\tPress 3 : To delete record\n"
<<"\tPress 4 : To modify data\n"
<<"\tPress 5 : To exit\n";
}
void main()
{
int opt;
float nmarks;
do
{
clrscr();
menu();
cout<<"\n\tEnter your option : ";
cin>>opt;
switch(opt)
{
case 1:clrscr();
cout<<"\nFill following details :\n";
create();
break;
case 2:clrscr();
cout<<"\nDetails are :\n " ;
display_data();
getch();
break;
case 3:clrscr();
Delete();
cout<<"\nNew file is :\n";
display_data();
getch();
break;
case 4:clrscr();
modify();
getch();
break;
case 5:cout<<"\n\tExit!!";
break;
default:cout<<"\n\tInvalid option!!";
getch();
}
} while(opt!=5);
getch();
}
No comments:
Post a Comment