为什么这个函数只能找到第一个联系人
#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 showPerson(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);
if (ret != -1)
{
for (int i = ret; i < book->size; i++)
{
book->personArray = book->personArray;
}
book->size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
}
void findPerson(struct addbook *book)
{
cout << "请输入查找联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(book, name);
if (ret != -1)
{
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;
}
else
{
cout << "查无此人" << endl;
}
}
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: showPerson(book);
break;
case 3:deletePerson(&book);
break;
case 4:findPerson(&book);
break;
case 5:
break;
case 6:
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
每次都是只能找到张三,查找后面的人就变成查无此人 int isExist(struct addbook *book, string name)
{
for (int i = 0; i < book->size; i++)
{
if (book->personArray.name == name)
{
return i;
}
}
return -1;// 应该在 for 循环外面
} #include <iostream>
#include <string>
using namespace std;
#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;
}
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 showPerson(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; // ???
}
return -1;
}
void deletePerson(struct addbook *book) {
cout << "请输入需要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(book, name);
if(ret != -1) {
//for(int i = ret; i < book->size; i++) {
for(int i = ret; i < book->size - 1; i++) {
book->personArray = book->personArray;
}
book->size--;
cout << "删除成功" << endl;
} else {
cout << "查无此人" << endl;
}
}
void findPerson(struct addbook *book) {
cout << "请输入查找联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(book, name);
if(ret != -1) {
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;
} else {
cout << "查无此人" << endl;
}
}
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:
showPerson(book); // 知道这么写意味着什么吗?这会把book整个复制一份
// 把复制的这个book传递给showPerson
break;
case 3:
deletePerson(&book);
break;
case 4:
findPerson(&book);
break;
case 5:
break;
case 6:
break;
case 0:
cout << "欢迎下次使用" << endl;
//system("pause");
return 0;
break;
default:
break;
}
}
//system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std; // 为什么要禁用C++的名字空间?
// C++为什么要引入名字空间?
// 是为了解决什么问题?
#define MAX 1000
struct person {
string name;
int sex; // 1男 2女
int age;
string phone;
string add;
};
struct addbook {
struct person personArray; // 为什么不用vector ?
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;
}
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 showPerson(struct addbook book) {
void showPerson(const 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) {
int isExist(const struct addbook *book, string name) {
for(int i = 0; i < book->size; i++) {
if(book->personArray.name == name) {
return i;
}
//return -1; // ???
}
return -1;
}
void deletePerson(struct addbook *book) {
cout << "请输入需要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(book, name);
if(ret != -1) {
//for(int i = ret; i < book->size; i++) {
for(int i = ret; i < book->size - 1; i++) {
book->personArray = book->personArray;
}
book->size--;
cout << "删除成功" << endl;
} else {
cout << "查无此人" << endl;
}
}
//void findPerson(struct addbook *book) {
void findPerson(const struct addbook *book) {
cout << "请输入查找联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(book, name);
if(ret != -1) {
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;
} else {
cout << "查无此人" << endl;
}
}
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:
//showPerson(book); // 知道这么写意味着什么吗?这会把book整个复制一份
// // 把复制的这个book传递给showPerson
showPerson(&book);
break;
case 3:
deletePerson(&book);
break;
case 4:
findPerson(&book);
break;
case 5:
break;
case 6:
break;
case 0:
cout << "欢迎下次使用" << endl;
//system("pause");
return 0;
break;
default:
break;
}
}
//system("pause");
return 0;
}
https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=208317&pid=5714343 人造人 发表于 2022-2-8 13:49
https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=208317&pid=5714343
using namespace std; // 为什么要禁用C++的名字空间?
// C++为什么要引入名字空间?
// 是为了解决什么问题?
其实这个我也不知道是什么意思,只是每次学习视频上都会有这个,老师说是固定的结构框架 孤世星辰 发表于 2022-2-8 14:23
其实这个我也不知道是什么意思,只是每次学习视频上都会有这个,老师说是固定的结构框架
这么写有时候会出问题,而且有时候是很不好解决,因为你根本就察觉不到是因为名字冲突导致的
这么写确实可以偷懒,但是换来的是几个小时的debug,我认为不值得,我反正是不这么写
推荐这样
#include <iostream>
using std::cout, std::endl;
int main() {
cout << "hello world!" << endl;
return 0;
}
人造人 发表于 2022-2-8 14:32
推荐这样
谢谢,以后我也按照这样写 人造人 发表于 2022-2-8 14:32
推荐这样
但是我用这个开头会报错呢 孤世星辰 发表于 2022-2-8 19:53
但是我用这个开头会报错呢
这是C++17的内容
在设置里面找一找吧,vs应该支持C++17,大概
https://blog.csdn.net/tcy23456/article/details/105080261 人造人 发表于 2022-2-8 20:06
这是C++17的内容
在设置里面找一找吧,vs应该支持C++17,大概
https://blog.csdn.net/tcy23456/article ...
不是很懂这个,不知道怎么弄 孤世星辰 发表于 2022-2-9 13:04
不是很懂这个,不知道怎么弄
https://www.baidu.com/baidu?tn=monline_7_dg&ie=utf-8&wd=vs+c%2B%2B17 人造人 发表于 2022-2-9 13:05
https://www.baidu.com/baidu?tn=monline_7_dg&ie=utf-8&wd=vs+c%2B%2B17
成功了!!!谢谢大佬!!!
页:
[1]