Recently, some followers expressed interest in more content about C++. Today, let’s learn together how to write a simple student grade management system using C++. This case is particularly useful for those preparing for IT recruitment exams and for students who have completed the basics of C++. It mainly involves structures, global variables, and the definition and calling of functions.
Requirements:
-
There should be a main interface guiding the directory for users to understand the available functions.
-
Ability to query all grades.
-
Ability to add a new grade.
-
Ability to query existing grades in the system, with guidance if none exist.
-
Ability to delete existing grades in the system, with guidance if none exist.
-
Ability to modify existing grades in the system, with guidance if none exist.
-
Ability to exit the system.
The main directory interface is as follows:

Writing Process:
1. Import the required header files
#include<stdlib.h>#include<stdio.h>#include<string.h>#include<iostream>
2. Use the namespace, define the structure student, which contains an integer ID id and a randomly assigned ID rand_id, as well as a character array name to store the name. The double precision floating point variables chinese, math, and english store the scores for Chinese, Math, and English respectively. Declare a global variable structure array stu[100] to store the entire class’s grades, and rec to store the operation record position.
using namespace std;struct student{ int id,rand_id; char name[10]; double chinese,math,english; };struct student stu[100];int rec=0;
3. Define a function to query all grades
void print_data(){ cout<<"Random Code\tID\tName\tChinese\tMath\tEnglish"<<endl; for( int i=0;i<rec;i++) { cout<<stu[i].rand_id<<"\t"<<stu[i].id<<"\t"<<stu[i].name<<"\t"<<stu[i].chinese<<"\t"<<stu[i].math<<"\t"<<stu[i].english<<endl; }}
4. Define a function to add a grade
void append_data(){ cout<<"Please enter the data to be added\nID\tName\tChinese\tMath\tEnglish\n"; cin>>stu[rec].id>>stu[rec].name>>stu[rec].chinese>>stu[rec].math>>stu[rec].english; stu[rec].rand_id=rand()*(9999-999)+999+1; rec++; }
5. Define a function to query a specific grade
void find_data(){ char find_name[10]; int find_flg=0; cout<<"Please enter the name to query\n"; cin>>find_name; for(int i=0;i<rec;i++) { if( strcmp(stu[i].name,find_name)==0) { find_flg=1; cout<<"Random Code\tID\tName\tChinese\tMath\tEnglish"<<endl; cout<<stu[i].rand_id<<"\t"<<stu[i].id<<"\t"<<stu[i].name<<"\t"<<stu[i].chinese<<"\t"<<stu[i].math<<"\t"<<stu[i].english<<endl; } } if(find_flg==0) cout<<"The system does not have this data\n"; }
6. Define a function to delete a grade
void del_data(){ char del_name[10]; int del_flg=0; cout<<"Please enter the name to delete\n"; cin>>del_name; for (int i=0;i<rec;i++) { if( strcmp(stu[i].name,del_name)==0) { del_flg=1; for(;i<rec;i++) { stu[i].rand_id=stu[i+1].rand_id; stu[i].id=stu[i+1].id; strcpy(stu[i].name,stu[i+1].name); stu[i].chinese=stu[i+1].chinese; stu[i].math=stu[i+1].math; stu[i].english=stu[i+1].english; } rec--; break; } } if(del_flg==1) cout<<"Deletion successful\n"; else cout<<"The system does not have this data"; }
7. Define a function to update a grade
void update_data(){ char update_name[10]; int update_flg=0; cout<<"Please enter the name to modify\n"; cin>>update_name; for (int i=0;i<rec;i++) { if( strcmp(stu[i].name,update_name)==0) { update_flg=1; cout<<"Please enter the data to modify\nID\tName\tChinese\tMath\tEnglish\n"; cin>>stu[i].id>>stu[i].name>>stu[i].chinese>>stu[i].math>>stu[i].english; break; } } if(update_flg==1){cout<<"Modification successful\n";} else{cout<<"The system does not have this data\n";} }
8. Define a directory function to guide operations
void mulu(){ cout<<"Welcome to the Grade Management System\n"; cout<<"1. Query all grades\n"; cout<<"2. Add a grade\n"; cout<<"3. Query a grade\n"; cout<<"4. Delete a grade\n"; cout<<"5. Modify a grade\n"; cout<<"6. Exit the system\n"; }
9. Write the main function, calling the previously defined functions as needed
int main(){ mulu(); int num; while(1) { cout<<"Please enter the operation number 1-6:\n"; cin>>num; if(num==1) { cout<<"1. Query all grades\n"; print_data();} else if(num==2) { cout<<"2. Add a grade\n";append_data();} else if(num==3) { cout<<"3. Query a grade\n"; find_data();} else if(num==4) { cout<<"4. Delete a grade\n"; del_data();} else if(num==5) { cout<<"5. Modify a grade\n"; update_data();} else if(num==6) { cout<<"6. Exit the system\n"; break;} } //system("pause"); return 0;}
Demonstration of operation effects:
Extension: Interested friends can add a database for binding, so that data can be read and written normally to manage student grades. You can also try adding an interface to create a graphical version of the grade management system.
Friends who have finished watching the case, please give a thumbs up, and don’t forget to scan the code to help follow, your support is my motivation to continue pushing new cases.
