如果用户尝试搜索先前未在结构中输入的名称,我需要使第二个函数打印“没有具有该名称的条目”。除了这部分之外,一切都工作正常。我当前的 else 语句有效,但它每次都会输出该行,而不仅仅是在结构中未输入名称时。有什么帮助可以改变吗?
#include <stdio.h>
#include <string.h>
#include "libpb.h"
void add_person(struct phone_book * pb, struct personal_info person)
{
int num = pb->num_people;
strcpy(pb->person[num].first, person.first);
strcpy(pb->person[num].last, person.last);
strcpy(pb->person[num].phone, person.phone);
num++;
pb->num_people = num;
}
void search_pb(struct phone_book pb, char find_name[])
{
int p;
for (p = 0; p < pb.num_people; p++)
{
if (strcmp(find_name, pb.person[p].first) == 0)
{
printf("\nName: %s %s\n", pb.person[p].first,
pb.person[p].last);
printf("Phone: %s\n", pb.person[p].phone);
} else
{
printf("No entries with that name. \n");
}
}
}
我得到了要使用的主函数phone_book.c,所以我只需要编写上面的函数和一个头文件:
int main ()
{
char cont;
char find_name[25];
struct phone_book pb;
pb.num_people = 0;
struct personal_info person;
printf("\n*********************************************\n");
printf("\n Start with entering new contacts! \n");
printf("\n*********************************************\n");
printf("\nWould you like to enter a new contact (Y/N): ");
while(pb.num_people < 20)
{
scanf("%c", &cont);
if (cont == 'Y')
{
printf("Enter a first name: ");
scanf("%s", person.first);
printf("Enter %s's last name: ", person.first);
scanf("%s", person.last);
printf("Enter %s's phone number: ", person.first);
scanf("%s", person.phone);
add_person(&pb, person);
}
else if (cont == 'N') break;
else if (cont == '\n') continue;
else printf("Error: User entered '%c'. Must enter either 'Y' or 'N'\n",
cont);
printf("\nWould you like to enter a new name (Y/N): ");
}
//search phone book by first name and print persons
printf("\n*********************************************\n");
printf("\n Now You can search for names! \n");
printf("\n*********************************************\n");
printf("\nWould you like to search for a name (Y/N)? ");
while(1)
{
scanf("%c", &cont);
if (cont == 'Y')
{
printf("Enter a person's name to search for: ");
scanf("%s", find_name);
//scanf("%c", &tmp);
search_pb(pb, find_name);
}
else if (cont == 'N') break;
else if (cont == '\n') continue;
else printf("Error: User entered '%c'. Must enter either 'Y' or 'N'\n",
cont);
printf("\nWould you like to search for a name (Y/N)? ");
}
return 0;
}
我也已经制作了必要的头文件 libpb.h:
#include<stdio.h>
#include<string.h>
#define MAX 20
#define _CRT_SECURE_NO_DEPRECATE
struct personal_info
{
char first[25];
char last[25];
char phone[15];
};
struct phone_book
{
struct personal_info person[MAX];
int num_people;
};
void add_person(struct phone_book *pb, struct personal_info person);
void search_pb(struct phone_book pb, char find_name[]);
最佳答案
所以我在 repl.it ( https://repl.it/repls/SphericalFuchsiaIntroductory ) 中输入了您的代码并声明缩小问题范围。根据我的测试,只有当您在结构中输入 2 个或更多名称时,才会发生此问题,这使我相信该函数被调用了两次或其他原因。
您的代码的问题是 for 循环内的 else 语句,您的循环遍历所有联系人并搜索匹配的联系人,如果只有一个联系人,则 else 会正常触发,但如果有多个联系人它将触发与循环一样多的次数。为了在 repl.it 上修复它(如果您想查看完整内容,请单击链接),我将您的函数更改为:
void search_pb(struct phone_book pb, char find_name[]) {
int p;
for (p = 0; p < pb.num_people; p++)
{
if (strcmp(find_name, pb.person[p].first) == 0)
{
printf("\nName: %s %s\n", pb.person[p].first,
pb.person[p].last);
printf("Phone: %s\n", pb.person[p].phone);
return;
}
}
printf("No entries with that name. \n");
}
如您所见,现在您的函数一旦找到匹配项就会返回并打印匹配项,否则它默认打印未找到的条目。 希望有帮助!
关于c - C 语言的基本结构帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49681388/