标题:代码中包含两个类,chain 是 associator 的友元类。在编译调试完毕后,执行 ...
只看楼主
a7392187
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-6-11
结帖率:50%
已结贴  问题点数:20 回复次数:5 
代码中包含两个类,chain 是 associator 的友元类。在编译调试完毕后,执行代码会发现选择 1,之后输入序号不管输入任何字符都无法继续执行,为什么会
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#define n 100
using namespace std;
class Chain;
class Associator
{
public:
    Associator *ptrnext;
    void input(Associator *p,int ji)      //该函数使得序号唯一
    {
        int j;
        int q=-1;
        cout<<"序号:";
        cin>>xh;
        while(q!=0){
            q=-1;
            for(j=0;j<=ji;j++){
                if(p[j].i==1 && strcmp(p[j].xh,xh)==0){
                    cout<<"序列号不能相同"<<endl;
                    q=1;
                    cin>>xh;
                    break;
                }
            }
            if(q==1) continue;
            else q=0;
        }
        cout<<"用户名:";
        cin>>yhm;
        cout<<"性别:";
        cin>>xb;
        cout<<"年龄:";
        cin>>nl;
        cout<<"职业:";
        cin>>zhy;
        cout<<"资金:";
        cin>>zj;
        cout<<"时间:";
        cin>>sj;
        i=1;
    }
   
    void input(ifstream &in)      //新会员入会登记
    {
        in>>yhm>>xb>>nl>>zhy>>zj>>sj;
        i=1;
    }
   
    void display()
    {
        cout<<xh<<setw(10)<<yhm<<setw(10)<<nl<<setw(10)<<zhy<<setw(10)<<zj<<setw(10)<<sj<<endl;
    }
   
    void display(ofstream &out)
    {
        out<<xh<<setw(10)<<yhm<<setw(10)<<xb<<setw(10)<<nl<<setw(10)<<zhy<<setw(10)<<zj<<setw(10)<<sj<<endl;
    }
   
    friend Chain;
private:
    int i;                            //标记,0删除,1未删
    char xh[15];
    char yhm[15];
    char xb[2];
    int nl;
    char zhy[18];
    float zj;
    char sj[20];
};


class Chain
{
public:
    void panduan()
    {
        int h;
        for(h=0;h<n;h++)
            p[h].i=0;
        pb.open("associator.txt",ios::in);
        if(!pb)
        {
            cout<<"错误!"<<endl;
            exit(1);
        }
        for(ji=0;;ji++)
        {
            pb>>p[ji].xh;
            if(strcmp(p[ji].xh,"*")==0)
                break;
            p[ji].input(pb);
        }
        pb.close();
    }
   
    void input()
    {
        p[ji].input(p,ji);
        ji++;
    }
   
    void display()
    {
        int y;
        for(y=0;y<ji;y++)
            if(p[y].i==1)
                p[y].display();
    }
   
    void Delete()                 //会员不想继续入会则必须将其记录删除
    {
        int l;
        char k[15];
        cout<<"请输入你想删除的序号:";
        cin>>k;
        for(l=0;l<ji;l++)
            if(p[l].i==1&&strcmp(p[l].xh,k)==0)
            {
                p[l].i=0;
                cout<<"删除成功"<<endl;
                break;
            }
            if(l==ji)
                cout<<"无此人"<<endl;
    }
   
    void find()                //按序号查询会员所剩资金
                                     //按用户名查询用户相关信息
    {
        char k[15];
        int g;
        string c;
        cout<<"1-查资金  2-查会员信息"<<endl;
        cin>>c;
        if(c=="1")
        {
            cout<<"请输入你想查的序号:";
            cin>>k;
            for(g=0;g<ji;g++)
                if(p[g].i==1&&strcmp(p[g].xh,k)==0)
                {
                    cout<<"资金为:";
                    cout<<p[g].zj<<endl;
                    break;
                }
                if(g==ji)
                    cout<<"无此人"<<endl;
        }
        else if(c=="2")
        {
            cout<<"请输入你想查的姓名:";
            cin>>k;
            for(g=0;g<ji;g++)
                if(p[g].i==1&&strcmp(p[g].yhm,k)==0)
                {
                    p[g].display();
                    break;
                }
                if(g==ji)
                    cout<<"无此人"<<endl;
        }
    }
   
    void change()                   //只能修改会员的资金情况
    {
        char k[15];
        int j,c;
        cout<<"请输入你想查的序号:"<<endl;
        cin>>k;
            for(j=0;j<ji;j++)
                if(p[j].i==1&&strcmp(p[j].xh,k)==0)
                {
                    cout<<"请输入你想重的资金"<<endl;
                    cin>>c;
                    p[j].zj=c+p[j].zj;
                    cout<<"资金已重入"<<endl;
                    break;
                }
            if(j==ji)
                cout<<"无此人"<<endl;
    }
   
    int string_int(char *k)// 将char的数转化为int 的数
    {
        int i,j=0;
        for(i=0;k[i]!='\0';i++)
            j=j*10+k[i]-'0';
        return j;
    }
   
