1. #include<iostream>
#include<conio.h>
#include<process.h>
#include<windows.h>
#include<string.h>
#include<stdio.h>
#include<fstream>
using namespace std;
void student_input(char);
void student_output(char);
void st_delete(char);
void modify(char);
void tfileinput();
void ttmodify() ;
void tfileoutput();
void tdelet();
class student
{
int rollno , year ;
// student data to be entered
long long contactno;
char name[40] , father_name[50] , gender ,course[50] ,dob[20] ,
hometown[50] ;
public:
student();
void input();
void output();
void smodify();
int getrollno();
void menu();
void displaylist();
};
class teacher
{
int teacher_id , age;
float salary;
//teacher data to be entered
char name[40] , gender , subject[70];
public:
teacher();
void input();
void output();
void tmodify();
int get_teacher_id();
void menu();
void displaylist();
};
/****************************************STUDENT
CONSTRUCTOR***************************************/
student::student()
{
rollno=year=contactno=0;
strcpy(name,"NULL");
strcpy(father_name,"NULL");
strcpy(course,"NULL");
strcpy(course,"NULL");
strcpy(dob,"NULL");
strcpy(hometown,"NULL");
gender='0';
}
/********************************************TEACHER
CONSTRUCTOR*******************************************************/
teacher::teacher()
2. {
teacher_id=age=salary=0;
strcpy(name,"NULL");
gender='0';
strcpy(subject,"NULL");
}
void display()
{
for(int i=0;i<3;i++)
{
cout<<"nnnttSTUDENT AND TEACHER DATABASEn";
cout<<"nnnntttLOADINGt";
for(int j=0;j<3;j++)
{
cout<<" . ";
Sleep(350);
}
system("cls");
}
}
/***********************************STUDENT
MENU********************************************************************/
void student::menu()
{
char c,ch,n;
system("cls");
cout<<"ntEnter year whose records to be maintainednFor First year press
1nFor Second year press 2nFor Third year press 3n";
cin>>n;
do
{
system("cls");
cout<<"ntttEnter choice:n";
cout<<"ntt1. Student Detailsn";
cout<<"ntt2. Add new recordn";
cout<<"ntt3. Modify a recordn";
cout<<"ntt4. Delete a recordn";
cout<<"ntt5. Return to main menun";
cout<<"ntt6. Exit from programn";
cin>>c;
cout<<"n";
switch(c)
{
case '1': student_output(n);
break;
case '2': student_input(n);
break;
case '3': modify(n);
break;
case '4': st_delete(n);
break;
case '5': return;
case '6': system("cls");
cout<<"nHave a nice day :)n";
3. getch();
exit(0);
}
cout<<"ntDo you want to continuen";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
/***********************************TEACHER
MENU********************************************************************/
void teacher::menu()
{
char c,ch;
do
{
system("cls");
cout<<"nttEnter choice:n";
cout<<"ntt1. Teacher Detailsn";
cout<<"ntt2. Add new recordn";
cout<<"ntt3. Modify a recordn";
cout<<"ntt4. Delete a recordn";
cout<<"ntt5. Return to main menun";
cout<<"ntt6. Exit from programn";
cin>>c;cout<<"n";
switch(c)
{
case '1': tfileoutput();
break;
case '2': tfileinput();
break;
case '3': ttmodify();
break;
case '4': tdelet();
break;
case '5': return ;
case '6': system("cls");
cout<<"nHave a nice day :)n";
getch();
exit(0);
}
cout<<"ntDo you want to continuen";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
void student_input(char q)
//writing student records in file
{
student s;
int n;
ofstream fout;
cout<<"nHow many records do u want to Entern";
cin>>n;
switch(q)
{
case '1':fout.open("stud1.txt",ios::app);
4. break;
case '2':fout.open("stud2.txt",ios::app);
break;
case '3':fout.open("stud3.txt",ios::app);
break;
}
while(n--)
{
s.input();
fout.write((char*)(&s),sizeof(s));
}
fout.close();
}
void tfileinput()
//writing teacher record in file
{
teacher t;
int n;
ofstream fout;
cout<<"nHow many records do u want to entern";
cin>>n;
fout.open("teacher.txt",ios::app);
while(n--)
{
t.input();
fout.write((char*)(&t),sizeof(t));
}
fout.close();
}
void student::displaylist()
//STUDENT DISPLAY LIST
{
cout<<rollno<<"tt"<<name<<"n";
}
void teacher::displaylist()
//TEACHER DISPLAY LIST
{
cout<<teacher_id<<"tt"<<name<<"n";
}
void student_output(char q)
{
system("cls");
ifstream fin;
//output
student
student s;
int f=0;
switch(q)
{
case '1': fin.open("stud1.txt");
break;
case '2': fin.open("stud2.txt");
5. break;
case '3': fin.open("stud3.txt");
break;
}
cout<<"ntttSTUDENT LISTn"<<"nRoll No. "<<"t Namen";
while(fin.read((char*)(&s),sizeof(s)))
//To print student details list
s.displaylist();
int n;
fin.close();
switch(q)
{
case '1':fin.open("stud1.txt");
break;
case '2':fin.open("stud2.txt");
break;
case '3':fin.open("stud3.txt");
break;
}
cout<<"nnttEnter Roll no. whose record you want to See ('-1' to go back
to previous menu)n";
cin>>n;
if(n==-1)
{
system("cls");
return;
}
system("cls");
while(fin.read((char*)(&s),sizeof(s)))
//To print student file details
{
if(n==s.getrollno())
{
s.output();
f=1;
}
}
if(f==0)
cout<<"nStudent record not found !!!n";
fin.close();
}
/***************************TEACHER READ FROM FILE
FUNCTION******************************************************************/
void tfileoutput()
{
system("cls");
ifstream fin;
teacher t;
int f=0;
fin.open("teacher.txt");
cout<<"ttTEACHER LISTnn";
cout<<"IDtt"<<"Namenn";
while(fin.read((char*)(&t),sizeof(t)))
t.displaylist();
int n;
fin.close();
cout<<"nnEnter Teacher ID whose record you want to see ('-1' to go back to
6. previous menu)n";
cin>>n;
if(n==-1)
{
system("cls");
return;
}
system("cls");
fin.open("teacher.txt");
while(fin.read((char*)(&t),sizeof(t)))
{
if(n==t.get_teacher_id())
{
t.output();
f=1;
}
}
if(f==0)
cout<<"nEntered ID not Foundn";
fin.close();
}
/************************************STUDENT MODIFY
FUNCTION************************************************/
void modify(char q)
{
fstream fio;
switch(q)
{
case '1':fio.open("stud1.txt");
break;
case '2':fio.open("stud2.txt");
break;
case '3':fio.open("stud3.txt");
break;
}
int no;
long pos;
student s;
char found='f';
cout<<"nntEnter the roll no. whose record is to be modified n ";
cin>>no;
fio.seekg(0);
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char *)&s, sizeof(s));
if(s.getrollno()==no)
{
fio.seekg(pos);
system("cls");
s.output();
s.smodify();
fio.write((char*) &s, sizeof(s));
found='t';
break ;
}
}
if(found=='f')
cout<<"nntRecord not found !!n";
fio.close();
getch();
7. }
void student::smodify()
{
int srollno , syear ; //
temporary data storages
long long scontactno;
char sname[40] , sfather_name[50] , sgender , scourse[50] , sdob[20] ,
shometown[50] ;
cout<<"nEnter New Detailsn";
cout<<"nRoll No. (-1 to retain old one)n";
cin>>srollno;
cout<<"nName ('.' to retain old one )n";
cin.ignore(40,'n');
cin.getline(sname,40,'n');
cout<<"nFather'sName ('.' to retain old one )n";
//cin.ignore(40,'n');
cin.getline(sfather_name,40,'n');
cout<<"nDate of birth ('.' to retain old one )n";
cin.getline(sdob,20,'n');
cout<<"nGender (M/F) ('.' to retain old one )n";
cin>>sgender;
cout<<"nContact no. (-1 to retain old one)n";
cin>>scontactno;
cout<<"nCourse ('.' to retain old one )n";
cin.ignore(50,'n');
cin.getline(scourse,50,'n');
cout<<"nHometown ('.' to retain old one )n";
//cin.ignore(50,'n');
cin.getline(shometown,50,'n');
cout<<"Year (-1 to retain old one)n";
cin>>syear;
if(srollno != -1)
rollno=srollno;
if(scontactno != -1)
contactno=scontactno;
if(syear != -1)
year=syear;
if(strcmp(sname,".") != 0)
strcpy(name,sname);
if(sgender != '.')
gender=sgender;
if(strcmp(sfather_name,".") != 0)
strcpy(father_name,sfather_name);
if(strcmp(shometown,".") != 0)
strcpy(hometown,shometown);
if(strcmp(sdob,".") != 0)
8. strcpy(dob,sdob);
if(strcmp(scourse,".") != 0)
strcpy(course,scourse);
}
/*************************************************TEACHER MODIFY
FUNCTION*******************************************************/
void ttmodify()
{
system(" cls ");
fstream fio;
int no;
long pos;
teacher t;
char found='f';
fio.open("teacher.txt",ios::in|ios::out);
cout<<"nntEnter the ID whose record is to be modified n ";
cin>>no;
fio.seekg(0);
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char *)&t, sizeof(t));
if(t.get_teacher_id()==no)
{
fio.seekg(pos);
system("cls");
t.output();
t.tmodify();
fio.write((char*) &t, sizeof(t));
found='t';
break ;
}
}
if(found=='f')
cout<<"nntRecord not found !!n";
fio.close();
getch();
}
void teacher::tmodify()
{
int tteacher_id , tage;
float tsalary;
//temporary teacher data to be entered
char tname[40] , tgender , tsubject[70];
cout<<"nnEnter New Detailsnn";
cout<<"Enter New Teacher ID ('-1' to retain old one )n";
cin>>tteacher_id;
cout<<"nName ('.' to retain old one )n";
cin.ignore(40,'n');
cin.getline(tname,40,'n');
cout<<"nAge ('-1' to retain old one )n";
cin>>tage;
cout<<"nEnter gender(M/F) ('.' to retain old one )n";
cin>>tgender;
cout<<"nEnter Subject ('.' to retain old one )n";
9. cin.ignore(40,'n');
cin.getline(tsubject,70,'n');
cout<<"nEnter salary ('-1' to retain old one )n";
cin>>tsalary;
if(tteacher_id != -1)
teacher_id=tteacher_id;
if(strcmp(tname,".") != 0)
strcpy(name,tname);
if(strcmp(tsubject,".") != 0)
strcpy(subject,tsubject);
if(tsalary != -1)
salary=tsalary;
if(tage != -1)
age=tage;
if(tgender != '.')
gender=tgender;
}
/*************************************STUDENT RECORD DELETE FUNCTION
FUNCTION*********************************************/
void st_delete(char q)
{
int n;
char ch,f;
student s;
ifstream fin;
ofstream fout;
fout.open("temp.txt");
switch(q)
{
case '1':fin.open("stud1.txt");
break;
case '2':fin.open("stud2.txt");
break;
case '3':fin.open("stud3.txt");
break;
}
cout<<"nEnter roll no. whose record you want to DELETEn";
cin>>n;
system("cls");
while(fin.read((char*)(&s),sizeof(s)))
{
if(n==s.getrollno())
{
f='y';
s.output();
cout<<"nCONFIRM DELETE(Y/N)n";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"nRECORD DELETEDn";
getch();
continue;
}
10. }
fout.write((char*)&s,sizeof(s));
}
if(f!='y')
cout<<"nstudent record not foundn";
fin.close();
fout.close();
switch(q)
{
case '1':remove("stud1.txt");
rename("temp.txt","stud1.txt");
break;
case '2':remove("stud2.txt");
rename("temp.txt","stud2.txt");
break;
case '3':remove("stud3.txt");
rename("temp.txt","stud3.txt");;
break;
}
}
/*************************************TEACHER RECORD DELETE FUNCTION
FUNCTION*********************************************/
void tdelet()
{ int n;
char ch,f;
teacher t;
ifstream fin;
ofstream fout;
fin.open("teacher.txt");
fout.open("temp.txt");
cout<<"nttEnter Teacher id whose record you want to DELETEn";
cin>>n;
system("cls");
while(fin.read((char*)(&t),sizeof(t)))
{if(n==t.get_teacher_id())
{ f='y';
t.output();
cout<<"nCONFIRM DELETE(Y/N)n";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"nRECORD DELETEDn";
getch();
continue;
}
}
fout.write((char*)&t,sizeof(t));
}
if(f!='y')
cout<<"nteacher record not foundn";
fin.close();
fout.close();
remove("teacher.txt");
rename("temp.txt","teacher.txt");
}
/**********************************STUDENT DATA INPUT
FUNCTION**************************************************************/
void student::input()
{