这是类部份
class student
{
private:
char num[6],name[16];
double chi,math,eng,sum,avg;
public:
student(double c=0,double m=0,double e=0)
{
chi=c;
math=m;
eng=e;
}
void input()
{
cout<<"请输入学号,姓名和语文,数学,英语成绩(请按所提示的顺序输入):"<<endl;
cin>>num>>name;
cin>>chi;
while(cin.fail())
{
cout<<"你输入的不是数字!"<<endl;
cin.clear();
cin.sync();
cin>>chi;
}
cin>>math;
while(cin.fail())
{
cout<<"你输入的不是数字!"<<endl;
cin.clear();
cin.sync();
cin>>math;
}
cin>>eng;
while(cin.fail())
{
cout<<"你输入的不是数字!"<<endl;
cin.clear();
cin.sync();
cin>>eng;
}
sum=chi+math+eng;
avg=sum/3;
}
void ouput()
{
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"学号:"<<num<<"
姓名:"<<name<<"
成绩:
";
cout<<"语文:"<<chi<<"数学:"<<math<<"英语:"<<eng;
cout<<"总分:"<<sum<<"平均分:"<<avg<<endl;
cout<<"------------------------------------------------------------------------------"<<endl;
cout<<endl;
}
char *GetNum()
{
return num;
}
};
下面的是主函数和其他函数:
#include<iostream>
using namespace std;
#include"stu.h"
#include<cstdlib>
#include<fstream>
#include<cstring>
#include<conio.h>
//函数声明
void InputData();
void OutputData();
void locate();
void modify();
void del();
int i;// 循环变量
int y;//用来统计循环变量的次数
char n[6];//学号
student s1;//对象数组
int main()
{
char x;
while(true)
{
system("cls");
cout<<"
***********************************************"<<endl;
cout<<"
学生成绩管理系统"<<endl;
cout<<"
***********************************************"<<endl;
cout<<endl;
cout<<"
1--数据输入"<<endl;
cout<<"
2--数据输出"<<endl;
cout<<"
3--数据查询"<<endl;
cout<<"
4--数据修改"<<endl;
cout<<"
5--删除学生"<<endl;
cout<<"
0--退出"<<endl;
cout<<"请从(0--5)选择:"<<endl;
cin>>x;
if(x=='0'||x=='1'||x=='2'||x=='3'||x=='4'||x=='5')
{
if(x=='1')
InputData();
if(x=='2')
OutputData();
if(x=='3')
locate();
if(x=='4')
modify();
if(x=='5')
del();
if(x=='0')
{
cout<<"退出!"<<endl;
exit(1);
}
}
else
cout<<"你输入有误请重新选择!"<<endl;
getch();
}
return 0;
}
void InputData()//输入数据函数
{
char x;
ofstream outf;//输出流对象
outf.open("D:\\grade.dat",ios::out|ios::binary|ios::app);
if(outf.fail())
{
cout<<"打开文件失败!"<<endl;
}
for(i=0;i<100;i++){
y++;
s1.input();
outf.write((char *)&s1,sizeof(s1));
cout<<"是否(Y|N)还有其他学生信息的输入?"<<endl;
cin>>x;
if(x=='N'||x=='n')
break;
}
cout<<endl;
outf.close();
}
void OutputData()//输出数据函数
{
ifstream inf;//输入流对象
inf.open("D:\\grade.dat",ios::in|ios::binary);
if(!inf)
{
cout<<"打开文件失败!"<<endl;
}
else{
for(i=0;i<y;i++){
inf.read((char *)&s1,sizeof(s1));
s1.ouput();
}
}
inf.close();
}
void locate()//查询函数
{
ifstream inf;//输入流对象
inf.open("D:\\grade.dat",ios::in|ios::binary);
if(!inf)
cout<<"打开文件失败!"<<endl;
else
do{
cout<<"请输入学号查询:";
cin>>n;
inf.read((char *)&s1,sizeof(s1));
if(strcmp(n,s1.GetNum())==0)
{
s1.ouput();
}
else
cout<<"无此学生!"<<endl;
}while(!inf.eof());
inf.close();
}
void modify()//修改数据函数
{
fstream file_io;//输入输出流对象
file_io.open("D:\\grade.dat",ios::in|ios::out|ios::binary|ios::app);
if(file_io.fail())
cout<<"打开文件失败!"<<endl;
else
do{
cout<<"请输入要修改学生的学号:"<<endl;
cin>>n;
if(strcmp(n,s1.GetNum())==0)
{
file_io.clear();
s1.input();
}
else{
cout<<"此学生不存在!"<<endl;
break;
}
}while(!file_io.eof());
file_io.close();
}
void del()//删除数据函数
{}
请各位高手帮小弟改改!小弟不胜感激!