Cpp Projects

C++ Projects

Bank Project

Student Database Project

Employee Database Project

Telephone Database Project

 

Student Database Project

Student database project

Student Database project

This projects maintains student details and has following options:

  • Add student record

  • Display all student records

  • search a student record by roll no

  • Search a student record by name

  • Delete a student record by roll no

  • Modify a student record by roll no

  • Random display of student record

#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
class student
{
int roll;
char name[10];
float age;
public:
void add();
void disp();
void get_input();
void disp_all();
void disp_roll();
void disp_name();
void del_roll();
void modify();
void rand_disp();
void menu();
int check_roll(int);
int check_age(float);
};
int student::check_roll(int n)
{
if (n<=0)
return(0);
else
return(1);
}
int student::check_age(float n)
{
if (n<=0)
return(0);
else
return(1);
}

void student::get_input()
{
int z=0;
while(1)
{
cout<<“Enter the Roll Number “;
cin>>roll;
z=check_roll(roll);
if (z==0)
cout<<“Invalid roll number , try again”<<endl;
else
break;
}
cout<<“Enter the First name “;
cin>>name;
strupr(name);
while(1)
{
cout<<“Enter the age “;
cin>>age;
z=check_age(age);
if(z==1)
break;
else
cout<<“Invalid age , try again”<<endl;
}
}

void student::disp()
{
cout<<roll<<” “<<name<<” “<<age<<endl;
}

void student::add()
{
student s1;
fstream file;
file.open(“stud1.dat”,ios::in);
if (file.fail())
{
//cout<<“Unable to open the file, file does not exist, we have to create it\n”;
file.open(“stud1.dat”,ios::out);
}
else
{
//cout<<“File is present , so should be opened in append mode\n”;
file.close();
file.open(“stud1.dat”,ios::app);
}
s1.get_input();
file.write((char*)&s1,sizeof(s1));
file.close();
}


void student::disp_all()
{
student s1;
fstream file;
file.open(“stud1.dat”,ios::in);
if (file.fail())
{
cout<<“Unable to open the file “;
getch();
return;
}
file.read((char*)&s1,sizeof(s1));
clrscr();
cout<<“Student Details”<<endl;
cout<<“—————-“<<endl;
cout<<“Roll name age “<<endl;
while(!file.eof())
{
s1.disp();
file.read((char*)&s1,sizeof(s1));
}
file.close();
}

void student::disp_roll()
{
student s1;
int tr,z=0;
fstream file;
cout<<“Enter the roll number “;
cin>>tr;
file.open(“stud1.dat”,ios::in);
if (file.fail())
{
cout<<“Unable to open the file “;
getch();
return;
}
file.read((char*)&s1,sizeof(s1));
while(!file.eof())
{
if (tr==s1.roll)
{
z=1;
cout<<“Record is found \n”;
cout<<“Press any key to see the details \n”;
getch();
clrscr();
cout<<“Student Details”<<endl;
cout<<“—————-“<<endl;
cout<<“Roll name age “<<endl;
s1.disp();
}

file.read((char*)&s1,sizeof(s1));
}
if (z==0)
cout<<“Record with roll number “<<tr <<” is not present”<<endl;

file.close();
}

void student::disp_name()
{
student s1;
int z=0;
char tn[10];
fstream file;
cout<<“Enter the name of student to search “;
cin>>tn;
strupr(tn);
file.open(“stud1.dat”,ios::in);
if (file.fail())
{
cout<<“Unable to open the file “;
getch();
return;
}
file.read((char*)&s1,sizeof(s1));
while(!file.eof())
{
if (strcmpi(tn,s1.name)==0)
{
z=1;
cout<<“Record is found \n”;
cout<<“Press any key to see the details \n”;
getch();
clrscr();
cout<<“Student Details”<<endl;
cout<<“—————-“<<endl;
cout<<“Roll name age “<<endl;
s1.disp();
}

file.read((char*)&s1,sizeof(s1));
}
if (z==0)
cout<<“Record with name “<<tn <<” is not present”<<endl;

file.close();
}


