#include<iostream>
#include<string>
using namespace std;
static int n = 0;
class addr {
public:
char post[10] = { };//邮编
char ad[40] = { };//家庭住址
addr(char *p, char *a)//构造函数
{
strcpy(post, p);
strcpy(ad, a);
}
void setadd(addr a)//用a为当前对象赋值
{
for (int i = 0; i < strlen(a.post); i++)
post[i] = a.post[i];
for (int i = 0; i < strlen(a.ad); i++)
post[i] = a.ad[i];
}
};
class birth {
public:
int year;
int month;
int day;
birth(int y = 0, int m = 0, int d = 0)//构造函数
{
year = y;
month = m;
day = d;
}
void setbir(birth b)//用b为当前对象赋值
{
year = b.year;
month = b.month;
day = b.day;
}
};
class friends {
private:
int number = 0;//序号
char name[20] = { 0 };//姓名
char sex = { 0 };//性别
char telephone[13] = { 0 };//联系电话
public:
birth bir;
addr add;
void setfri(int nu, char n[20], char s, birth b, addr a, char t[13])
{
number = nu;//引发了异常: 写入访问权限冲突。 **this** 是 nullptr。
strcpy(name, n);
sex = s;
bir.setbir(b);
add.setadd(a);
strcpy(telephone, t);
}
friends input(friends& bb)//为对象bb输出数据
{
return bb;
}
int search(friends aa[100], char nn[20])
{
for (int i = 0; i < n; i++)
{
if (strcmp(aa[i].name, nn))
return aa[i].number;
}
return 0;
}
void show(friends aa)//输出对象aa
{
cout << "序号:" << number << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "联系电话:" << telephone << endl;
cout << "出生年月:" << bir.year << "年" << bir.month << "月" << bir.day << "日" << endl;
cout << "通信住址:" << '\t';
cout << "邮编:" << add.post << '\t' << "家庭住址:" << add.ad << endl;
}
void delet(friends aa[100], int k)//在aa中删除下标为k的元素
{
for (int i = 0; i < n; i++)
if (i == k)
{
for (int j = i; j < n; j++)
aa[j] = aa[j + 1];
}
}
};
int main()
{
cout << "1.输入数据" << '\n' << "2.输出数据" << '\n' << "3.查找数据" << '\n' << "4.删除数据" << '\n' << "5.退出" << '\n' << "请选择(1~5)" << endl;
int x;
cin >> x;//用户选择的数字
static friends *a;
if (x == 1)//输入数据
{
int number;//序号
char name[20]; //姓名
char sex;//性别 ?
char telephone[13];//手机号码
int year;//出生年
int month;//出生月
int day;//出生日
char post[10];//邮编
char address[40];//家庭住址
cout << "请输入序号:";
cin >> number;
cout << "请输入姓名:";
cin >> name;
cout << "请输入性别:";
cin >> sex;
cout << "请输入联系电话:";
cin >> telephone;
cout << "请输入出生年月日:";
cin >> year >> month >> day;
cout << "请输入邮政编码:";
cin >> post;
cout << "请输入家庭住址:";
cin >> address;
addr add(post, address);
birth bir(year, month, day);
a[n].setfri(number, name, sex, bir, add, telephone);
n++;
}
if (x == 2)//输出数据
{
a[n].show(a[n]);
}
if (x == 3)//查找数据
{
cout << "你的姓名为:";
char name[20];
cin >> name;
cout << "你的序号为:" << a[n].search(a, name) << endl;
}
if (x == 4)//删除数据
{
cout << "你的序号是:";
int k = 0;
cin >> k;
a[k].delet(a, k);
}
if (x == 5)//退出
{
return 0;
}
return 0;
}
这个该怎么改哇,很急,在线等
static friends *a;
这个a指针你没有初始化!
你这就离谱。static friends a[100];试试
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y