C++ Student Management by using class

 Hello, Guys .

We write a program on Student Management System.

Student Management System is a basic C++ program for education establishments to manage student data. Student Management Systems provide capabilities for entering student test and other assessment scores, build student schedules, and manage many other student-related data needs in a school. In this Student Management System project, user can create, display, search, modify and delete student record from a binary file.

What is C++ ?

C++ is close to the metal — just a few small steps away from assembly code. While other programming languages are built around the business domain, C++ is built around the computer. This gives you a much greater understanding of all the building blocks of programming (useful when you’re starting out).

What is class in C++?

class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private.

Source code of Student Management System (using) class.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>

using namespace std;

class Student
{
int admno;
char name[20];
char gender;
int std;
float marks;
float percentage;

public:
void getData();
void showData();
int getAdmno(){return admno;}
}s;

void Student::getData()
{
cout<<"\n\nEnter Student Details......\n";
cout<<"Enter Admission No.     : "; cin>>admno;
cout<<"Enter Full Name         : "; cin.ignore(); cin.getline(name,20);
cout<<"Enter Gender (M/F)      : "; cin>>gender;
cout<<"Enter Standard          : "; cin>>std;
cout<<"Enter Marks (out of 500): "; cin>>marks;
cout<<endl;
percentage=marks*100.0/500.00;
}

void Student::showData()
{
cout<<"\n\n.......Student Details......\n";
cout<<"Admission No.     : "<<admno<<endl;
cout<<"Full Name         : "<<name<<endl;
cout<<"Gender            : "<<gender<<endl;
cout<<"Standard          : "<<std<<endl;
cout<<"Marks (out of 500): "<<marks<<endl;
cout<<"Percentage        : "<<percentage<<endl;
cout<<endl;
}

void addData()
{
ofstream fout;
fout.open("Students.dat",ios::binary|ios::out|ios::app);
s.getData();
fout.write((char*)&s,sizeof(s));
fout.close();
cout<<"\n\nData Successfully Saved to File....\n";
}

void displayData()
{
ifstream fin;
fin.open("Students.dat",ios::in|ios::binary);
while(fin.read((char*)&s,sizeof(s)))
{
s.showData();
}
fin.close();
cout<<"\n\nData Reading from File Successfully Done....\n";
}

void searchData()
{
int n, flag=0;
ifstream fin;
fin.open("Students.dat",ios::in|ios::binary);
cout<<"Enter Admission Number you want to search : ";
cin>>n;

while(fin.read((char*)&s,sizeof(s)))
{
if(n==s.getAdmno())
{
cout<<"The Details of Admission No. "<<n<<" shown herewith:\n";
s.showData();
flag++;
}
}
fin.close();
if(flag==0)
cout<<"The Admission No. "<<n<<" not found....\n\n";
cout<<"\n\nData Reading from File Successfully Done....\n";
}

void deleteData()
{
int n, flag=0;
ifstream fin;
ofstream fout,tout;

fin.open("Students.dat",ios::in|ios::binary);
fout.open("TempStud.dat",ios::out|ios::app|ios::binary);
tout.open("TrashStud.dat",ios::out|ios::app|ios::binary);



cout<<"Enter Admission Number you want to move to Trash : ";
cin>>n;

while(fin.read((char*)&s,sizeof(s)))
{
if(n==s.getAdmno())
{
cout<<"The Following Admission No. "<<n<<" has been moved to Trash:\n";
s.showData();
tout.write((char*)&s,sizeof(s));
flag++;
}
else
{
fout.write((char*)&s,sizeof(s));
}
}
fout.close();
tout.close();
fin.close();
if(flag==0)
cout<<"The Admission No. "<<n<<" not found....\n\n";
remove("Students.dat");
rename("tempStud.dat","Students.dat");
}

void getTrash()
{
ifstream fin;
fin.open("TrashStud.dat",ios::in|ios::binary);
while(fin.read((char*)&s,sizeof(s)))
{
s.showData();
}
fin.close();
cout<<"\n\nData Reading from Trash File Successfully Done....\n";
}

void modifyData()
{
int n, flag=0, pos;
fstream fio;

fio.open("Students.dat",ios::in|ios::out|ios::binary);

cout<<"Enter Admission Number you want to Modify : ";
cin>>n;

while(fio.read((char*)&s,sizeof(s)))
{
pos=fio.tellg();
if(n==s.getAdmno())
{
cout<<"The Following Admission No. "<<n<<" will be modified with new data:\n";
s.showData();
cout<<"\n\nNow Enter the New Details....\n";
s.getData();
fio.seekg(pos-sizeof(s));
fio.write((char*)&s,sizeof(s));
flag++;
}
}
fio.close();

if(flag==0)
cout<<"The Admission No. "<<n<<" not found....\n\n";
}

void project()
{
int ch;
do
{
system("cls");
cout<<"...............STUDENT MANAGEMENT SYSTEM..............\n";
cout<<"======================================================\n";
cout<<"0. Exit from Program\n";
cout<<"1. Write Data to File\n";
cout<<"2. Read Data From File\n";
cout<<"3. Search Data From File\n";
cout<<"4. Delete Data From File\n";
cout<<"5. Get Deleted Records from Trash file\n";
cout<<"6. Modify Data in File\n";
cout<<"Enter your choice  : ";
cin>>ch;
system("cls");
switch(ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: getTrash(); break;
case 6: modifyData(); break;
}
system("pause");
}while(ch);
}

int main()
{
project();
}

______________________________________________

_______________Try second code if  first code not working_______________________________

2.

#include<iostream>

#include<fstream>

#include<iomanip>

 

using namespace std;

 

 

 

class student

{

int idnum;

char name[50];

int physics, chemistry, mathematics, english, comscience;

double per;

char grade;

void calculate();

public:

void getdata();

void showdata() const;

void show_tabular() const;

int getIDNum() const;

}; 

 

 

void student::calculate()

{

per=(physics+chemistry+mathematics+english+comscience)/5.0;

if(per>=90)

grade='A+';

else if(per>=80)

grade='A';

else if(per>=75)

grade='A-';

else if(per>=70)

grade='B+';

else if(per>=65)

grade='B';

else if(per>=60)

grade='B-';

else if(per>=55)

grade='C+';

else if(per>=50)

grade='C';

else

grade='F';

}

 

void student::getdata()

{

cout<<"\nEnter The ID number of the student ";

cin>>idnum;

cout<<"\n\nEnter student's Name: ";

cin.ignore();

cin.getline(name,50);

cout<<"\nEnter student's physics grade: ";

cin>>physics;

cout<<"\nEnter student's chemistry grade: ";

cin>>chemistry;

cout<<"\nEnter student's mathematics grade: ";

cin>>mathematics;

cout<<"\nEnter student's english grade: ";

cin>>english;

cout<<"\nEnter student's computer science grade: ";

cin>>comscience;

calculate();

}

 

void student::showdata() const

{

cout<<"\nID Number: "<<idnum;

cout<<"\nName: "<<name;

cout<<"\nPhysics: "<<physics;

cout<<"\nChemistry: "<<chemistry;

cout<<"\nMathematics: "<<mathematics;

cout<<"\nEnglish: "<<english;

cout<<"\nComputer Science: "<<comscience;

cout<<"\nPercentage: "<<per;

cout<<"\nLetter Grade: "<<grade;

}

 

void student::show_tabular() const

{

cout<<idnum<<setw(6)<<" "<<name<<setw(10)<<physics<<setw(4)<<chemistry<<setw(4)<<mathematics<<setw(4)

<<english<<setw(4)<<comscience<<setw(8)<<per<<setw(6)<<grade<<endl;

}

 

int  student::getIDNum() const

{

return idnum;

}

 

 

 

void SaveStudent();

void displayAll();

void Searchdisplay(int);

void modifyStudent(int);

void deleteStudent(int);

void DisplayClassResult();

void DisplayResult();

 

 

 

 

void write_student()

