我的程序可以运行但总有几个功能有问题,大家帮我看看错哪里了???
大家我的程序可以运行,可以输入储存,但是读取完信息后在显示就会出问题,是读取功能有问题嘛?? 而且没法计算平均成绩,排序也不能用...大家帮我找找错啊!!!
程序代码:
#include <iostream> #include <fstream> #include <string> #include <vector> #include <iomanip> #include <algorithm> #include <functional> #include <stdlib.h> using namespace std; class Student { public: Student(); virtual ~Student(); void Get_message(); /*输入函数*/ void Show_message(); /*输出函数*/ int num;//定义学号 string name;//定义姓名 string sex; int age; /*年龄*/ }; Student::Student() { num=0; name=""; sex=""; age=0; } Student::~Student(){} void Student::Get_message() { cout<<"请输入学生学号:"; cin>>num; cout<<"请输入学生姓名:"; cin>>name; cout<<"请输入学生性别:"; cin>>sex; cout<<"请输入学生年龄:"; cin>>age; } void Student::Show_message() { cout <<"\t\t 学号:" <<num <<endl; cout <<"\t\t 姓名:" <<name <<endl; cout <<"\t\t 性别:" <<sex <<endl; cout <<"\t\t 年龄:" <<age <<endl; } class benke:public Student /*本科生类*/ { public: benke(){} ~benke(){} string major; /*专业*/ int math; int english; int lisan; int chinese; float average; void compute();//计算平均成绩 void Get_message() /*输入函数*/ { Student::Get_message(); cout <<"请输入学生专业: "; cin>>major; /*输入其特有信息 专业*/ cout <<"请输入学生数学成绩: "; cin>>math; cout <<"请输入学生英语成绩: "; cin>>english; cout <<"请输入学生离散成绩: "; cin>>lisan; cout <<"请输入学生语文成绩: "; cin>>chinese; } void Show_message(); }; void benke::compute() { average=float(math+english+lisan+chinese)/4; } void benke::Show_message() { Student::Show_message(); cout <<"\t\t 专业: "<<major <<endl; cout <<"\t\t 数学成绩:" <<math <<endl; cout <<"\t\t 英语成绩:" <<english <<endl; cout <<"\t\t 离散成绩:" <<lisan <<endl; cout <<"\t\t 语文成绩:" <<chinese <<endl; cout <<"\t\t 平均成绩:" <<average <<endl; } class yanjiu:public Student /*本科生类*/ { public: yanjiu(){} ~yanjiu(){} string teacher; /*专业*/ int yanjiushengwu; int yanjiuenglish; int yanjiulixue; float average; void compute(); void Get_message() /*输入函数*/ { Student::Get_message(); cout <<"请输入学生导师: "; cin>>teacher; /*输入其特有信息 专业*/ cout <<"请输入学生研究生物成绩: "; cin>>yanjiushengwu; cout <<"请输入学生研究英语成绩: "; cin>>yanjiuenglish; cout <<"请输入学生研究力学成绩: "; cin>>yanjiulixue; } void Show_message(); }; void yanjiu::compute() { average=float(yanjiuenglish+yanjiulixue+yanjiushengwu)/3; } void yanjiu::Show_message() { Student::Show_message(); cout <<"\t\t 导师: "<<teacher <<endl; cout <<"\t\t 研究生物成绩:" <<yanjiushengwu<<endl; cout <<"\t\t 研究英语成绩:" <<yanjiuenglish <<endl; cout <<"\t\t 研究力学成绩:" <<yanjiulixue <<endl; cout <<"\t\t 平均成绩:" <<average <<endl; } class chengren:public Student /*本科生类*/ { public: chengren(){} ~chengren(){} string danwei; /*专业*/ int chengrenmath; int chengrenenglish; int chengrenhuaxue; float average; void compute(); void Get_message() /*输入函数*/ { Student::Get_message(); cout <<"请输入学生工作单位: "; cin>>danwei; /*输入其特有信息 专业*/ cout <<"请输入学生成人数学成绩: "; cin>>chengrenmath; cout <<"请输入学生成人英语成绩: "; cin>>chengrenenglish; cout <<"请输入学生成人化学成绩: "; cin>>chengrenhuaxue; cout<<"pingjunchengji"<<average<<endl; } void Show_message(); }; void chengren::compute() { average=float(chengrenmath+chengrenenglish+chengrenhuaxue)/3; } void chengren::Show_message() { Student::Show_message(); cout <<"\t\t 工作单位: "<<danwei <<endl; cout <<"\t\t 成人数学成绩:" <<chengrenmath <<endl; cout <<"\t\t 成人英语成绩:" <<chengrenenglish <<endl; cout <<"\t\t 成人化学成绩:" <<chengrenhuaxue<<endl; cout <<"\t\t 平均成绩:" <<average <<endl; } typedef vector<benke> BenKe; typedef vector<yanjiu> Yanjiu; typedef vector<chengren> Chengren; class Xitong /*操作管理系统*/ { public: BenKe mybenke; Yanjiu myyanjiu; Chengren mychengren; static int benkeSort(benke a, benke b); static int yanjiuSort(yanjiu a, yanjiu b); static int chengrenSort(chengren a, chengren b); Xitong(){}; ~Xitong(){}; void Show(int c); //显示信息 void save(); void Interface1(); //选择种类界面 void Interface(); /*功能主界面函数*/ void read(); bool del(); void paixu(); void In_information(); /*信息添加功能函数*/ }; void Xitong::Interface1() { cout <<"\t\t 1.本科生 " <<endl; cout <<"\t\t 2.研究生 " <<endl; cout <<"\t\t 3.成人继续教育生 " <<endl; cout <<"\t\t 4.返回上一级菜单 " <<endl; cout <<"\t\t 请您选择学生类别: " <<endl; } void Xitong::save() //储存信息 { ofstream fout("Student.txt",ios::app); ofstream fout1("fenshu.txt",ios::app); if(!fout||!fout1) { cout<<"Cannot open output file!\n,"; return; } for(unsigned int i=0; i<mybenke.size(); i++) { fout<<mybenke[i].num<<" "<<mybenke[i].name<<" "<<mybenke[i].sex<<" "<<mybenke[i].age<<" "<<mybenke[i].major<<" "<< mybenke[i].math<<" "<<mybenke[i].english<<" "<<mybenke[i].lisan<<" "<<mybenke[i].chinese<<" "<<mybenke[i].average<<" "<<endl; fout1<<mybenke[i].num<<" "<<mybenke[i].math<<" "<<mybenke[i].english<<" "<<mybenke[i].lisan<<" "<<mybenke[i].chinese<<" "<<endl; } for(i=0; i<myyanjiu.size(); i++) { fout<<myyanjiu[i].num<<" "<<myyanjiu[i].name<<" "<<myyanjiu[i].sex<<" "<<myyanjiu[i].age<<" "<<myyanjiu[i].teacher<<" "<< myyanjiu[i].yanjiushengwu<<" "<<myyanjiu[i].yanjiuenglish<<" "<<myyanjiu[i].yanjiulixue<<" "<<myyanjiu[i].average<<" "<<endl; fout1<<myyanjiu[i].num<<" "<<myyanjiu[i].yanjiushengwu<<" "<<myyanjiu[i].yanjiuenglish<<" "<<myyanjiu[i].yanjiulixue<<" "<<endl; } for(i=0; i<mychengren.size(); i++) { fout<<mychengren[i].num<<" "<<mychengren[i].name<<" "<<mychengren[i].sex<<" "<<mychengren[i].age<<" "<<mychengren[i].danwei<<" "<< mychengren[i].chengrenmath<<" "<<mychengren[i].chengrenenglish<<" "<<mychengren[i].chengrenhuaxue<<" "<<mychengren[i].average<<" "<<endl; fout1<<mychengren[i].num<<" "<<mychengren[i].chengrenmath<<" "<<mychengren[i].chengrenenglish<<" "<<mychengren[i].chengrenhuaxue<<" "<<endl; } fout.close(); fout1.close(); } void Xitong::In_information() //输入信息 { int q; // 定义字符型num int again=1; char t; benke b; yanjiu y; chengren c; Xitong s; while(again) { Interface1(); cin>>q; switch(q) { case 1: { b.Get_message(); mybenke.push_back(b); s.mybenke.push_back(b); break; } case 2: { y.Get_message(); myyanjiu.push_back(y); s.myyanjiu.push_back(y); break; } case 3: { c.Get_message(); mychengren.push_back(c); s.mychengren.push_back(c); break; } case 4: Interface(); break; default: cout <<" 输入错误!! " <<endl; continue; /*输入错误,给出提示*/ } cout <<"\t\t\t 信息储存成功!" <<endl; cout <<"\t\t\t 是否继续输入(y/n)? "; cin>>t; cout <<endl; if(!(t=='Y'||t=='y')) again=0; } Interface(); } void Xitong::Show(int c)//显示所有人信息 { if(c == 1)//本科生 { for(unsigned int i=0; i<mybenke.size(); i++) mybenke[i].Show_message(); } if(c == 2)//研究生 { for(unsigned int i=0; i<myyanjiu.size(); i++) myyanjiu[i].Show_message(); } if(c == 3)//成教生 { for(unsigned int i=0; i<mychengren.size(); i++) mychengren[i].Show_message(); } cout<<"显示完成!!"<<endl; Interface(); }
程序代码:
void Xitong::read() { ifstream file("Student.txt"); char data[10]; int size = 0; int i=0; file.getline(data,10); size = atoi(data); while(i++<size) { benke b; file.getline(data,10,' '); b.num =(int)atof(data); file.getline(data,10,' '); b.name = string(data); //读取名字 file.getline(data,10,' '); b.sex = string(data); //读取性别 file.getline(data,10,' '); b.age = (int)atof(data); file.getline(data,10,' '); b.major = string(data); //读取专业 file.getline(data,10,' '); b.math = (int)atof(data); file.getline(data,10,' '); b.english = (int)atof(data); file.getline(data,10,' '); b.lisan = (int)atof(data); file.getline(data,10,' '); b.chinese = (int)atof(data); mybenke.push_back(b); } i=0; file.getline(data,10); size = atoi(data); while(i++<size) { yanjiu y; file.getline(data,10,' '); y.num =(int)atof(data); file.getline(data,10,' '); y.name = string(data); //读取名字 file.getline(data,10,' '); y.sex = string(data); //读取性别 file.getline(data,10,' '); y.age = (int)atof(data); file.getline(data,10,' '); y.teacher = string(data); file.getline(data,10,' '); y.yanjiushengwu = (int)atof(data); file.getline(data,10,' '); y.yanjiuenglish = (int)atof(data); file.getline(data,10,' '); y.yanjiulixue = (int)atof(data); myyanjiu.push_back(y); } i=0; file.getline(data,10); size = atoi(data); while(i++<size) { chengren c; file.getline(data,10,' '); c.num =(int)atof(data); file.getline(data,10,' '); c.name = string(data);//读取名字 file.getline(data,10,' '); c.sex = string(data);//读取性别 file.getline(data,10,' '); c.age = (int)atof(data); file.getline(data,10,' '); c.danwei = string(data);//读取专业 file.getline(data,10,' '); c.chengrenmath = (int)atof(data); file.getline(data,10,' '); c.chengrenenglish = (int)atof(data); file.getline(data,10,' '); c.chengrenhuaxue = (int)atof(data); mychengren.push_back(c); } cout<<"读取成功"<<endl; Interface(); } bool Xitong::del() { cout<<"请输入要删除的学生姓名:"; int c = 0; string name; cin>>name; for(unsigned int i=0; i<mybenke.size(); i++) { if(mybenke[i].name== name) { c++; mybenke.erase(mybenke.begin() + i); } } for(i=0; i<myyanjiu.size(); i++) { if(myyanjiu[i].name== name) { c++; myyanjiu.erase(myyanjiu.begin() + i); } } for(i=0; i<mychengren.size(); i++) { if(mychengren[i].name== name) { c++; mychengren.erase(mychengren.begin() + i); } } if(c == 0) cout<<"对不起,没有这个学生!!"<<endl; else cout<<"删除成功"<<endl; return 1; } void Xitong::paixu() { Xitong s; vector<Student> stu; int q; // 定义字符型num int again=1; char t; benke b; yanjiu y; chengren c; while(again) { Interface1(); cin>>q; switch(q) { case 1: { sort(mybenke.begin(),mybenke.end(),benkeSort); cout<<"排序后:"<<endl;//显示排序后的本科生 s.Show(1); break; } case 2: { sort(myyanjiu.begin(),myyanjiu.end(),yanjiuSort); cout<<"排序后:"<<endl; s.Show(2); break; } case 3: { sort(mychengren.begin(),mychengren.end(),chengrenSort); cout<<"排序后:"<<endl; s.Show(3); break; } case 4: Interface(); break; default: cout <<" 输入错误!! " <<endl; continue; /*输入错误,给出提示*/ } cout <<"\t\t\t 是否继续输入(y/n)? "; cin>>t; cout <<endl; if(!(t=='Y'||t=='y')) again=0; } Interface(); } int Xitong::benkeSort(benke a, benke b) { return a.average>b.average; } int Xitong::yanjiuSort(yanjiu a, yanjiu b) { return a.average>b.average; } int Xitong::chengrenSort(chengren a, chengren b) { return a.average>b.average; } void Xitong::Interface() { int rev; cout <<"\n\n\n\n\n\n\n"; cout <<"\t\t ******************欢迎使用******************" <<endl; cout <<"\t\t **************学生成绩管理系统**************" <<endl; cout <<"\t\t 1.添加学生信息 " <<endl; cout <<"\t\t 2.显示学生信息 " <<endl; cout <<"\t\t 3.删除学生信息 " <<endl; cout <<"\t\t 4.读取文件信息 " <<endl; cout <<"\t\t 5.排序 " <<endl; cout <<"\t\t 6.存储信息 " <<endl; cout <<"\t\t 7.计算平均成绩 " <<endl; cout <<"\t\t 8.退出 " <<endl; cout <<"\t\t 请您选择(1~5): " <<endl; cin>>rev; switch(rev) { case 1: In_information(); break; case 2: Show(1); Show(2); Show(3); break; case 3: del(); Interface(); break; case 4: read(); break; case 5: paixu(); Interface(); break; case 6: save(); Interface(); break; case 7: benke b; (); yanjiu y; (); chengren c; (); break; exit(0); } Interface(); } void main() { Xitong s; s.Interface(); }