    void kong()
    {
        string k;
        for(;;)
        {
            cout<<"1-按年龄统计   2-按入会时间统计"<<endl;
            cin>>k;
            if(k=="1")
            {
                kong_age();
                break;
            }
            else if(k=="2")
            {
                kong_time();
                break;
            }
            else
            {
                cout<<"输入错误!"<<endl;
            }
        }
    }
   
    void kong_age()                //按年龄统计
    {
        int i,a[8],j;
        int c=10;
        for(i=0;i<8;i++)
            a[i]=0;
        for(i=0;i<ji;i++)
        {
            if(p[i].i==1)
            {
                j=p[i].nl;
                if(10<=j&&j<20)  a[0]++;
                else if(20<=j&&j<30) a[1]++;
                else if(30<=j&&j<40) a[2]++;
                else if(40<=j&&j<50) a[3]++;
                else if(50<=j&&j<60) a[4]++;
                else if(60<=j&&j<70) a[5]++;
                else if(70<=j&&j<80) a[6]++;
                else if(80<=j&&j<90) a[7]++;
                else cout<<p[i].yhm<<"不在统计范围内"<<endl;
            }
        }
        for(i=0;i<8;i++)
        {
            cout<<c<<"~~"<<c+10<<"的人数为:"<<a[i]<<endl;
            c=c+10;
        }
    }
   
    void kong_time()         //按入会时间统计
    {
        int a[8];
        int i;
        for(i=0;i<8;i++)
            a[i]=0;
        for(i=0;i<ji;i++)
        {
            if(p[i].i==1)
            {
                if(strcmp(p[i].sj,"2000")==0) { a[0]=a[0]+p[i].zj;}
                else if(strcmp(p[i].sj,"2001")==0) {a[1]=a[1]+p[i].zj;}
                else if(strcmp(p[i].sj,"2002")==0) {a[2]=a[2]+p[i].zj;}
                else if(strcmp(p[i].sj,"2003")==0) {a[3]=a[3]+p[i].zj;}
                else if(strcmp(p[i].sj,"2004")==0) {a[4]=a[4]+p[i].zj;}
                else if(strcmp(p[i].sj,"2005")==0) {a[5]=a[5]+p[i].zj;}
                else if(strcmp(p[i].sj,"2006")==0) {a[6]=a[6]+p[i].zj;}
                else if(strcmp(p[i].sj,"2007")==0) {a[7]=a[7]+p[i].zj;}
            }
        
        }
        for(i=0;i<8;i++)
        {
            cout<<"200"<<i<<"的资金:"<<a[i]<<endl;
        }
    }
   
     void save()           //进行对会员信息的储存函数,输入结束后应该输入"7"来结束,否则无法对会员信息进行储存
     {
         Associator *p1=p;
         int j;
         pc.open("associator.txt",ios::out);
         for(j=0;j<ji;j++)
             p1[j].display(pc);
         pc<<"*";
         pc.close();
     }

    ~Chain()
    {
        save();
    }
   
   
private:
    ifstream pb;//输入流
    ofstream pc;//输出流
    Associator p[n];
    int ji;//链表长度
};
   
    int main()
    {
        Chain a;
        int choose;
        choose=-1;
        cout<<"__________________足球俱乐部会员管理系统___________________"<<endl;
        cout<<"1-录入信息  2-重值  3-查询  4-输出全部信息   5-删除信息  6-统计  7-结束"<<endl;
        while(choose!=7){
            cout<<"请选择:";
            cin>>choose;
            switch(choose){
            case 1:
                a.input();
                break;
            case 2:
                a.change();
                    break;
            case 3:
                a.find();
                break;
            case 4:
                a.display();
                break;
            case 5:
                a.Delete();
                break;
            case 6:
                a.kong();
                break;
            case 7:
                break;
            }
            cout<<endl;
        }
        return 0;
    }
搜索更多相关主题的帖子: include public 序列号 chain include public 序列号 chain include public 序列号 chain 
2014-06-11 18:08
a7392187
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-6-11
得分:0 
希望大神们能够耐心地看一下,我找了好多遍都无法找到错误之处,真的很着急。谢谢你们。
2014-06-11 18:56
律吕调阳
Rank: 2
等 级:论坛游民
威 望:2
帖 子:16
专家分:93
注 册:2014-6-9
得分:10 
给China加个构造函数把 ji初始化一下
2014-06-11 19:20
律吕调阳
Rank: 2
等 级:论坛游民
威 望:2
帖 子:16
专家分:93
注 册:2014-6-9
得分:5 
我电脑上可以正常运行,好像没什么错。
另外建议楼主以后在类中的成员变量,尽量都在构造函数中初始化一下
2014-06-11 19:55
a7392187
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-6-11
得分:0 
回复 4 楼 律吕调阳
恩 我初始化了 ,可是还是不能运行,把ji初始化成了0,麻烦您看下还有哪里需要改进,谢谢,真的很着急
2014-06-11 22:12
律吕调阳
Rank: 2
等 级:论坛游民
威 望:2
帖 子:16
专家分:93
注 册:2014-6-9
得分:5 
加我Q我们讨论,发帖太慢了
1404302957
2014-06-12 12:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-432797-1-1.html




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

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