Completely Solved C, C++ Programs Assignment.




C++ program to create a student database

Filed Under:

Problem Statement :
Write a C++ program to create a student database,student records should be written into a file and should be read from that file whenever information is needed.

Logical Approach :
The above program is done using an Abstract Data Type (ADT) called class. A class is the mechanism to create objects. In the program,a class named student is created. All the student records that are entered by user are stored in a file Student.dat(We can give any name) so that the records are not lost after the program is executed.

It contains the following data members :
 roll_no
• name[]
• dept_name[]
• marks[]

It contains the following member functions :
• get_input()- It is used to get all the inputs.
• print_data() - It is used to print all the information.
• calculate()- This function is used to calculate a students grade on the following basis:

Calculation of grade :
1. If average marks is greater than or equal to 90 then grade = EX and print “High first class with distinction”.

2. If average marks is greater than or equal to 80 but less than 90 then grade = A and print “High first class”.

3. If average marks is greater than or equal to 70 but less than 80 then grade = B and print “First class”.

4. If average marks is greater than or equal to 60 but less than 70 then grade = C and print “Second class”.

5. If average marks is greater than or equal to 50 but less than 60 then grade = D and print “Third class”.

6. Else the student is fail.

 filein()-It is used to read the student records from file Student.dat .
 fileout()-It is used to write the student records into file Student.dat. If previous data exists
new records are appended.
 countstudent()- It is used to count number of students recorded in the file.

User can insert number of student records according to his or her choice which are written into the file Student.dat. The file is opened in append mode so that previous data(if exists) are not deleted and new records are inserted at the end. After the calculation of average mark and grade for each student, the student records are read from the file and are printed in the reverse order.

Algorithm :
Creating an ADT with name student

Step1: Creating an ADT student

Step2: Declaring private Data members
int roll_no;//Declaring an integer
char name[20];//Declaring a character array of size 20
char dept_name[40];//Declaring a character array of size 40
int marks[5];//Declaring a character array of size 5

Step3: Declaring public member functions
get_input();
calculate();
print_data();
filein();
fileout();
countstudent()


This function is used to get all the inputs

get_input()
{
Read roll_no;
Read name;
Read dept_name;
for(int i=1 to 5)
Read marks[i];

}

This function is used to calculate a student's grade

calculate()
{
for(i=1 to 5)
sum=sum+marks[i];
avg=sum/5;
if(avg>=90)
Print EX, High 1st Class with ***DISTINCTION***;
else if(avg<90 AND avg>=80)
Print A,High 1st Class;
else if(avg<80 AND avg>=70)
Print B,1st Class;
else if(avg<70 AND avg>=60)
Print C,2nd Class;
else if(avg<60 AND avg>=50)
Print D,3rd Class;
else
Print Fail;
}

This function is used to print all the information

print_data()

{
Print roll_no;
Print name;
Print dept_name;
for(int i=1 to 5)
Print marks[i];
call calculate() to print grade;

}

This function is used to read the student records from file Student.dat

filein()
{
Create a stream using ifstream;
Open the file Student.dat in binary mode;
Seek get(input) pointer to specified record ;
read the specified record;
}

This function is used to write the student records into file Student.dat. If previous data exists new records are appended

fileout()
{
Create a stream using ofstream;
Open the file Student.dat in binary mode and append mode ;
get_input();
write the student record in the file;
}

This function is used to count number of students recorded in the file

countstudent()
{
Create a stream using ifstream;
Open the file Student.dat in binary mode;
Seek get(input) pointer to end of the file;
Divide current position of the get pointer by size of a student record and return the value ;
}

Main Procedure

main()
{
student s;
do
{
Print "Enter data for student";
s.fileout();
Print "Enter another(Y/N):";
Read choice in ch;
}while(ch='Y' OR ch==y');
n=s.countstudent();
for i=n-1 to 0)
{
s.filein(i);
s.print_data();
}
}

Program listing :
//C++ program to create a student database,student records should be written into a file and should be read from that file whenever information is needed

#include<iostream>
#include<fstream>

