函数传参问题第118行
本帖最后由 孤世星辰 于 2022-2-6 19:32 编辑118行book传参会报错
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
struct person
{
string name;
int sex;//1男 2女
int age;
string phone;
string add;
};
struct addbook
{
struct person personArray;
int size;
};
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;
}
void addPerson(struct addbook *book)
{
if (book->size == MAX)
{
cout << "联系人已满" << endl;
return;
}
else
{
string name;
cout << "请输入联系人姓名" << endl;
cin >> name;
book->personArray.name = name;
int sex;
cout << "请输入联系人性别 1、男 2、女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
book->personArray.sex = sex;
break;
}
cout << "输入有误请重新输入" << endl;
}
int age;
cout << "请输入联系人年龄" << endl;
cin >> age;
book->personArray.age = age;
string phone;
cout << "请输入联系人电话" << endl;
cin >> phone;
book->personArray.phone = phone;
string add;
cout << "请输入联系人住址" << endl;
cin >> add;
book->personArray.add = add;
book->size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
void shouPerson(struct addbook book)
{
if (book.size == 0)
{
cout << " 当前记录为空" << endl;
}
for (int i = 0; i < book.size; i++)
{
cout << "姓名:" << book.personArray.name << "\t";
cout << "性别:" << (book.personArray.sex == 1 ? "男":"女") << "\t";
cout << "年龄:" << book.personArray.age << "\t";
cout << "电话:" << book.personArray.phone << "\t";
cout << "住址:" << book.personArray.add << endl;
};
system("pause");
system("cls");
}
int isExist(struct addbook book,string name)
{
for (int i = 0; i < book.size; i++)
{
if (book.personArray.name == name)
{
return i;
}
return -1;
}
}
void deletePerson(struct addbook *book)
{
cout << "请输入需要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(book, name); //这个book传参为啥会报错
}
int main()
{
struct addbook book;
book.size = 0;
int select = 0;
while (1)
{
showMenu();
cin >> select;
switch (select)
{
case 1:addPerson(&book);//利用地址传递修改
break;
case 2: shouPerson(book);
break;
case 3:deletePerson(&book);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
} 改成
int ret = isExist(*book, name);
试试 ckblt 发表于 2022-2-6 19:37
改成
试试
为什么要传这个,可以解释一下嘛 因为你传的book是指针(addbook *),而参数不是指针(addbook),所以你得把book变成*book void deletePerson(struct addbook *book)// 注意这里的 book 是 addbook * int isExist(struct addbook book,string name) //这里book不是指针变量
{
for (int i = 0; i < book.size; i++)
{
if (book.personArray.name == name)
{
return i;
}
return -1;
}
}
void deletePerson(struct addbook *book)//这里book是指针变量
{
cout << "请输入需要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(book, name); //这里的形参是一般变量,但book确是指针变量,应该用 *book 替代 book
}
页:
[1]