C++ student management program
Write a C++ program to keep records and perform statistical analysis for a class of 20 students. The information of each student contains ID, Name, Sex, quizzes Scores (3 quizzes per semester), mid-term score, final score, and total score.
The program will prompt the user to choose the operation of records from a menu as shown below:
======
MENU
======
- Add student records
- Delete student records
- Update student records
- View all student records
- Calculate an average of a selected student’s scores
- Calculate total scores of a selected student
- Display the highest and lowest scores
- Sort students’ records by ID
- Sort students’ records by total score
Enter your choice:
Note: All students records store in an array of structures and this array is stored in a file called studentrecords.dat.
Source code:
#include <cstdlib>
#include <iostream>
#include<iomanip.h>
#include<fstream.h>
using namespace std;
struct student
{
int stnumber;
char stname[20];
char sex;
float quizz1;
float quizz2;
float assigment;
float midterm;
float final;
float total;
int numberOfitem;
};
student st[80];
int itemcount=0;
void displayheading();
void Showmax();
void Showmin();
void Sortbyid();
bool Searchduplicate(int);
//Menu contruction
void displaymenu(){
cout<"======"<"\n";
cout<" MENU "<"\n";
cout<"======"<"\n";
cout<" 1.Add student records"<"\n";
cout<" 2.Delete student records"<"\n";
cout<" 3.Update student records"<"\n";
cout<" 4.View all student records"<"\n";
cout<" 5.Sort student records by ID"<"\n";
cout<" 6.Sort student records by Total score"<"\n";
cout<" 7.Display average score of a selected student"<"\n";
cout<" 8.Display the highest and the lowest scores"<"\n";
cout<" 9.Search student by ID"<"\n";
}
void Add(){
again:
cout<"Enter student's ID(1-1000):";
cin>st[itemcount].stnumber;
if(Searchduplicate((int)st[itemcount].stnumber)==true){
cout<"This ID already exists\n";goto again;
}
cout<"\n";
cout<"Enter student's Name:";
cin>st[itemcount].stname;
cout<"\n";
cout<"Enter student's Sex(F or M):";cin>st[itemcount].sex;
cout<"\n";
cout<"Enter student's quizz1 score:";cin>st[itemcount].quizz1;
cout<"\n";
cout<"Enter student's quizz2 score:";cin>st[itemcount].quizz2;
cout<"\n";
cout<"Enter student's assigment score:";cin>st[itemcount].assigment;
cout<"\n";
cout<"Enter student's mid term score:";cin>st[itemcount].midterm;
cout<"\n";
cout<"Enter student's final score:";cin>st[itemcount].final;
st[itemcount].total=st[itemcount].quizz1+st[itemcount].quizz2+st[itemcount].assigment+st[itemcount].midterm+st[itemcount].final;
++itemcount;
}
bool Searchduplicate(int id){
bool match=false;
for(int i=0;i<itemcount;++i){
if(st[i].stnumber==id) match=true;}
return match;
}
void writedata(){
//save to studentrecords.data file
st[0].numberOfitem=itemcount;
fstream file("studentrecords.dat",ios::out);
file.write((char *)(&st),sizeof(st));
file.close();
}
void ViewAll(){
int i=0;
displayheading();
while(i<=itemcount){
if(st[i].stnumber!=0){
cout<left<setw(5)<st[i].stnumber<setw(20)<st[i].stname<setw(5)<st[i].sex;
cout<setw(5)<st[i].quizz1<setw(5)<st[i].quizz2<setw(5)<st[i].assigment
<setw(5)<st[i].midterm<setw(5)<st[i]. final<setw(5)
<st[i].total;
cout<"\n";}
i=i+1;
}
}
void Delete(){
int id;
cout<"Enter student's ID:";
cin>id;
for(int i=0;i<itemcount;++i){
if((st[i].stnumber==id)&(itemcount!=0)){
st[i].stnumber=0;
strcpy(st[i].stname,"0");
st[i].sex='0';
st[i].quizz1=0;
st[i].quizz2=0;
st[i].assigment=0;
st[i].midterm=0;
st[i].final=0;
st[i].total=0;
}
}
}
void Update(){
int id;
cout<"Enter student's ID:";
cin>id;
for(int i=0;i<itemcount;++i){
if((st[i].stnumber==id)&(itemcount!=0)){
cout<"Enter student's Name:";cin>st[i].stname;
cout<"Enter student's Sex:";cin>st[i].sex;
cout<"Enter quizz1 score:";cin>st[i].quizz1;
cout<"Enter quizz2 score:";cin>st[i].quizz2;
cout<"Enter assigment score:";cin>st[i].assigment;
cout<"Enter mid term score:";cin>st[i].midterm;
cout<"Enter final score:";cin>st[i].final;
st[i].total=st[i].quizz1+st[i].quizz2+st[i].assigment+st[i].midterm+st[i].final;
}
}
}
void Sortbyid(){
int i,j;
student temp;
for(i=0;i<itemcount;++i)
for(j=0;j<itemcount-1;++j)
if(st[j].stnumber>st[j+1].stnumber){
temp=st[j];st[j]=st[j+1];st[j+1]=temp;}
}
void Sortbytotal(){
int i,j;
student temp;
for(i=0;i<itemcount;++i)
for(j=0;j<itemcount-1;++j)
if(st[j].total>st[j+1].total){
temp=st[j];st[j]=st[j+1];st[j+1]=temp;}
}
void Displayaverage(){
int id;
float avg;
cout<"Enter student's ID:";
cin>id;
for(int i=0;i<itemcount;++i){
if(st[i].stnumber==id){
//st[i].total=st[i].quizz1+st[i].quizz2+st[i].assigment+st[i].midterm+st[i].final;
avg=st[i].total/5;
}
}
cout<"\nThe average score is:"<avg;
cout<"\n";
}
void DisplayHL(){
Showmax();
Showmin();
}
void Showmax(){
float max=st[0].total;
for(int i=0;i<itemcount;++i){
for(int j=0;j<itemcount;++j)
if(max<st[j].total) max=st[j].total;
}
cout<"The highest score is:"<max<"\n";
}
void Showmin(){
float min=st[0].total;
for(int i=0;i<itemcount;++i){
for(int j=0;j<itemcount;++j)
if(st[j].stnumber!=0)
if(min>st[j].total) min=st[j].total;
}
cout<"The highest score is:"<min<"\n";
}
void Searchbyid(){
int id;
cout<"Enter student's ID:";
cin>id;
for(int i=0;i<itemcount;++i)
if(st[i].stnumber==id){
displayheading();
cout<left<setw(5)<st[i].stnumber<setw(20)<st[i].stname<setw(5)<st[i].sex;
cout<setw(5)<st[i].quizz1<setw(5)<st[i].quizz2<setw(5)<st[i].assigment
<setw(5)<st[i].midterm<setw(5)<st[i]. final<setw(5)
<st[i].total;
cout<"\n";
}
}
void displayheading(){
cout<left<setw(5)<"ID"<setw(20)<"NAME"<setw(5)<"SEX"<setw(5)<"Q1"
<setw(5)<"Q2"<setw(5)<"As"<setw(5)<"Mi"<setw(5)<"Fi"
<setw(5)<"TOTAL"<"\n";
cout<"======\n";
}
//main method
int main(int argc, char *argv[])
{
//read from studentrecords.dat file
fstream file("studentrecords.dat",ios::in);
file.read((char *)(&st),sizeof(st));
file.close();
itemcount=st[0].numberOfitem;
//cout<"No of Students:"<itemcount<"\n";
//show menu
displaymenu();
int yourchoice;
char confirm;
do
{
cout<"Enter your choice(1-9):";
cin>yourchoice;
switch(yourchoice){
case 1:Add();break;
case 2:Delete();break;
case 3:Update();break;
case 4:ViewAll();break;
case 5:Sortbyid();break;
case 6:Sortbytotal();break;
case 7:Displayaverage();break;
case 8:DisplayHL();break;
case 9:Searchbyid();break;
default:cout<"invalid!\n";
}
cout<"Press y or Y to continue:";
cin>confirm;
}while(confirm=='y'||confirm=='Y');
cout<"Save?y or n:";
char c;
cin>c;
if(c=='y'||c=='Y')
writedata();
system("PAUSE");
return EXIT_SUCCESS;
}