using namespace std;
//Creating a class student
class student
{
private:
int roll_no;
char name[100];
char dept_name[100];
int marks[5];
public:
void get_input();
void calculate();
void print_data();
void filein(int);
void fileout();
int countstudent();
};
//This function is used to read the student records from file Student.dat
void student::filein(int pn)
{
ifstream infile;
infile.open("Student.dat",ios::binary);
infile.seekg(pn*sizeof(student));
infile.read((char*) this,sizeof(*this));
}
//This function is used to write the student records into file Student.dat. If previous data exists new records are appended
void student::fileout()
{
ofstream outfile;
outfile.open("Student.dat",ios::app|ios::binary);
get_input();
outfile.write((char*)this,sizeof(*this));
}
//This function is used to count number of students recorded in the file
int student::countstudent()
{
ifstream infile;
infile.open("Student.dat",ios::binary);
infile.seekg(0,ios::end);
int n=infile.tellg()/sizeof(student);
return n;
}
//This function is used to get all the inputs
void student::get_input()
{
cout<<"\nEnter roll-no:";
cin>>roll_no;
cout<<"\nEnter name:";
getchar();
cin.getline(name,100);
cout<<"\nEnter department name:";
cin.getline(dept_name,100);
cout<<"\nEnter marks in Hons. Paper 1:";
cin>>marks[0];
cout<<"\nEnter marks in Hons. Paper 2:";
cin>>marks[1];
cout<<"\nEnter marks in Pass Paper 1:";
cin>>marks[2];
cout<<"\nEnter marks in Pass Paper 2:";
cin>>marks[3];
cout<<"\nEnter marks in Compulsury Paper:";
cin>>marks[4];
for(int i=0;i<5;i++)
{
if(marks[i]>100 || marks[i]<0)
{
cout<<"Invalid input marks given!!!!!\n";
system("PAUSE");
exit(0);

}
}
}
//This function is used to calculate a student's grade
void student::calculate()
{
int sum=0,i,avg;
for(i=0;i<5;i++)
sum+=marks[i];
avg=sum/5;
cout<<"\n\n"<<"grade:";
if(avg>=90)
cout<<"EX,"<<"High 1st Class***";
else if(avg<90 && avg>=80)
cout<<"A,"<<"High 1st Class";
else if(avg<80 && avg>=70)
cout<<"B,"<<"1st Class";
else if(avg<70 && avg>=60)
cout<<"C,"<<"2nd Class";
else if(avg<60 && avg>=50)
cout<<"D,"<<"3rd Class";
else
cout<<"Fail!!!!!";
cout<<"\n..........";
}
//This function is used to print all the information
void student::print_data()
{
cout<<"\n\nRoll:"<<roll_no;
cout<<"\n\nName:"<<name;
cout<<"\n\nDepartment:"<<dept_name;
cout<<"\n\nMarks in Hons. Paper 1:"<<marks[0];
cout<<"\n\nMarks in Hons. Paper 2:"<<marks[1];
cout<<"\n\nMarks in Pass Paper 1:"<<marks[2];
cout<<"\n\nMarks in Pass Paper 2:"<<marks[3];
cout<<"\n\nMarks in Compulsury Paper:"<<marks[4];
calculate();
}


int main()
{
student s;
int n;
char ch;
do
{
cout<<"Enter data for student\n-----------------------------------------\n";
s.fileout();//Read from user and write to file
cout<<"Enter another(Y/N):";
cin>>ch;
}while(ch=='Y'||ch=='y');
n=s.countstudent();
for(int i=n-1;i>=0;i--)
{
cout<<"\n\nStudent "<<i+1<<"\n----------------";
s.filein(i);//Read from file
s.print_data();//Print records in reverse order
}
return 0;
}


Output :
Enter data for student
-----------------------------------------

Enter roll-no:190
Enter name:Vijay Malakar
Enter department name:History
Enter marks in Hons. Paper 1:45
Enter marks in Hons. Paper 2:55
Enter marks in Pass Paper 1:67
Enter marks in Pass Paper 2:65
Enter marks in Compulsury Paper:43

Enter another(Y/N):y
Enter data for student
-----------------------------------------

Enter roll-no:230
Enter name:Amar Malhotra
Enter department name:English
Enter marks in Hons. Paper 1:78
Enter marks in Hons. Paper 2:76
Enter marks in Pass Paper 1:23
Enter marks in Pass Paper 2:33
Enter marks in Compulsury Paper:44

Enter another(Y/N):y
Enter data for student
-----------------------------------------

Enter roll-no:345
Enter name:Lalita Saha
Enter department name:Mathematics
Enter marks in Hons. Paper 1:65
Enter marks in Hons. Paper 2:89
Enter marks in Pass Paper 1:46
Enter marks in Pass Paper 2:76
Enter marks in Compulsury Paper:43

Enter another(Y/N):n


Student 3
----------------

Roll:345

Name:Lalita Saha
Department:Mathematics
Marks in Hons. Paper 1:65
Marks in Hons. Paper 2:89
Marks in Pass Paper 1:46
Marks in Pass Paper 2:76
Marks in Compulsury Paper:43
grade:C,2nd Class
..........

Student 2
----------------

Roll:230

Name:Amar Malhotra

Department:English

Marks in Hons. Paper 1:78

Marks in Hons. Paper 2:76

Marks in Pass Paper 1:23

Marks in Pass Paper 2:33

Marks in Compulsury Paper:44

grade:D,3rd Class
..........

Student 1
----------------

Roll:190

Name:Vijay Malakar

Department:History

Marks in Hons. Paper 1:45

Marks in Hons. Paper 2:55

Marks in Pass Paper 1:67

Marks in Pass Paper 2:65

Marks in Compulsury Paper:43

grade:D,3rd Class
..........


Discussions :
&#61548; Namespaces allow to group entities like classes,objects and functions under a name. This way the global scope can be divided in “sub-scopes”,each one with its own name.

&#61548; All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.

&#61548; We can open files using constructor or by using open() function. The syntax is:
file-stream-class stream-object;
stream-object.open(“filename”,mode);

&#61548; The file mode parameter can take one or more parameters defined in the class ios.
Ios::app is the append mode which allows us to add data to the end of the file only. In this case,a file is created by the specified name if it does not exist.

&#61548; The mode can combine two or more parameters using the bitwise OR operator.

&#61548; The functions read() and write() handle the data in binary form. The binary format is more accurate for storing records as they are stored in the exact internal representation




Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments