我经过调试显示读取访问权限冲突附上源代码
#include <iostream>using namespace std;
#include <string>
#define MAX 1000
struct Person//定义的成员
{
string NAME;
int Sex;
int Age;
string Phone;
string Address;
};
struct Addressbooks//定义的通讯录
{
struct Person PeasonArray;
int Size;
};
void addPerson(Addressbooks * abs)//输入用户的函数
{
if (abs->Size == MAX)
{
cout << "通讯录已满,无法添加!"<<endl;
return;
}
else
{
//输入名字
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->PeasonArray.NAME = name;
//输入性别
cout << "请输入性别: " << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int sex = 0;
cin >> sex;
while (true)
{
if (sex == 1 || sex == 2)
{
abs->PeasonArray.Sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入" << endl;
}
}
//输入年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->PeasonArray.Age = age;
//输入电话
cout << "请输入联系电话: " << endl;
string phone;
cin >> phone;
abs->PeasonArray.Phone = phone;
//输入住址
cout << "请输入家庭住址:"<<endl;
string address;
cin >> address;
abs->PeasonArray.Address = address;
//加入成员后数组改变
abs->Size++;
cout << "添加成功" << endl;
system("pause");//按任意继续
system("cls");//清屏
}
}
void showPerson(Addressbooks * abs)
{
if (abs->Size == 0)
{
cout << "没有联系人请添加" << endl;
}
else
{
for (int i = 0; i < abs->Size; i++)
{
cout << "姓名" << abs->PeasonArray.NAME << endl;
cout << "性别" << abs->PeasonArray.Sex << endl;
cout << "年龄" << abs->PeasonArray.Age << endl;
cout << "电话" << abs->PeasonArray.Phone << endl;
cout << "住址" << abs->PeasonArray.Address << endl;
}
}
system("pause");
system("cls");
}
int isExist(Addressbooks* abs, string name)
{
for (int i = 0; i < abs->Size; i++)
{
if (abs->PeasonArray.NAME == name)
{
return i;
}
else
{
return -1;
}
}
}
void deletePerson(Addressbooks* abs)
{
cout << "请输入你要删除的人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->Size; i++)
{
abs->PeasonArray = abs->PeasonArray;
}
abs->Size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findPerson(Addressbooks* abs)
{
cout << "请输入你要查找的人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "姓名" << abs->PeasonArray.NAME << endl;
cout << "性别" << abs->PeasonArray.Sex << endl;
cout << "年龄" << abs->PeasonArray.Age << endl;
cout << "电话" << abs->PeasonArray.Phone << endl;
cout << "住址" << abs->PeasonArray.Address << endl;
}
system("pause");
system("cls");
}
void modityPerson(Addressbooks* abs)
{
cout << "请输入你要修改的人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout<< "查无此人" << endl;
}
else
{
string name;
cout << "请输入你要修改的名字" << endl;
cin >> name;
abs->PeasonArray.NAME = name;
cout << "请输入性别:" << endl;
cout << "1 -- 男" << endl;
cout << "2 -- 女" << endl;
int sex = 0;
cin >> sex;
while (true)
{
if (sex == 1 || sex == 2)
{
abs->PeasonArray.Sex = sex;
}
else
{
cout << "输入错误,请重新输入。" << endl;
}
}
cout << "请输入你要修改的年龄" << endl;
int age = 0;
cin >> age;
abs->PeasonArray.Age = age;
cout << "请输入你要修改的电话号码" << endl;
string phone;
cin >> phone;
abs->PeasonArray.Phone = phone;
cout << "请输入你要修改的住址" <<endl;
string address;
cin >> address;
abs->PeasonArray.Address = address;
}
system("pause");
system("cls");
}
void cleanPerson(Addressbooks* abs)
{
abs->Size = 0;
cout << "清空通讯录" << endl;
system("pause");
system("cls");
}
void showmenu()
{
cout << "**************************" << endl;
cout << "*****1,添加联系人*****" << endl;
cout << "*****2,显示联系人*****" << endl;
cout << "*****3,删除联系人*****" << endl;
cout << "*****4,查找联系人*****" << endl;
cout << "*****5,修改联系人*****" << endl;
cout << "*****6,清空联系人*****" << endl;
cout << "*****0,退出联系人*****" << endl;
cout << "**************************" << endl;
}
int main()
{
Addressbooks abs;
abs.Size = 0;
int select = 0;
while (true)
{
showmenu();
cin >> select;
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePerson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modityPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用通讯录" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
} 这是一个通讯录系统实现输入查找修改清空等功能。sex为性别。 Addressbooks abs;
你确定结构体能这样用吗? 这个 Addressbooks 定义了吗?
struct Addressbooks abs; 这样写应该才对。
或
typedef struct Addressbooks//定义的通讯录
{
struct Person PeasonArray;
int Size;
}Addressbooks;
chxchxkkk 发表于 2020-3-18 10:30
Addressbooks abs;
你确定结构体能这样用吗? 这个 Addressbooks 定义了吗?
struct Addressbooks abs; ...
好的谢谢
izues 发表于 2020-3-18 11:10
好的谢谢
不对兄弟我的错误不是这个,是我的isExist函数没有考虑到abs->size=0的情况。改了就行了,你给我说的那个地方我是对的,你仔细看我的代码。
页:
[1]