{

student st;

ofstream outFile;

outFile.open("student.dat",ios::binary|ios::app);

st.getdata();

outFile.write(reinterpret_cast<char *> (&st), sizeof(student));

outFile.close();

    cout<<"\n\nStudent record Has Been Created ";

cin.ignore();

cin.get();

}

 

 

 

void display_all()

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

cin.ignore();

cin.get();

return;

}

cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";

while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))

{

st.showdata();

cout<<"\n\n====================================\n";

}

inFile.close();

cin.ignore();

cin.get();

}

 

 

 

void display_sp(int n)

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

cin.ignore();

cin.get();

return;

}

bool flag=false;

while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))

{

if(st.getIDNum()==n)

{

  st.showdata();

flag=true;

}

}

inFile.close();

if(flag==false)

cout<<"\n\nrecord not exist";

cin.ignore();

cin.get();

}

 

 

void modify_student(int n)

{

bool found=false;

student st;

fstream File;

File.open("student.dat",ios::binary|ios::in|ios::out);

if(!File)

{

cout<<"File could not be open !! Press any Key...";

cin.ignore();

cin.get();

return;

}

    while(!File.eof() && found==false)

{

 

File.read(reinterpret_cast<char *> (&st), sizeof(student));

if(st.getIDNum()==n)

{

st.showdata();

cout<<"\n\nPlease Enter The New Details of student"<<endl;

st.getdata();

    int pos=(-1)*static_cast<int>(sizeof(st));

    File.seekp(pos,ios::cur);

    File.write(reinterpret_cast<char *> (&st), sizeof(student));

    cout<<"\n\n\t Record Updated";

    found=true;

}

}

File.close();

if(found==false)

cout<<"\n\n Record Not Found ";

cin.ignore();

cin.get();

}

 

 

 

void delete_student(int n)

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

cin.ignore();

cin.get();

return;

}

ofstream outFile;

outFile.open("Temp.dat",ios::out);

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

while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))

{

if(st.getIDNum()!=n)

{

outFile.write(reinterpret_cast<char *> (&st), sizeof(student));

}

}

outFile.close();

inFile.close();

remove("student.dat");

rename("Temp.dat","student.dat");

cout<<"\n\n\tRecord Deleted ..";

cin.ignore();

cin.get();

}

 

 

void class_result()

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

cin.ignore();

cin.get();

return;

}

cout<<"\n\n\t\tALL STUDENTS RESULT \n\n";

cout<<"==========================================================\n";

cout<<"R.No       Name        P   C   M   E   CS   %age   Grade"<<endl;

cout<<"==========================================================\n";

while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))

{

st.show_tabular();

}

cin.ignore();

cin.get();

inFile.close();

}

 

 

 

 

int main()

{

char ch;

int num;

cout.setf(ios::fixed|ios::showpoint);

cout<<setprecision(2); 

do

{

system("cls");

cout<<"\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";

cout<<"\n\n\t1.CREATE STUDENT RECORD";

cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORDS";

cout<<"\n\n\t3.SEARCH STUDENT RECORD ";

cout<<"\n\n\t4.MODIFY STUDENT RECORD";

cout<<"\n\n\t5.DELETE STUDENT RECORD";

cout<<"\n\n\t6. DISPLAY CLASS RESULT";

cout<<"\n\n\t7. EXIT";

cout<<"\n\n\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";

cout<<"\n\n\tPlease Enter Your Choice (1-7): ";

cin>>ch;

system("cls");

switch(ch)

{

case '1': write_student(); break;

case '2': display_all(); break;

case '3': cout<<"\n\n\tPlease Enter Student's ID number: "; cin>>num;

display_sp(num); break;

case '4': cout<<"\n\n\tPlease Enter Student's ID number: "; cin>>num;

modify_student(num);break;

case '5': cout<<"\n\n\tPlease Enter Student's ID number: "; cin>>num;

delete_student(num);break;

case '6' : class_result(); break;

case '7': exit(0);;

default: cout<<"\a"; 

    }

}while(ch!='7');

 

return 0;

}

Comments