求助,不知道程序哪里错了。题目是:编一个等级考试报名
题目是17. 等级考试报名系统
近年来,参加计算机考试的人员日益增多,用计算机来降低工作量成为了一种迫切需求,为此目的编写软件
要求:
报名信息维护
等级分类
等级查询
身份证号查询
姓名查询
学号查询
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
int r2=0,r4=0,r6=0;
struct infor *creat();
struct infor
{
string name;
string rank;
string idnumber;
string number;
struct infor *next;
};
class menu
{
public:
void interface();
void baoming();
void tuibao();
void chaxun();
void chaxun_id();
void chaxun_name();
void chaxun_rank();
void chaxun_number();
void rank_rank();
};
struct infor * creat()
{
ifstream fin("infor.txt",ios::in|ios::binary);
if(!fin)
{
cerr<<"输入文件打开失败"<<endl;
exit(2);
}
struct infor *p3,*p4,*head;
head=NULL;
p3=p4=new infor;
p3->next=NULL;
if(!fin.eof())
{
fin.read((char*)&p3->name,sizeof(p3->name));
fin.read((char*)&p3->rank,sizeof(p3->rank));
fin.read((char*)&p3->idnumber,sizeof(p3->idnumber));
fin.read((char*)&p3->number,sizeof(p3->number));
if(head=NULL)
head=p3;
else
p4->next=p3;
p4=p3;
p3=new infor;
}
p4->next=NULL;
fin.close();
return head;
}
void menu::baoming()
{
struct infor *p1;
system("cls");
p1=new infor;
cout<<"请输入你的姓名:";
cin>>p1->name;
system("cls");
int a,b=0,c=0,d=0;
do
{
a=1;
cout<<"请选择报考等级:"<<endl;
cout<<"2级请按2,4级请按4,6级请按6."<<endl;
char c1;
cin>>c1;
switch(c1)
{
case '2':p1->rank="2级";r2++;b=1;break;
case '4':p1->rank="4级";r4++;c=1;break;
case '6':p1->rank="6级";r6++;d=1;break;
default:cout<<"输入有误,请重新输入!!!"<<endl;a=0;break;
}
}while(a!=1);
system("cls");
cout<<"请输入你的身份证号:";
cin>>p1->idnumber;
system("cls");
cout<<"请输入你的学号:";
cin>>p1->number;
char c2;
do
{
system("cls");
cout<<"请确认信息是否正确,正确请按y,错误请按n。"<<endl;
cout<<"姓名:"<<p1->name<<endl;
cout<<"报考等级:"<<p1->rank<<endl;
cout<<"身份证号:"<<p1->idnumber<<endl;
cout<<"学号:"<<p1->number<<endl;
cin>>c2;
int a2;
switch(c2)
{
case 'Y':
case 'y':cout<<"报名成功"<<endl;interface();break;
case 'N':
case 'n':do
{
a2=1;
cout<<"请选择修改信息,按1修改姓名,按2修改报考等级,按3修改身份证号,按4修改学号."<<endl;
char c3;
cin>>c3;
switch(c3)
{
case '1':cout<<"请输入你的姓名:";
cin>>p1->name;break;
case '2':cout<<"2级请按2,4级请按4,6级请按6"<<endl;
if(b==1)
r2--;
if(c==1)
r4--;
if(d==1)
r6--;
do
{
a=1;
char c1;
cin>>c1;
switch(c1)
{
case '2':p1->rank="2级";r2++;break;
case '4':p1->rank="4级";r4++;break;
case '6':p1->rank="6级";r6++;break;
default:cout<<"输入有误,请重新输入!!!"<<endl;a=0;break;
}
}while(a!=1);break;
case '3':cout<<"请输入你的身份证号:";
cin>>p1->idnumber;break;
case '4':cout<<"请输入你的学号:";
cin>>p1->number;break;
default:cout<<"输入有误,请重新输入!!!"<<endl;a2=0;break;
}
}while(a2!=1);
break;
}
}while(c2=='Y'||c2=='y');
ofstream fout("infor.txt",ios::app|ios::binary);
if(!fout)
{
cerr<<"输入文件打开失败"<<endl;
exit(1);
}
fout.write((char*)&p1->name,sizeof(p1->name));
fout.write((char*)&p1->rank,sizeof(p1->rank));
fout.write((char*)&p1->idnumber,sizeof(p1->idnumber));
fout.write((char*)&p1->number,sizeof(p1->number));
fout.close();
}
void menu::tuibao()//退考
{
struct infor *p2,*head;
head=p2=new infor;
head=creat();
cout<<"请输入你的姓名:";
cin>>p2->name;
cout<<"请输入报考等级:";
cin>>p2->rank;
cout<<"请输入身份证号:";
cin>>p2->idnumber;
cout<<"请输入学号:";
cin>>p2->number;
int a5=1,k=0;
ifstream fin("infor.txt",ios::in|ios::binary);
if(!fin)
{
cerr<<"输入文件打开失败"<<endl;
exit(2);
}
if(head==NULL)
cout<<"该人并没有报考"<<endl;
else
{
struct infor *p5,*p6;
p5=p6=new infor;
p5=head;
while(p5!=p2&&p5->next!=NULL)
{
p6=p5;
p5=p5->next;
}
if(p5==p2)
{
k=1;
if(p5==head)
{
cout<<"姓名:"<<p2->name<<endl;
cout<<"报考等级:"<<p2->rank<<endl;
if(p2->rank=="2级")
r2--;
if(p2->rank=="4级")
r4--;
if(p2->rank=="6级")
r6--;
cout<<"身份证号:"<<p2->idnumber<<endl;
cout<<"学号:"<<p2->number<<endl;
cout<<"该人已退考"<<endl;
head=p5->next;
}
else
{
p6->next=p5->next;
p5->next=NULL;
cout<<"姓名:"<<p2->name<<endl;
cout<<"报考等级:"<<p2->rank<<endl;
if(p2->rank=="2级")
r2--;
if(p2->rank=="4级")
r4--;
if(p2->rank=="2级")
r6--;
cout<<"身份证号:"<<p2->idnumber<<endl;
cout<<"学号:"<<p2->number<<endl;
cout<<"该人已退考"<<endl;
}
}
else
{
if(p5->next==NULL)
{
cout<<"该人并没有报考"<<endl;
cout<<"请确认信息是否正确,正确请按y,错误请按n。"<<endl;
cout<<"姓名:"<<p2->name<<endl;
cout<<"报考等级:"<<p2->rank<<endl;
cout<<"身份证号:"<<p2->idnumber<<endl;
cout<<"学号:"<<p2->number<<endl;
char c4;
cin>>c4;
int a3;
switch(c4)
{
case 'Y':
case 'y':cout<<"该人没有报考"<<endl;
delete [] p5;
delete [] p6;
break;
case 'N':
case 'n':do
{
a3=1;
cout<<"请选择修改信息,按1修改姓名,按2修改报考等级,按3修改身份证号,按4修改学号."<<endl;
char c5;
cin>>c5;
switch(c5)
{
case '1':cin>>p2->name;break;
case '2':cin>>p2->rank;break;
case '3':cin>>p2->idnumber;break;
case '4':cin>>p2->number;break;
default:cout<<"输入有误,请重新输入!!!"<<endl;a3=0;break;
}
}while(a3!=1);
break;
}
}
struct infor *p7,*p8;
p7=p8=new infor;
p7=head;
while(p7!=p2&&p7->next!=NULL)
{
p8=p7;
p7=p7->next;
}
if(p7==p2)
{
k=1;
if(p7==head)
{
cout<<"姓名:"<<p2->name<<endl;
cout<<"报考等级:"<<p2->rank<<endl;
if(p2->rank=="2级")
r2--;
if(p2->rank=="4级")
r4--;
if(p2->rank=="2级")
r6--;
cout<<"身份证号:"<<p2->idnumber<<endl;
cout<<"学号:"<<p2->number<<endl;
cout<<"该人已退考"<<endl;
head=p7->next;
}
else
{
p8->next=p7->next;
p7->next=NULL;
cout<<"姓名:"<<p2->name<<endl;
cout<<"报考等级:"<<p2->rank<<endl;
cout<<"报考等级:"<<p2->rank<<endl;
if(p2->rank=="2级")
r2--;
if(p2->rank=="4级")
r4--;
if(p2->rank=="2级")
r6--;
cout<<"身份证号:"<<p2->idnumber<<endl;
cout<<"学号:"<<p2->number<<endl;
cout<<"该人已退考"<<endl;
}
}
else
{
if(p7->next==NULL)
{
cout<<"该人并没有报考"<<endl;
}
}
}
}
if(k==1)
{
ofstream fout("infor.txt",ios::trunc|ios::binary);
if(!fout)
{
cerr<<"输入文件打开失败"<<endl;
exit(1);
}
struct infor *p9;
p9=head;
while(p9->next!=NULL)
{
fout.write((char*)&p9->name,sizeof(p9->name));
fout.write((char*)&p9->rank,sizeof(p9->rank));
fout.write((char*)&p9->idnumber,sizeof(p9->idnumber));
fout.write((char*)&p9->number,sizeof(p9->number));
p9=p9->next;
}
fout.close();
}
}
void menu::chaxun()//因为字符字数限制,2和3的功能删了
{
int a;
char c1;
do
{
a=1;
cout<<"请选择查询信息,按1通过姓名查询,按2通过身份证号查询,按3通过学号查询,按4查询本次所有考生信息,按5查询本次可报考等级."<<endl;
cin>>c1;
switch(c1)
{
case '1':chaxun_name();break;
case '2':chaxun_id();break;
case '3':chaxun_number();break;
case '4':rank_rank();break;
case '5':chaxun_rank();break;
default:cout<<"输入数据有误,请重新输入"<<endl;
a=0;system("cls");break;
}
}while(a!=1);
}
void menu::chaxun_name()//用名字来查询考生信息
{
string name;
cout<<"请输入你要查询的姓名:";
cin>>name;
struct infor *p1;
p1=creat();
while(p1->name!=name&&p1->next!=NULL)
p1=p1->next;
if(p1->name==name)
{
cout<<"姓名:"<<p1->name<<endl;
cout<<"报考等级:"<<p1->rank<<endl;
cout<<"身份证号:"<<p1->idnumber<<endl;
cout<<"学号:"<<p1->number<<endl;
}
else
{
system("cls");
char c2;
cout<<"此人并没有报考!!!"<<endl;
cout<<"是否要报考,报考请按Y,不报考按N."<<endl;
cin>>c2;
switch(c2)
{
case 'Y':
case 'y':baoming();break;
default:break;
}
}
delete [] p1;
}
void menu::rank_rank()//用来告诉每个等级有谁谁谁参加和参加人数
{
struct infor *p1,*p;
struct infor a[r2],b[r4],c[r6];
int d=0,e=0,f=0;
p1=p=new infor;
p1=creat();
while(p1->next!=NULL)
{
if(p1->rank=="2级")
{
a[d++]=*p1;
p1=p1->next;
}
if(p1->rank=="4级")
{
b[e++]=*p1;
p1=p1->next;
}
if(p1->rank=="6级")
{
c[f++]=*p1;
p1=p1->next;
}
}
cout<<"报考2级的有"<<r2<<"人,详细信息如下:"<<endl;
for(int i=0;i<r2;i++)
{
*p=a[i];
cout<<"姓名:"<<p->name<<endl;
//cout<<"身份证号:"<d[i]->idnumber<<endl;
cout<<"学号:"<<p->number<<endl;
}
cout<<"报考4级的有"<<r4<<"人,详细信息如下:"<<endl;
for(int i=0;i<r4;i++)
{
*p=b[i];
cout<<"姓名:"<<p->name<<endl;
//cout<<"身份证号:"<<e[i]->idnumber<<endl;
cout<<"学号:"<<p->number<<endl;
}
cout<<"报考6级的有"<<r6<<"人,详细信息如下:"<<endl;
for(int i=0;i<r6;i++)
{
*p=c[i];
cout<<"姓名:"<<p->name<<endl;
//cout<<"身份证号:"<<f[i]->idnumber<<endl;
cout<<"学号:"<<p->number<<endl;
}
delete [] p;
delete [] p1;
}
void menu::chaxun_rank()
{
cout<<"本次等级考试有2级,4级,6级"<<endl;
}
void menu::interface()
{
system("cls");
int k,l;
do
{
k=1;
char c1;
cout<<"欢迎进入计算机等级考试报名系统"<<endl;
cout<<"按1进行报名,按2进行退报,按3进行信息查询,按4退出本系统"<<endl;
do
{
l=1;
cin>>c1;
switch(c1)
{
case '1':baoming();break;
case '2':tuibao();break;
case '3':chaxun();break;
case '4':k=0;break;
default:cout<<"输入数据有误,请重新输入"<<endl;
l=0;system("cls");break;
}
}while(l!=1);
}while(k==1);
}
int main()
{
menu a;
a.interface();
return 0;
}
C ,c++
[解决办法]
struct infor * creat()
{
ifstream fin("infor.txt",ios::in
[解决办法]
ios::binary);
if(!fin)
{
cerr<<"输入文件打开失败"<<endl;
exit(2);
}
struct infor *p3,*p4,*head;
head=NULL;
p3=p4=new infor;
p3->next=NULL;
if(!fin.eof())
{
fin.read((char*)&p3->name,sizeof(p3->name));
fin.read((char*)&p3->rank,sizeof(p3->rank));
fin.read((char*)&p3->idnumber,sizeof(p3->idnumber));
fin.read((char*)&p3->number,sizeof(p3->number));
if(head=NULL)
head=p3;
else
p4->next=p3;
p4=p3;
p3=new infor; //这个实例没有挂在链表中。
}
p4->next=NULL;
fin.close();
//退出前,需要 delete p3 否则造成内存泄漏。
return head;
//p3 p4 命名不好,可考虑将 p3 改为 pTmp , p4 改为 pListEnd ;
//指针操作常引起错误,这里读程序时检查指针错误是比较痛苦的,
//一个好的做法是封装一个函数
//void AppendItem(infor *InsertItem , infor *NewItem );
//封装后,用函数操作,读代码不用检查指针操作,会轻松好多。
}