2 qq 22651039 qq_22651039 于 2014.10.28 19:09 提问

我编得通讯录创建联系人函数哪儿有问题?

#include
using namespace std;
struct contact
{
int ID;

char name[10];

char ch;

char phone[13];

char addr[31];

struct contact *next;

};
class contacts
{
public:
contacts(){front= new contact; front=NULL;}
void AddCon(int n);
~contacts();
void InsertCon();////插入联系人的基本信息
void FindCon(); ////查找联系人的基本信息
void AmendMessage();//修改联系人的基本信息
void DeleteMessage();//删除联系人的基本信息
void Con();
private:
contact * front;
};
void contacts::AddCon(int n)
{
system("CLS");
contact p= new contact;
cout<<"输入联系人信息:"< cout cin>>p->name;
cout<<"ID:"< cin>>p->ID;
cout<<"sex:"< cin>>p->ch;
cout<<"phone:"< cin>>p->phone;
cout<<"address:"< cin>>p->addr;
front=p;
for(int i=0;i< n-1;i++)
{
system("CLS");
cout<<"现在输入第"<<i+2<<"个联系人的信息"<<endl;
contact
q=new contact;
cout<<"name:"< cin>>q->name;
cout<<"ID:"< cin>>q->ID;
cout<<"sex:"< cin>>q->ch;
cout<<"phone:"< cin>>q->phone;
cout<<"address:"< cin>>q->addr;
p->next=q;
p=q;

}
p->next=NULL;

}
contacts::~contacts()
{

contact *p=front;
while(p)
{
    contact *q=new contact;
    q=p;
    p=p->next;
    delete q;
}

}
void contacts::InsertCon()

{
contact *p=new contact;
p=front;
while(p)
{
p=p->next;
};

if(p==NULL)
    cout<<"请先添加联系人"<<endl;
else 
    {
        contact *q=new contact;
        p->next=q;
        cout << "请依次被插入人的信息:姓名"<<"性别"<<"住址"<<"手机号"<<"学号"<< endl;
        cin >> q->name >> q->ch >> q->addr >> q->phone >> q->ID;
        q->next=NULL;
      }

}
void contacts::FindCon()
{
char a[20];
system("CLS");
/*if(front==NULL)
cout<<"请先添加联系人"< //else
//{
cout cin>>a;
contact *p=front;
while(p)
{
if(strcmp(p->name ,a)==0)
{
cout<<"结果如下"<<"name:"<name<<"ID:"<ID<<"sex:"<ch<<"phone:"<phone<<"address:"<addr< break;
}
else if(p->next!= NULL && p->name != a)
{
p = p->next ;
}
else
{
cout << "输入名字错误" << endl;

             }

        }
//}

}
void contacts::AmendMessage()
{

system("CLS");
if(front==NULL)
    cout<<"请先添加联系人"<<endl;
else 
    {
        int ID1;         
        char name1[10];  
        char ch1;   
        char phone1[13];  
        char addr1[31]; 
        int n;
        cout<<"输入要修改的名字"<<endl;
        cin>>name1;
        contact *p=front;
        while(p)
        {
            if(strcmp(p->name ,name1)==0)
            {
                cout<<"选择需要修改的内容:"<<endl;
                cout<<"1,性别 2,学号 3,电话 4,地址"<<endl;
                cin>>n;
                switch(n)
                {
                case 1:
                    cout<<"输入改变后的性别"<<endl;
                    cin>>ch1;
                    p->ch=ch1;   //char ch,ch1 strcpy(p->ch,ch1)为什么不行 必须是字符数组吗
                    cout<<"success"<<endl;
                    system("CLS");
                    break;
                case 2:
                    cout<<"输入改变后的学号"<<endl;
                    cin>>ID1;
                    p->ID=ID1;
                    cout<<"success"<<endl;
                    system("CLS");
                    break;
                case 3:
                    cout<<"输入改变后的电话"<<endl;
                    cin>>phone1;
                    strcpy(p->phone,phone1);
                    cout<<"success"<<endl;
                    system("CLS");
                    break;
                case 4:
                    cout<<"输入改变后的地址"<<endl;
                    cin>>addr1;
                    strcpy(p->addr, addr1);
                    cout<<"success"<<endl;
                    system("CLS");
                    break;
                default:
                    cout<<"wrong"<<endl;
                    break;
                }

                break;
            }
            else if(p->next!= NULL && p->name != name1)
            {
                 p = p->next ;
            }
            else
            {
                cout << "输入名字错误" << endl;

             }

        }
    }

}
void contacts::DeleteMessage()
{
system("CLS");
if(front==NULL)
cout<<"请先添加联系人"< else
{
char name2[10];
cout cin>>name2;
contact *p=front;
while(p)
{
if(strcmp(p->name ,name2)==0)
{
contact *q=p->next;
strcpy(p->addr,q->addr);//p->addr=q->addr;
p->ch=q->ch;
p->ID=q->ID;
strcpy(p->name,q->name);
strcpy(p->phone,q->phone);
p->next=q->next;
delete q;
break;
}
else if(p->next!= NULL && p->name != name2)
{
p = p->next ;
}
else
{
cout << "输入名字错误" << endl;

             }
        }
}

}
void contacts::Con()
{
int n;
do
{
cout<<" 通讯录 "< cout cout cout cout cout cout contacts a;
cin>>n;
if(n!=1&&n!=2&&n!=3&&n!=4&&n!=5&&n!=6)
{
cout<<"输入错误"< }
else
{
switch(n)
{
case 1:
int m;
do
{
cout cin>>m;

        }while(m<=0);
         a.AddCon(m);
         break;
    case 2:
        a.InsertCon();
        break;
    case 3:
        a.FindCon();
        break;
    case 4:
        a.AmendMessage();
        break;
    case 5:
        a.DeleteMessage();
        break;
    case 6:
        break;
    default:

    cout << "你输入错误,请重新选择!!!" << endl;
    break;



     }
}
}while(n !=6);

}
int main()

{
contacts b;
b.Con();
return 0;

}
为什么创建联系人之后,调用其他函数,都一直显示“请先添加联系人”?不知道怎么改,先谢谢大家了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!