This article mainly introduces C++ Implementation of student management system example analysis , In this paper, the example code is introduced in detail , It has certain reference learning value for everyone's study or work , Friends in need, let's study together with Xiaobian .
1. Key points of the project
1、 Every time you enter the initial login interface , Because the contents of the file will be loaded again , So we need to put list underst and list ad Use of content in clear() The function is cleared and then read in .
2、 When the file is read , Due to the use !infile.eof() Function causes the last line to be read twice . therefore , Add... To the file reading loop infile.get(), The purpose is to wrap a line immediately after reading it .
2. Function is introduced
As can be seen from the above figure , This project has three functional modules : Open administrator account 、 Administrator function interface ( Login as Administrator )、 Undergraduate function interface ( Undergraduate province login ). The first time you run this project , First of all, the administrator account must be opened to carry out other operations .
2.1 Administrator function interface
As can be seen from the above figure, the administrator has five functions .
View all student information
View student information by name
Check student information by student number
Enter student information by student number
Delete student information
2.2 Student function interface
You can see from the picture above that students have two functions .
Check your personal information and change your password
It should be noted that , Before you log in to the student account, you must first log in to the administrator system to create student information, and then you can use the account to log in to the student interface .
3. code( The following codes are in VS2017 On the compiler run through )
/*Administer.h This document is Administer Class */
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Administer
{
private:
string name;
string ID;
string password;
public:
Administer() {}
Administer(string na, string id, string passw);
string get_id(){return ID;}
string get_name(){return name;}
string get_password(){ return password; }
void display();
};
/*Administer.cpp This document is Administer The realization of the class */
#include"Administer.h"
Administer::Administer(string na, string id, string passw) :name(na), ID(id), password(passw)
{}
void Administer::display()
{
cout << endl << "******************" << endl;
cout << endl << "* full name :" << name;
cout << endl << "* account number :" << ID;
cout << endl << "******************" << endl;
}
/*UnderStudent.h This document is UnderStuent Class */
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Understudent
{
private:
string name;
string ID;
string password;
float grade;
string sex;
public:
Understudent() {}
Understudent(string na, string id, string passw, float gra, string s);
string get_name(){return name;}
string get_id(){return ID;}
string get_password(){return password;}
float get_grade() { return grade; }
string get_sex() { return sex; }
void display();
bool operator == (const Understudent &u)const // Note that the parameter must be const type , Talent and STL in list The built-in functions match
{
return ID == u.ID;
}
void set_password(string passw)
{
password = passw;
}
};
/*UnderStudent.cpp This document is UnderStudent The realization of the class */
#include"UnderStudent.h"
Understudent::Understudent(string na, string id, string passw, float gra, string s):name(na),ID(id),password(passw),grade(gra), sex(s)
{}
void Understudent::display()
{
cout << "******************"<< endl;
cout << "* full name :" << name << endl;
cout << "* Student number :" << ID <<endl ;
cout << "* Gender :" << sex <<endl ;
cout << "* Grade point :" << grade << endl;
cout << "******************"<< endl;
}
/*System.h This document is System The header file */
#pragma once
#include<list>
#include<fstream>
#include"UnderStudent.h"
#include"Administer.h"
class System
{
private:
list<Understudent> underst;
list<Administer> ad;
static int underst_count;
static int ad_count;
public:
virtual void load_interface(); // Login interface
void exit_system(); // Exit the system
void understudent_functionshow(); // Student user interface
void administer_functionshow(); // Administrator function interface
void set_ad_account(); // Set up an administrator account
void enter_ad_account(); // Login as administrator
void enter_underst_account(); // Landing as an undergraduate
void save_undst(); // Save undergraduate data
void save_ad(); // Save administrator data
void load_undst(); // Reading undergraduate data
void load_ad(); // Read administrator data
/* Administrator function */
void input_underst_info(); // Enter undergraduate information
void look_all_underst(); // View all undergraduate information
void look_underst_by_name(string name); // Check undergraduate information by name
void look_underst_by_id(string id); // according to ID Check undergraduate information
void delete_underst_by_id(string id); // according to ID Delete undergraduate information
/* Undergraduate function */
void change_password(string id); // Change Password
};
/*System.cpp This document is System The realization of the class */
#include"System.h"
int System::underst_count = 0;
int System::ad_count = 0;
// Login interface
void System::load_interface()
{
int i;
do
{
system("cls");
load_ad();
load_undst();
cout << "********************" << endl;
cout << "1) Open administrator account !" << endl;
cout << "2) Login as administrator !" << endl;
cout << "3) Landing as an undergraduate !" << endl;
cout << "4) Exit the system !" << endl;
cout << "********************" << endl;
cout << " Please input the operation :";
cin >> i;
while (i < 1 || i>4)
{
cout << " Please enter the correct serial number !" << endl;
cout << " Please re-enter :";
cin >> i;
}
switch (i)
{
case 1:
set_ad_account();
break;
case 2:
enter_ad_account();
break;
case 3:
enter_underst_account();
break;
case 4:
exit_system();
break;
default:
break;
}
//cin.get();
} while (true);
}
// Exit the system
void System::exit_system()
{
cout << "**************** Thank you for using !******************" << endl;
exit(0);
}
// Undergraduate function interface
void System::understudent_functionshow()
{
cout << "***************************" << endl;
cout << "1) View personal information " << endl;
cout << "2) Change Password " << endl;
cout << "3) Go back to the previous menu !" << endl;
cout << "*****************************" << endl;
cout << " Please choose what you want to do :";
}
// Administrator function interface
void System::administer_functionshow()
{
cout << "***************************" << endl;
cout << "1) View all student information !" << endl;
cout << "2) Search for student information by name !" << endl;
cout << "3) Search student information by student number !" << endl;
cout << "4) Enter student information " << endl;
cout << "5) Delete student information by student number " << endl;
cout << "6) Go back to the previous menu !" << endl;
cout << "*****************************" << endl;
cout << " Please choose what you want to do :";
}
// Set up an administrator account
void System::set_ad_account()
{
string name;
string id;
string password;
string password2;
cout << endl<<" Please enter a name :";
cin >> name;
cout << endl << " Please enter ID:";
cin >> id;
cout << endl << " Please input a password :";
cin >> password;
cout << endl << " Please input the password again :";
cin >> password2;
while (password != password2)
{
cout << " The two passwords don't match , Please confirm again :";
cin >> password2;
}
Administer adm(name, id, password);
ad.push_back(adm);
cout << " Account opened successfully !" << endl;
cin.get();
ad_count++;
save_ad();
}
// Login as administrator
void System::enter_ad_account()
{
string udst_name; // The name of the student to be queried
string udst_id; // To check the student's ID
string id;
string passw;
list<Administer>::iterator iter;
cout << " Please enter the account :";
cin >> id;
int flag = 1;
for (iter = ad.begin(); iter != ad.end(); iter++)
{
if (id == iter->get_id())
{
flag = 0;
break;
}
}
if (flag)
{
cout << endl<<" Account does not exist !" << endl;
return;
}
cout << endl << " Please input a password :";
cin >> passw;
while (passw != iter->get_password())
{
cout << endl<<" Wrong password , Please re-enter :";
cin >> passw;
}
cin.get();
int n;
do
{
system("cls");
administer_functionshow();
cin >> n;
while (n < 1 || n>6)
{
cout << " Please enter the correct options :";
cin >> n;
}
switch (n)
{
case 1:
look_all_underst();
break;
case 2:
cout << " Please enter the name of the student you want to query :";
cin >> udst_name;
look_underst_by_name(udst_name);
break;
case 3:
cout << " Please input the... Of the student you want to query ID:";
cin >> udst_id;
look_underst_by_id(udst_id);
break;
case 4:
input_underst_info();
break;
case 5:
cout << " Please enter the... Of the student you want to delete ID:";
cin >> udst_id;
delete_underst_by_id(udst_id);
break;
case 6:
return;
break;
default:
break;
}
} while (1);
}
// Landing as an undergraduate
void System::enter_underst_account()
{
list<Understudent>::iterator iter;
string id;
string passw;
cout << " Please enter the account :";
cin >> id;
int flag = 1;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (id == iter->get_id())
{
flag = 0;
break;
}
}
if (flag)
{
cout << endl << " Account does not exist !" << endl;
return;
}
cout << endl << " Please input a password :";
cin >> passw;
while (passw != iter->get_password())
{
cout << endl << " Wrong password , Please re-enter :";
cin >> passw;
}
int n;
do
{
system("cls");
understudent_functionshow();
cin >> n;
while (n < 1 || n>3)
{
cout << endl << " Please enter the correct operation :";
cin >> n;
}
system("cls");
switch (n)
{
case 1:
iter->display();
break;
case 2:
change_password(id);
break;
case 3:
return;
break;
default:
break;
}
system("pause");
} while (true);
}
// Save administrator data
void System::save_ad()
{
ofstream outfile("administer.dat",ios::out);
list<Administer>::iterator iter;
outfile << ad_count << endl;
for (iter = ad.begin(); iter != ad.end(); iter++)
{
outfile << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_password() << endl;
}
outfile.close();
}
// Save undergraduate data
void System::save_undst()
{
ofstream outfile("understudent.dat",ios::out);
list<Understudent>::iterator iter;
outfile << underst_count << endl;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
outfile << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_password() << "\t" << iter->get_grade() << "\t"
<< iter->get_sex() << endl;
}
outfile.close();
}
// Reading undergraduate data
void System::load_undst()
{
ifstream infile("understudent.dat");
if (!infile)
{
cout << " No undergraduate information !" << endl;
return;
}
string name;
string ID;
string password;
float grade;
string sex;
infile >> underst_count;// Read the total number of undergraduates
infile.get();
if (!underst.empty())
underst.clear();
while (!infile.eof() && infile.peek() != EOF)
{
infile >> name >> ID >> password >> grade >> sex;
Understudent undst(name, ID, password, grade, sex);
underst.push_back(undst);
infile.get();
}
infile.close();
cout << " Reading undergraduate data is normal ." << endl;
}
// Read administrator data
void System::load_ad()
{
ifstream infile("administer.dat");
if (!infile)
{
cout << " No administrator information !" << endl;
return;
}
string name;
string ID;
string password;
infile >> ad_count;// Total number of read Administrators
infile.get();
if (!ad.empty())
ad.clear();
while (!infile.eof()||infile.peek()!=EOF)
{
infile >> name >> ID >> password;
Administer adm(name, ID, password);
ad.push_back(adm);
infile.get();
}
infile.close();
cout << " It is normal to read administrator data ." << endl;
}
/*
Administrator rights :
*/
//1) View all undergraduate information
void System::look_all_underst()
{
system("cls");
if (underst.empty())
{
cout << " No undergraduate data !" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
cout << " full name " << "\t" << "ID" << "\t" << "\t" <<" Gender " << "\t" << " Grade point " << endl;
for (iter = underst.begin(); iter != underst.end(); iter++)
cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
cout << endl << " The total number of students :" << underst_count << endl;
system("pause");
}
//2) View undergraduate data by name
void System::look_underst_by_name(string udst_name)
{
system("cls");
if (underst.empty())
{
cout << " No undergraduate data !" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_name() == udst_name)
{
cout << " full name " << "\t" << "ID" << "\t" << "\t" << " Gender " << "\t" << " Grade point " << endl;
cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
// Names can be repeated , So traverse all of
}
if (iter == --underst.end())
{
system("pause");
return;
}
}
cout << " There is no data to be born !" << endl;
system("pause");
return;
}
//3) Press ID Look at undergraduate data
void System::look_underst_by_id(string udst_id)
{
system("cls");
if (underst.empty())
{
cout << " No undergraduate data !" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_id()==udst_id)
{
cout << " full name " << "\t" << "ID" << "\t" << "\t" << " Gender " << "\t" << " Grade point " << endl;
cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
system("pause");
return; //ID There can be no repetition
}
}
cout << " There is no data to be born !" << endl;
system("pause");
return;
}
//4) Enter undergraduate information
void System::input_underst_info()
{
string name;
string ID;
string password;
float grade;
string sex;
char s; // Do you want to continue to enter flag
do
{
system("cls");
cout << endl << " Please enter the student's name :";
cin >> name;
cout << endl << " Please input the students ID:";
cin >> ID;
cout << endl << " Please enter the student's initial password :";
cin >> password;
cout << endl << " Please enter student grade point :";
cin >> grade;
cout <<endl<< " Please enter the student's gender :";
cin >> sex;
Understudent undst(name, ID, password, grade, sex);
underst.push_back(undst);
underst_count++;
cout << endl << " Do you want to continue to enter ?(Y/N)";
cin >> s;
while (s != 'Y'&&s != 'N'&&s != 'y'&&s != 'n')
{
cout << endl << " Please input the correct operation (Y/N):";
cin >> s;
}
} while (s == 'Y'||s=='y');
save_undst();
}
//5) Press ID Delete student information
void System::delete_underst_by_id(string udst_id)
{
system("cls");
if (underst.empty())
{
cout << " No undergraduate data !" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
string name;
string ID;
string password;
float grade;
string sex;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_id() == udst_id)
{
name = iter->get_name();
ID = iter->get_id();
password = iter->get_password();
grade = iter->get_grade();
sex = iter->get_sex();
Understudent undst(name, ID, password, grade, sex);
underst.remove(undst);
underst_count--;
cout << " Delete successful !" << endl;
system("pause");
save_undst();
return; //ID There can be no repetition
}
}
cout << " There is no data to be born !" << endl;
system("pause");
return;
}
/*
Undergraduate authority
*/
//2) Change Password
void System::change_password(string id)
{
string password, passw;
cout << " Please enter the new password :";
cin >> password;
cout <<endl<<" Please enter again :";
cin >> passw;
while (password != passw)
{
cout << endl<<" The two passwords don't match , Please re-enter :";
cin >> passw;
}
list<Understudent>::iterator iter;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_id() == id)
break;
}
iter->set_password(password);
cout << " Password changed successfully !" << endl;
save_undst();
}
/*mai.cpp This file is the main function file */
#include"System.h"
int main()
{
System s;
s.load_interface();
system("pause");
return 0;
}
Code directory structure
This is about C++ The article that realizes student management system example analysis introduced here . See here, are you right about “C++” I have a little new understanding ~
If you like this article , Move your little finger , Pay attention to it ~
Programming learning books :
Programming learning video :