这段代码中的 change 函数和 chaxun 函数总是只能修改和显示 mes【1】中的数据,小弟不才无法解决这个 bug,求大神帮忙
#include<iostream>#include<fstream>
#include<iomanip>
#include<string>
#define n 100
using namespace std;
class Chain
{
public:
Chain()
{
i=0;
}
void panduan(int p,int j)
{
int b;
if(i>=1){
ifstream infile("f1.txt",ios::in|ios::binary);
if(!infile){
cerr<<"错误!"<<endl;
exit(1);
}
for(b=0;b<i;b++){
infile.read((char*)&mes[b],sizeof(mes[b]));
}
infile.close();
int m;
bool q;
do
{
q=false;
for(m=0;m<i;m++)
{
if(p==mes[m].xh && mes[m].xx==1){
q=true;
break;
}
}
if(q==true){
cout<<"请重新输入,序号不能相同!"<<endl;
cout<<"序号:";
cin>>mes[i].xh;
break;
}
}while(q!=false);
}
}
void input()
{
ofstream outfile("f1.txt",ios::app|ios::binary);
if(!outfile)
{
cerr<<"错误!"<<endl;
exit(1);
}
cout<<"请需要需要录入会员的信息:"<<endl;
cout<<"序号:";
cin>>mes[i].xh;
panduan(mes[i].xh,i);
cout<<"用户名:";
cin>>mes[i].yhm;
cout<<"性别:";
cin>>mes[i].xb;
cout<<"年龄:";
cin>>mes[i].nl;
cout<<"时间:";
cin>>mes[i].sj;
cout<<"资金:";
cin>>mes[i].zj;
mes[i].xx=1;
outfile.write((char*)&mes[i],sizeof(mes[i]));
i++;
outfile.close();
ofstream shuchuliu("f2.txt",ios::out);
if(!shuchuliu){
cerr<<"错误!"<<endl;
exit(1);
}
shuchuliu<<i;
shuchuliu.close();
}
[b]void change()
{
int b;
ifstream infile("f1.txt",ios::in|ios::binary);
if(!infile){
cerr<<"错误!"<<endl;
exit(1);
}
for(b=0;b<i;b++){
infile.read((char*)&mes[b],sizeof(mes[b]));
}
infile.close();
int a,m;
float c;
cout<<"1.充值资金"<<endl;
cout<<"2.减少资金"<<endl;
cout<<"请选择:";
cin>>m;
if(m==1){
cout<<"请输入您想充值的序号:"<<endl;
cin>>a;
for(b=0;b<i;b++){
if(a==mes[b].xh && mes[b].xh==1){
cout<<"余额为:"<<mes[b].zj<<endl;
cout<<"请输入您想充值的资金:";
cin>>c;
mes[b].zj=mes[b].zj+c;
cout<<"充值成功!"<<endl;
break;
}
else if(b==i-1) cout<<"无此人"<<endl;
}
}
if(m==2){
cout<<"请输入您想减少的序号:"<<endl;
cin>>a;
for(b=0;b<i;b++){
if(a==mes[b].xh && mes[b].xh==1){
cout<<"余额为:"<<mes[b].zj<<endl;
cout<<"请输入您想减少的资金:";
cin>>c;
mes[b].zj=mes[b].zj-c;
cout<<"减少成功!"<<endl;
break;
}
else if(b==i-1) cout<<"无此人"<<endl;
}
}
ofstream outfile("f1.txt",ios::out|ios::binary);
for(b=0;b<i;b++){
outfile.write((char*)&mes[b],sizeof(mes[b]));
}
outfile.close();
}
void chaxun()
{
int b;
ifstream infile("f1.txt",ios::in|ios::binary);
if(!infile){
cerr<<"错误!"<<endl;
exit(1);
}
for(b=0;b<i;b++){
infile.read((char*)&mes[b],sizeof(mes[b]));
}
infile.close();
int a,d;
char h[15];
cout<<"1.查询余额:"<<endl;
cout<<"2.查询会员信息:"<<endl;
cout<<"请选择:";
cin>>d;
if(d==1){
cout<<"请输入您想查询的序号:";
cin>>a;
for(b=0;b<i;b++){
if(a==mes[b].xh && mes[b].xh==1){
cout<<"该用户的余额为:"<<mes[b].zj<<endl;
break;
}
else if(b==i) cout<<"无此人!"<<endl;
}
}
if(d==2){
cout<<"请输入您想查询的用户名:";
cin>>h;
for(b=0;b<i;b++){
if(strcmp(h,mes[b].yhm)==0 && mes[b].xx==1){
cout<<"该用户的信息为:"<<mes[b].xh<<" "<<mes[b].yhm<<" "<<mes[b].xb<<" "<<mes[b].nl<<" "<<mes[b].sj<<" "<<mes[b].zj<<" "<<mes.xx;
break;
}
else if(b==i) cout<<"无此人!"<<endl;
}
}
}
void shanchu()
{
int b;
ifstream infile("f1.txt",ios::in|ios::binary);
if(!infile){
cerr<<"错误!"<<endl;
exit(1);
}
for(b=0;b<i;b++){
infile.read((char*)&mes[b],sizeof(mes[b]));
}
infile.close();
int h,a;
cout<<"请输入您想删除的序号:";
cin>>a;
for(b=0;b<i;b++){
if(a==mes[b].xh && mes[b].xx==1){
mes[b].xx=0;
h=b;
cout<<"删除成功!"<<endl;
break;
}
else if(b==i) cout<<"无此人!"<<endl;
}
ofstream outfile("f1.txt",ios::out|ios::binary);
for(b=0;b<i;b++){
if(b==h) b++;
else outfile.write((char*)&mes[b],sizeof(mes[b]));
}
i--;
outfile.close();
ofstream shuchuliu("f2.txt",ios::out);
if(!shuchuliu){
cerr<<"错误!"<<endl;
exit(1);
}
shuchuliu<<i;
shuchuliu.close();
}
void xianshi()
{
int b;
ifstream infile("f1.txt",ios::in|ios::binary);
if(!infile){
cerr<<"错误!"<<endl;
exit(1);
}
for(b=0;b<i;b++){
infile.read((char*)&mes[b],sizeof(mes[b]));
}
infile.close();
for(b=0;b<i;b++){
cout<<mes[b].xh<<" "<<mes[b].yhm<<" "<<mes[b].xb<<" "<<mes[b].nl<<" "<<mes[b].sj<<" "<<mes[b].zj<<" "<<mes[b].xx<<endl;
}
}
int i;
private:
struct message
{
int xh;
char yhm[15];
char xb[2];
int nl;
char zhy[18];
float zj;
char sj[20];
int xx;
};
message mes[n];
};
int main()
{
ifstream shuchuliu("f1.txt",ios::in);
if(!shuchuliu){
cerr<<"错误!"<<endl;
exit(1);
}
Chain r;
shuchuliu>>r.i;
int choose;
choose=-1;
cout<<"__________________足球俱乐部会员管理系统___________________"<<endl;
cout<<"1-录入信息 2-重值 3-查询 4-输出全部信息 5-删除信息 6-结束"<<endl;
while(choose!=7){
cout<<"请选择:";
cin>>choose;
switch(choose){
case 1:
r.input();
break;
case 2:
r.change();
break;
case 3:
r.chaxun();
break;
case 4:
r.xianshi();
break;
case 5:
r.shanchu();
break;
case 6:
break;
}
cout<<endl;
}
return 0;
}