我在用C++编写一个学生信息管理程序,到添加学生这一步时就显示访问权限冲突了,是我的代码哪里不对吗?
题目要求:用面向对象的方法重新编写一个学生成绩管理程序。要求能添加、编辑、查找、删除学生有关信息。
#include<iostream>
#include <cstdlib>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
using namespace std;
class Student {
public:
void creat();
void add();
void edit();
void deleted();
void find();
void count();
void arrange();
void gradepage();
friend class menuscreen;
private:
Student* stu;//指针
int stunumber;//学生人数
string name;//姓名
unsigned id;//学号
float Math;//数学成绩
float Chinese;//语文成绩
float English;//英语成绩
float total;//总成绩
};
void menuscreen()//程序开始的菜单界面
{
cout << "学生信息管理程序\n";
cout << "*****************\n";
cout<<"1--创建成绩单\n";
cout << "2--添加学生\n";
cout << "3--编辑学生\n" ;
cout << "4--删除学生\n";
cout << "5--查找学生\n";
cout << "6--统计成绩\n";
cout << "7--排序成绩\n";
cout << "8--显示成绩\n";
cout << "9--Quit\n";
}
void Student::creat()//创建成绩单
{
int n;//学生人数
cout << "开始创建成绩单!\n";
cout << "Please enter the number of the student:";
cin >> n;
stunumber = n;
stu = new Student[stunumber];//使用new创建堆对象
for (int i = 0; i < stunumber; i++) {
cout << "please enter student" << i + 1<<"'s Name:";
cin >> stu[i].name;
cout << "please enter student" << i + 1 << "'s ID:";
cin >> stu[i].id;
cout << "please enter student" << i + 1 << "'s Math Score:";
cin >> stu[i].Math;
cout << "please enter student" << i + 1 << "'s Chinese Score:";
cin >> stu[i].Chinese;
cout << "please enter student" << i + 1 << "'s English Score:";
cin >> stu[i].English;
stu[i].total = stu[i].Math + stu[i].Chinese + stu[i].English;
}
system("pause");
}
void Student::add()
{
cout <<"开始添加学生!\n";
int i;
for (i = 0; i < stunumber; i++) {
if (stu[i].id == NULL)//寻找到没有录入学生的位置
break;
}
cout << "please enter student's Name:";//添加学生
cin >> stu[i].name;
cout << "please enter student's ID:";
cin >> stu[i].id;
cout << "please enter student's Math score:";
cin >> stu[i].Math;
cout << "please enter student's Chinese score:";
cin >> stu[i].Chinese;
cout << "please enter student's English score:";
cin >> stu[i].English;
cout << "录入完成!";
system("pause");
}
void Student::edit()
{
cout << "开始编辑学生!\n";
string str;
cout << "please enter the name of the student to edit:";
cin >> str;
int i;
for (i = 0; i < stunumber; i++)
{
if (name == stu[i].name)//寻找相同姓名的学生
{
break;
}
}
cout << "修改前学生的信息:" << endl;//输出修改前学生的信息
cout << stu[i].name << " " << stu[i].id << " " << stu[i].Math << " " << stu[i].Chinese << " " << stu[i].English << " " << stu[i].total;
cout << "please enter student's Name:";//编辑学生
cin >> stu[i].name;
cout << "please enter student's ID:";
cin >> stu[i].id;
cout << "please enter student's Math score:";
cin >> stu[i].Math;
cout << "please enter student's Chinese score:";
cin >> stu[i].Chinese;
cout << "please enter student's English score:";
cin >> stu[i].English;
stu[i].total = stu[i].Math + stu[i].Chinese + stu[i].English;
cout << "编辑完成!";
}
void Student::deleted()
{
cout << "开始删除学生!\n";
string str;
cout << "please enter the name of student to delete:";
cin >> str;
int num=0;//姓名对应的顺序位置
for (int i = 0; i < stunumber; i++)//寻找相同姓名的对应学生
{
if (str == stu[i].name)//当姓名相同时
{
num = i;
}
}
for (int j = num + 1; j < stunumber; j++)//将后面的学生向前移
{
stu[j - 1] = stu[j];
}
stunumber -= 1;//学生人数减少一个
}
void Student::find()
{
cout << "开始查找学生!\n";
string str;
cout << "please enter the name of the student to find:";
cin >> str;//输入学生姓名
int i;
for (i = 0; i < stunumber; i++)
{
if (name == stu[i].name)//找到学生姓名
{
break;
}
}
cout << stu[i].name << " " << stu[i].id << " " << stu[i].Math << " " << stu[i].Chinese << " " << stu[i].English << " " << stu[i].total;
}//输出学生信息
void Student::count()
{
cout << "开始统计成绩!\n";
int GreatMath = 0, GreatChinese = 0, GreatEnglish = 0;//统计优秀人数
for (int i = 0; i < stunumber; i++)
{
if (stu[i].Math >= 90)
GreatMath++;
if (stu[i].Chinese >= 90)
GreatChinese++;
if (stu[i].English >= 90)
GreatEnglish++;
}
if (GreatMath == 0)
{
cout<<"数学无人优秀\n";
}
else cout<<"数学优秀人数为"<< GreatMath<<"\n";
if (GreatChinese == 0)
{
cout<<"语文无人优秀\n";
}
else cout<<"语文优秀人数为"<<GreatChinese << "\n";
if (GreatEnglish == 0)
{
cout<<"英语无人优秀\n";
}
else cout<<" 英语优秀人数为"<<GreatEnglish << "\n";
}
void Student::arrange() {
int n;
cout << "开始排序成绩单!\n";
cout<<"1——按学号升序排列\n";
cout<<"2——按平均成绩升序排列\n";
cout<<"你需要如何排序:\n";
cin >> n;
if (n == 1)
{
for (int i = 0; i < stunumber - 1; i++)
{
for (int j = 0; j < stunumber - i - 1; j++)
{
if (stu[j + 1].id > stu[j].id)//比较相邻学生的学号大小
{
Student temp = stu[j + 1];
stu[j + 1] = stu[j];
stu[j] = temp;//交换学生位置
}
}
}
}
else if (n == 2)
{
for (int i = 0; i < stunumber - 1; i++)
{
for (int j = 0; j < stunumber - i - 1; i++)
{
if (stu[j].total<stu[j+1].total)//比较相邻学生的总成绩
{
Student temp = stu[j + 1];
stu[j + 1] = stu[j];
stu[j] = temp;//交换学生位置
}
}
}
}
if (n == 1 || n == 2)
{
cout<<"排序完成!";
}
}
void Student::gradepage()
{
cout<<"Gradepage!";
for (int i = 0; ; i++) {
if (id == 0)
break;
cout << "Name:" << stu[i].name<<"\n";
cout << "ID:" << stu[i].id<<"\n";
cout << "Math:" << stu[i].Math << "\n";
cout << "Chinese:" << stu[i].Chinese << "\n";
cout << "English:" << stu[i].English << "\n";
cout << "Total:" << stu[i].total << "\n";
}
}
int main() {
for(;;){
int s;
Student a;
menuscreen();
cout << "Please select a number:";//选择菜单编号
cin >> s;
switch (s)
{
case 1: a.creat(); break;
case 2: a.add(); break;
case 3: a.edit(); break;
case 4: a.deleted(); break;
case 5:a.find(); break;
case 6: a.count(); break;
case 7: a.arrange(); break;
case 8:a.gradepage(); break;
case 9:goto end; break;//引用标签end,如果选择9,则退出
default:cout << "error!"; break;
}
system("pause");
end:system("pause");//end的定义
}
}