| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 700 人关注过本帖
标题:这段代码中的 change 函数和 chaxun 函数总是只能修改和显示 mes【1】中的数 ...
只看楼主 加入收藏
a7392187
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-6-11
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
这段代码中的 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;
    }
搜索更多相关主题的帖子: include change public 
2014-06-14 12:30
funyh250
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:26
帖 子:290
专家分:1573
注 册:2013-12-25
收藏
得分:10 
代码太长了
问题做个精简或浓缩

学习是大事   吃喝拉撒睡是小事   其他的那都不是事
2014-06-14 22:20
疯狂的图灵机
Rank: 2
等 级:论坛游民
威 望:2
帖 子:5
专家分:40
注 册:2014-6-15
收藏
得分:10 
你没有定义i
2014-06-15 08:35
快速回复:这段代码中的 change 函数和 chaxun 函数总是只能修改和显示 mes【1】 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028239 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved