[go: up one dir, main page]

0% found this document useful (0 votes)
88 views29 pages

School Management Project C++

This document defines two C++ classes - Student and Class. The Student class stores student record details like name, roll number, address etc. It contains methods to get, print and modify these details. The Class class stores class record details like student name, roll number and subject. It also contains methods to get, print and modify these class records.

Uploaded by

Bhavesh K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views29 pages

School Management Project C++

This document defines two C++ classes - Student and Class. The Student class stores student record details like name, roll number, address etc. It contains methods to get, print and modify these details. The Class class stores class record details like student name, roll number and subject. It also contains methods to get, print and modify these class records.

Uploaded by

Bhavesh K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

class Student { //Student class for storing Records in the

student file

int title; //Master = 1, Mr = 2, Miss = 3

char studentName[30];

int rollNo;

char fatherName[30];

char motherName[30];

char address[80];

char bloodGroup[4];

public:

void getDetails(void); //Get Student Details from user

void printDetails(void){ //Printing the details of Student

cout<<"Student Name : "<<strTitle(title)<<'


'<<studentName<<endl;

cout<<"Roll No. : "<<rollNo<<endl;

cout<<"Father's Name : "<<fatherName<<endl;

cout<<"Mother's Name : "<<motherName<<endl;

cout<<"Address : "<<address<<endl;

cout<<"Blood Group : "<<bloodGroup<<endl;

int retRollNo(){ //Return Roll No

return rollNo;

char * retString(char x){ //Return all strings avaialable from the Student Class

if(x=='T')

return strTitle(title);

if(x=='N')

return studentName;

if(x=='F')

return fatherName;

if(x=='M')

return motherName;
if(x=='A')

return address;

if(x=='B')

return bloodGroup;

char * retStudentName(){ //Returns Student Name

return retString('N');

void modDetail(char ch); //Modify Details for Student

};

void Student::getDetails(){ //Get Student Details from user

system("CLS");

cout<<"Enter Title \n(Master = 1, Mr = 2, Miss = 3) : ";

do{

title = scan();

}while(title!=1 && title!=2 && title!=3);

cout<<"Enter Student Name : ";

do{

gets(studentName);

}while(strlen(studentName)==0);

cout<<"Enter Roll No. : ";

do{

rollNo=scan();

}while(rollNo<1);

cout<<"Enter Father Name : ";

do{

gets(fatherName);

}while(strlen(fatherName)==0);

cout<<"Enter Mother Name : ";

do{
gets(motherName);

}while(strlen(motherName)==0);

cout<<"Enter Address : ";

do{

gets(address);

}while(strlen(address)==0);

cout<<"Enter Blood Group : ";

int v = 0,i;

do{

gets(bloodGroup);

for (i=0;i<strlen(bloodGroup);i++) bloodGroup[i] =


toupper(bloodGroup[i]);

for(i=0;i<8;i++)

if(!strcmp(bloodGroup,bGs[i])){

v=1;

break;

}while(!v);

void Student::modDetail(char ch){ //Modify Details for Student

system("CLS");

if(ch=='T'){ //Argument will tell which detail to modify

cout<<"Enter Title \n(Master = 1, Mr = 2, Miss = 3) : ";

do{

title=scan();

}while(title!=1 && title!=2 && title!=3);

else if(ch=='N'){

cout<<"Enter Student Name : ";

do{

gets(studentName);
}while(strlen(studentName)==0);

else if(ch=='R'){

int r=rollNo; //Save current Roll No.

cout<<"Enter Roll No. : ";

fstream fl(FLBSTUD,ios::in|ios::binary);

Student obj;

do{

rollNo = scan();

fl.close();

fl.open(FLBSTUD,ios::in|ios::binary);

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

if(obj.retRollNo()==rollNo && r!=rollNo){ //Check if


the new rollNo already exists

cout<<"\nSame Roll No. Already Exists !\n";

cout<<"Enter Roll No. : ";

rollNo = -1;

}while(rollNo<1);

fl.close();

else if(ch=='F'){

cout<<"Enter Father Name : ";

do{

gets(fatherName);

}while(strlen(fatherName)==0);

}
else if(ch=='M'){

cout<<"Enter Mother Name : ";

do{

gets(motherName);

}while(strlen(motherName)==0);

else if(ch=='A'){

cout<<"Enter Address : ";

do{

gets(address);

}while(strlen(address)==0);

else {

cout<<"Enter Blood Group : ";

int v = 0,i;

do{

gets(bloodGroup); //Loop for checking


valid Blood Groups

for (i=0;i<strlen(bloodGroup);i++) bloodGroup[i] =


toupper(bloodGroup[i]);

for(i=0;i<8;i++)

if(!strcmp(bloodGroup,bGs[i])){

v=1;

break;

}while(!v);

}class Class { //Class class for storing Records in class file

int class_standard;

char studentName[30];

int rollNo;

char Subject[30];
public:

void getDetails(); //Get Class Record Detail from user

void printDetails(int i=1){ //Print Class Record Details

cout<<"Student Name : "<<studentName<<endl;

if(i)

cout<<"Class Standard : "<<class_standard<<endl;

cout<<"Roll No. : "<<rollNo<<endl;

cout<<"Subject : "<<Subject<<endl;

int retClass(){ //Return Class Standard

return class_standard;

int retRollNo(){ //Return Roll No

return rollNo;

char * retString(char x){ //Return all strings avaialable from the Class class

if(x=='N')

return studentName;

if(x=='S')

return Subject;

char * retStudentName(){ //Return Student Name

return retString('N');

void modDetail(char ch);

};

void Class::getDetails(){ //Get Class Record Details from user

system("CLS");

cout<<"Enter Class Standard : ";

do{
class_standard = scan();

}while(class_standard>12 || class_standard<1);

cout<<"Enter Student Name : ";

do{

gets(studentName);

}while(strlen(studentName)==0);

cout<<"Enter Roll No. : ";

do{

rollNo = scan();

}while(rollNo<1);

cout<<"Enter Subject : ";

do{

gets(Subject);

}while(strlen(Subject)==0);

void Class::modDetail(char ch){ //Modify Class Record

system("CLS");

if(ch=='C'){

cout<<"Enter Class Standard : ";

do{

class_standard = scan();

}while(class_standard>12 || class_standard<1);

else if(ch=='N'){

cout<<"Enter Student Name : ";

do{

gets(studentName);

}while(strlen(studentName)==0);

else if(ch=='R'){

fstream fl(FLBCLAS,ios::in|ios::binary);
Class obj;

int r = rollNo; //Save Current Roll No.

cout<<"Enter Roll No. : ";

do{

rollNo = scan();

fl.close();

fl.open(FLBCLAS,ios::in|ios::binary);

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break; //Check if new Roll No.


Already Exists

if(obj.retRollNo()==rollNo && r!=rollNo){

cout<<"\nSame Roll No. Already Exists !\n";

cout<<"Enter Roll No. : ";

rollNo=-1;

}while(rollNo<1);

fl.close();

else {

cout<<"Subject : ";

int v = 0,i;

do{

gets(Subject);

}while(strlen(Subject)==0);

}void insertStudent(){ //Insert Student Record in File

system("CLS");

Student obj,obj2;
char ch;

int v=0;

cout<<"Enter Details for new Student :\n";

obj.getDetails();

fstream fl1(FLBSTUD, ios::in|ios::binary);

ofstream fl2;

if(!fl1){ //If file does not exist, create new file

fl2.open(FLBSTUD,ios::out|ios::binary);

fl2.write((char*)&obj, sizeof(obj));

fl2.close();

cout<<"Record successfully inserted !\n";

return;

while(!fl1.eof()){

fl1.read((char*)&obj2,sizeof(obj));

if(fl1.eof()){

break;

if(obj.retRollNo()==obj2.retRollNo()){ //If record with same Roll No. exists,


then abort insertion

cout<<"Record with same Roll No. with following details already exists :
\n";

obj2.printDetails();

cout<<"Insertion Aborted !\n";

return;

else if(strcmpi(obj.retStudentName(),obj2.retStudentName())==0){

if (!v) //Warns user that


Record with same name exists

cout<<"Warning : Record with same name exists with follwing


details : \n";

obj2.printDetails();
cout<<'\n';

v=1;

if(v){

cout<<"Do you still wish to insert record (Y/N) ? ";

do{ //Asks for


user confirmation after warning

cin>>ch;

ch = toupper(ch);

}while(ch!= 'Y' && ch!='N');

if(ch=='N'){

cout<<"Insertion Aborted !\n";

return;

fl2.open(FLBSTUD,ios::out|ios::app|ios::binary);

fl2.seekp(0,ios::end);

fl2.write((char*)&obj, sizeof(obj));

fl2.close();

cout<<"Record Inserted successfully !\n";

}void insertClass(){ //Insert Class Record in File

system("CLS");

Class obj,obj2;

char ch;

int v=0;

cout<<"Enter Class Details :\n";

obj.getDetails();

fstream fl1(FLBCLAS, ios::in|ios::binary);

ofstream fl2;

if(!fl1){ //Create new file if it does


not exist
fl2.open(FLBCLAS,ios::out|ios::binary);

fl2.write((char*)&obj, sizeof(obj));

fl2.close();

cout<<"Record Inserted successfully !\n";

return;

while(!fl1.eof()){

fl1.read((char*)&obj2,sizeof(obj));

if(fl1.eof()){

break;

if(obj.retRollNo()==obj2.retRollNo()){ //Abort if same Roll No already


exists

cout<<"Record with same Roll No. with following details already exists :
\n";

obj2.printDetails();

cout<<"Insertion Aborted !\n";

return;

else if(strcmpi(obj.retStudentName(),obj2.retStudentName())==0){

if (!v)
//Warns user if record with same Roll No. Already Exists

cout<<"Warning : Record with same name exists with follwing


details : \n";

obj2.printDetails();

cout<<'\n';

v=1;

if(v){ //Asks for


confirmation after warning

cout<<"Do you still wish to insert record (Y/N) ? ";

do{
cin>>ch;

ch = toupper(ch);

}while(ch!= 'Y' && ch!='N');

if(ch=='N'){

cout<<"Insertion Aborted !\n";

return;

fl2.open(FLBCLAS,ios::out|ios::app|ios::binary);

fl2.seekp(0,ios::end);

fl2.write((char*)&obj, sizeof(obj));

fl2.close();

cout<<"Record Inserted successfully !\n";

}int dispClassRecord(){ //Display all Class Records

Class obj;

int v=0;

fstream fl(FLBCLAS, ios::in|ios::binary);

if(!fl){ //If file does not exist

cout<<"Empty Records !\n";

return 0;

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

v=1;

obj.printDetails();

RULE('-');

fl.close();
if(!v)

cout<<"Empty Records !\n";

return v;

int dispStudentRecord(){ //Display all Student Records

system("CLS");

Student obj;

int v=0;

fstream fl(FLBSTUD, ios::in|ios::binary);

if(!fl){ //If file does not exist

cout<<"Empty Records !\n";

return 0;

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

v=1;

obj.printDetails();

RULE('-');

fl.close();

if(!v)

cout<<"Empty Records !\n";

return v;

}int searchClassID(const string str = "search"){ //Searching Class Record by different


Attributes

fstream fl(FLBCLAS,ios::in|ios::ate|ios::binary);

if((!fl)||fl.tellg()==0){ //If file is empty or zero size

cout<<"No Records Found !\n";

return 0;
}

fl.close();

fl.open(FLBCLAS,ios::in|ios::binary);

cout<<"Enter class to "<<str<<" (0 to disable) : "; //0 to search independent of


class

int cl;

char ch;

char query[30];

Class obj;

int found = 0;

do{

cin>>cl;

}while(cl>12 || cl<0);

cout<<"Enter Attribute to search :\n";

cout<<" (N)ame of Student.\n";

cout<<" (S)ubject.\n";

cout<<"Enter your choice : ";

do{

cin>>ch;

ch = toupper(ch);

}while(ch!='N' && ch!='S');

cout<<"Enter Query : ";

do{

gets(query);

}while(strlen(query)==0);

while(!fl.eof()){

fl.read((char*)&obj,sizeof(obj));

if(fl.eof()){

break;
}

if(CL(cl,obj.retClass())){ //Check class using the defined Macro

if((strcmpi(query,obj.retString(ch))==0)){

if(!found)

cout<<"\nSearch Results : \n\n";

obj.printDetails();

RULE('-');

found = 1;

if(!found)

cout<<"No Records Found !\n";

fl.close();

return found;

int searchStudentID(const string str = "search"){ //Search Student by Attributes

system("CLS");

fstream fl(FLBSTUD,ios::in|ios::ate|ios::binary);

if((!fl)||fl.tellg()==0){ //If file is empty or zero size

cout<<" No Records Found !\n";

return 0;

fl.close();

fl.open(FLBSTUD,ios::in|ios::binary);

char ch;

char query[30];

Student obj;

int found = 0;

cout<<"Enter Attribute to "<<str<<" :\n";

cout<<" (T)itle.\n";
cout<<" (N)ame of Student.\n";

cout<<" (F)ather's Name.\n";

cout<<" (M)other's Name.\n";

cout<<" (A)ddress.\n";

cout<<" (B)lood Group.\n";

cout<<"Enter your choice : ";

do{

cin>>ch;

ch = toupper(ch);

}while(ch!='T' && ch!='N' && ch!='F' && ch!='M' &&


ch!='A' && ch!='B');

cout<<"\nEnter Query : ";

do{

gets(query);

}while(strlen(query)==0);

while(!fl.eof()){

fl.read((char*)&obj,sizeof(obj));

if(fl.eof()){

break;

if((strcmpi(query,obj.retString(ch))==0)){

if(!found)

cout<<"\nSearch Results : \n\n";

obj.printDetails();

RULE('-');

found = 1;

if(!found)

cout<<"No Records Found !\n";

fl.close();
return found;

int searchByRollNo(int i){ //Search Record by Roll No., 1 for Class, 2 for
Student

system("CLS");

int r;

if(i==1){

Class obj;

int found = 0;

int cl;

cout<<"Enter class to search in (0 to disable) : "; //0 to search independent


of class

do{

cin>>cl;

}while(cl>12 || cl<0);

cout<<"Enter Roll No. to search for : ";

cin>>r;

fstream fl(FLBCLAS,ios::in|ios::binary);

if(!fl){ //No file exists

cout<<"No Records Found !\n";

return 0;

while(!fl.eof()){

fl.read((char*)&obj,sizeof(obj));

if(fl.eof()){

break;

if(CL(cl,obj.retClass())){

if(r==obj.retRollNo()){ //Match attribute


for each Record

if(!found)

cout<<"\nSearch Results : \n\n";


obj.printDetails();

RULE('-');

found = 1;

if(!found)

cout<<"No Records Found !\n";

fl.close();

return found;

else{

int found=0;

Student obj;

cout<<"Enter Roll No. to search for : ";

cin>>r;

fstream fl(FLBSTUD,ios::in|ios::binary);

if(!fl){ //No file exists

cout<<"No Records Found !\n";

return 0;

while(!fl.eof()){

fl.read((char*)&obj,sizeof(obj));

if(fl.eof()){

break;

if(r==obj.retRollNo()){

if(!found)

cout<<"\nSearch Results : \n\n";

obj.printDetails();

RULE('-');
found = 1;

if(!found)

cout<<"No Records Found !\n";

fl.close();

return found;

}void sortByStudents(char ch){ //Sort Records

vector <pair<string,int> > lst; //Make vector of pairs of string to sort by


and Roll No.

int i;

if(ch=='C'){ //Sort Class Records

Class obj;

int v=0;

fstream fl(FLBCLAS, ios::in|ios::binary);

if(!fl){

cout<<"Empty Records !\n";

return;

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

v=1;

lst.push_back(make_pair(obj.retString('N'),obj.retRollNo())); //Push each


pair in the vector

fl.close();

if(v==0){

cout<<"Empty Records !\n";

return;
}

sort(lst.begin(),lst.end(),strcmpis); //Sort using <algorithm> sort and


Custom Comparison

fstream tmp("temp.txt",ios::out|ios::binary);

fl.open(FLBCLAS,ios::in|ios::binary);

fl.seekg(0,ios::beg);

for(i=0;i<lst.size();i++){

fl.close();

fl.open(FLBCLAS,ios::in|ios::binary);

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

if(obj.retRollNo()==lst[i].second){ //Check each Roll


No. from each pair and write record to new file

tmp.write((char*)&obj,sizeof(obj));

fl.close();

tmp.close();

remove(FLBCLAS);

rename("temp.txt",FLBCLAS);

cout<<"\nThe Records have been successfully sorted !\n\n";

dispClassRecord();

else{ //Sort Student Records

Student obj;

int v=0;

char c;

fstream fl(FLBSTUD, ios::in|ios::binary);

system("CLS");
cout<<"Enter criteria to sort :\n";

cout<<" (N)ame of Student.\n";

cout<<" (T)itle.\n";

cout<<"Enter your choice : \n";

do{

cin>>c;

c=toupper(c);

}while(c!='N' && c!='T');

if(!fl){

cout<<"Empty Records !\n";

return;

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

v=1;

lst.push_back(make_pair(obj.retString(c),obj.retRollNo())); //Push each


pair in the vector

fl.close();

if(v==0){

cout<<"Empty Records !\n";

return;

sort(lst.begin(),lst.end(),strcmpis); //Sort using <algorithm> sort and


Custom Comparison

fstream tmp("temp.txt",ios::out|ios::binary);

fl.open(FLBSTUD,ios::in|ios::binary);

fl.seekg(0,ios::beg);

for(i=0;i<lst.size();i++){

fl.close();
fl.open(FLBSTUD,ios::in|ios::binary);

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

if(obj.retRollNo()==lst[i].second){ //Check each Roll


No. from each pair and write record to new file

tmp.write((char*)&obj,sizeof(obj));

fl.close();

tmp.close();

remove(FLBSTUD);

rename("temp.txt",FLBSTUD);

cout<<"\nThe Records have been successfully sorted !\n\n";

dispStudentRecord();

}void delClassRecord(){ //Delete Class Records

system("CLS");

Class obj; //Writes to new file except record to be


deleted

int f=0;

if(!searchClassID("delete from"))

return;

cout<<"\nEnter Roll No. to delete : ";

int r;

char ch;

cin>>r;

fstream fl(FLBCLAS, ios::in|ios::binary);

fstream fo("temp.dat", ios::out|ios::binary);

while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

if (r==obj.retRollNo()){

cout<<"Record with following info will be deleted :\n\n";

obj.printDetails();

cout<<"Do you wish to continue ? (Y/N) : ";

do{

cin>>ch;

ch = toupper(ch);

}while(ch!='N' && ch!='Y');

if(ch=='N'){

cout<<"Deletion Aborted !\n";

fl.close();

fo.close();

remove("temp.dat");

return;

f=1;

continue;

fo.write((char*)&obj,sizeof(obj));

fl.close();

fo.close();

remove(FLBCLAS);

rename("temp.dat",FLBCLAS);

if(f)

cout<<"Record Successfully Deleted !\n";

else

cout<<"No Such Record Exists !\n";


}

void delStudentRecord(){ //Delete Student Records

system("CLS");

Student obj; //Writes to new file except record to be


deleted

int f=0;

if(!searchStudentID("delete using"))

return;

cout<<"\nEnter Roll No. to delete : ";

int r;

char ch;

cin>>r;

fstream fl(FLBSTUD, ios::in|ios::binary);

fstream fo("temp.dat", ios::out|ios::binary);

while(!fl.eof()){

fl.read((char*)&obj, sizeof(obj));

if(fl.eof())

break;

if (r==obj.retRollNo()){

cout<<"Record with following info will be deleted :\n\n";

obj.printDetails();

cout<<"Do you wish to continue ? (Y/N) : ";

do{

cin>>ch;

ch = toupper(ch);

}while(ch!='N' && ch!='Y');

if(ch=='N'){

cout<<"Deletion Aborted !\n";

fl.close();

fo.close();
remove("temp.dat");

return;

f=1;

continue;

fo.write((char*)&obj,sizeof(obj));

fl.close();

fo.close();

remove(FLBSTUD);

rename("temp.dat",FLBSTUD);

if(f)

cout<<"Record Successfully Deleted !\n";

else

cout<<"No Such Record Exists !\n";

}int main(){

char ch;

char ch1;

int ch2;

load();

do{

CLS();

RULE("*");

cout<<"\t\t\t School Management System";

RULE("*");

cout<<"\t1. Student Database Management.\n";

cout<<"\t2. Class Database Management.\n";

cout<<"\t0. Exit.\n\n";

cout<<"Enter your choice : ";

fflush(stdin);
cin>>ch1;

if(ch1=='1'){

fflush(stdin);

load();

do{

CLS();

RULE("*");

cout<<"\t\t\t School Management System";

RULE("*");

cout<<"\t\t\tStudent Database Management System";

RULE('-');

cout<<"\t1. Insert Records.\n";

cout<<"\t2. Display all Records.\n";

cout<<"\t3. Search for a Record using Roll No.\n";

cout<<"\t4. Search for a Record using other Attributes.\n";

cout<<"\t5. Sort all Records.\n";

cout<<"\t6. Modify a Record.\n";

cout<<"\t7. Delete a Record.\n";

cout<<"\t0. Exit.\n";

cout<<"\nEnter your choice : ";

ch2 = scan();

switch(ch2){

case 1:

insertStudent();

break;

case 2:

dispStudentRecord();

break;

case 3:

searchByRollNo(2);
break;

case 4:

searchStudentID();

break;

case 5:

sortByStudents('S');

break;

case 6:

modEntry('S');

break;

case 7:

delStudentRecord();

break;

fflush(stdin);

if(ch2)

cin>>ch;

else

load();

}while(ch2!=0);

if(ch1=='2'){

fflush(stdin);

load();

do{

CLS();

RULE("*");

cout<<"\t\t\t Class Management System";

RULE("*");

cout<<"\t\t\tClass Database Management System";

RULE('-');
cout<<"\t1. Insert Records.\n";

cout<<"\t2. Display all Records.\n";

cout<<"\t3. Search for a Record using Roll No.\n";

cout<<"\t4. Search for a Record using other Attributes.\n";

cout<<"\t5. Sort all Records.\n";

cout<<"\t6. Modify a Record.\n";

cout<<"\t7. Delete a Record.\n";

cout<<"\t8. Display Total Number of Students in Each Class.\


n";

cout<<"\t9. Display Students by Standard.\n";

cout<<"\t10. Display Total Fee Revenue Generated.\n";

cout<<"\t0. Exit.\n";

cout<<"\nEnter your choice : ";

ch2 = scan();

switch(ch2){

case 1:

insertClass();

break;

case 2:

dispClassRecord();

break;

case 3:

searchByRollNo(1);

break;

case 4:

searchClassID();

break;

case 5:

sortByStudents('C');

break;

case 6:
modEntry('C');

break;

case 7:

delClassRecord();

break;

case 8:

checkNoInClass();

break;

case 9:

dispByStandard();

break;

case 10:

totalRevenueGenerated();

break;

fflush(stdin);

if(ch2)

cin>>ch;

else{

load();

}while(ch2!=0);

}while(ch1!='0');

CLS();

You might also like