void student::del_roll()
{
student s1;
int z=0,tr;
fstream infile,tempfile;
cout<<“Enter the roll number whose record u want to delete “;
cin>>tr;
infile.open(“stud1.dat”,ios::in);
tempfile.open(“temp.dat”,ios::out);

if (infile.fail())
{
cout<<“Unable to open the file \n”;
return;
}
infile.read((char*)&s1,sizeof(s1));
while(!infile.eof())
{
if (tr==s1.roll)
{
z=1;
cout<<“Record is found \n”;
cout<<“Press any key to see the details \n”;
getch();
cout<<“Student Details”<<endl;
cout<<“—————-“<<endl;
cout<<“Roll name age “<<endl;
s1.disp();
}
else
tempfile.write((char*)&s1,sizeof(s1));

infile.read((char*)&s1,sizeof(s1));
}
infile.close();
tempfile.close();
if (z==0)
cout<<“The record with the roll number is not present”<<endl;
else
{
remove(“stud1.dat”);
rename(“temp.dat”,”stud1.dat”);
}

}

void student::modify()
{
student s1;
int z=0,tr;
fstream infile,tempfile;
cout<<“Enter the roll number whose record u want tomodify “;
cin>>tr;
infile.open(“stud1.dat”,ios::in);
tempfile.open(“temp.dat”,ios::out);

if (infile.fail())
{
cout<<“Unable to open the file \n”;
return;
}
infile.read((char*)&s1,sizeof(s1));
while(!infile.eof())
{
if (tr==s1.roll)
{
z=1;
cout<<“Record is found \n”;
cout<<“Press any key to see the details \n”;
getch();
cout<<“Student Details”<<endl;
cout<<“—————-“<<endl;
cout<<“Roll name age “<<endl;
s1.disp();
cout<<“Enter new data”<<endl;
s1.get_input();
}
tempfile.write((char*)&s1,sizeof(s1));

infile.read((char*)&s1,sizeof(s1));
}
infile.close();
tempfile.close();
if (z==0)
cout<<“The record with the roll number is not present”<<endl;
else
{
remove(“stud1.dat”);
rename(“temp.dat”,”stud1.dat”);
}

}

void student::rand_disp()
{
fstream infile;
student s;
long len=0,trec=0,flen=0;
int n;
infile.open(“stud1.dat”,ios::in);
infile.seekg(0,ios::end);
flen=infile.tellp();
cout<<“rec size is “<<sizeof(s)<<endl;
len=sizeof(s);
trec=flen/len;
cout<<“total records are “<<trec<<endl;
cout<<“total file length is “<<flen<<endl;
cout<<“Enter the rec number to display “;
cin>>n;
if (n<=0 || n>trec)
{
cout<<“Invalid record number “;
getch();
}
else
{

infile.seekg(0,ios::beg);
infile.seekg((n-1)*sizeof(s),ios::beg);
infile.read((char*)&s,sizeof(s));
cout<<“roll “<<s.roll<<endl;
cout<<“name is ” <<s.name<<endl;
cout<<“age “<<s.age<<endl;
}
}
void student::menu()
{
cout<<“Main Menu “<<endl;
cout<<“1. Addition of record “<<endl;
cout<<“2. Display of all the record “<<endl;
cout<<“3. display by roll number “<<endl;
cout<<“4. Search a record by name “<<endl;
cout<<“5. delete by roll number “<<endl;
cout<<“6. Modify a record by roll number “<<endl;
cout<<“7. display by record number (random)”<<endl;
cout<<“8. Exit “<<endl;
cout<<“Enter your choice “;

}

void main()
{
student s1;
int op=0;
while(op!=8)
{
clrscr();
s1.menu();
cin>>op;
switch(op)
{
case 1:
s1.add();
break;
case 2:
s1.disp_all();
break;
case 3:
s1.disp_roll();
break;
case 4:
s1.disp_name();
break;
case 5:
s1.del_roll();
break;
case 6:
s1.modify();
break;
case 7:
s1.rand_disp();
break;
case 8:
cout<<“End of the program “<<endl;
break;
default:
cout<<“invalid option entered \n”;
break;
}
getch();
}
}

Number Of Visitors

0173166