Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C新手,需要结构相关功能方面的帮助_C - Fatal编程技术网

C新手,需要结构相关功能方面的帮助

C新手,需要结构相关功能方面的帮助,c,C,我需要让第二个函数(search_pb)打印在personal_info结构中输入的所有匹配名称。现在如果有两个重复的名字,它只打印第一个。例如,如果我添加 名字:“阿尔伯特” 姓氏:“爱因斯坦” 电话号码:35245 并补充说, 名字:“阿尔伯特” 姓:“韦斯克” 电话号码:17367 当我搜索“Albert”时,它只会打印Albert输入的第一个,而不是两个。关于如何改变这一点有什么想法吗 #include <stdio.h> #include <string.h>

我需要让第二个函数(search_pb)打印在personal_info结构中输入的所有匹配名称。现在如果有两个重复的名字,它只打印第一个。例如,如果我添加

名字:“阿尔伯特” 姓氏:“爱因斯坦” 电话号码:35245

并补充说,

名字:“阿尔伯特” 姓:“韦斯克” 电话号码:17367

当我搜索“Albert”时,它只会打印Albert输入的第一个,而不是两个。关于如何改变这一点有什么想法吗

#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);

            return;

        } 

    }

    printf("No entries with that name. \n");  
}
#包括
#包括
#包括“libpb.h”
无效添加人员(结构电话簿*pb,结构个人信息人员)
{
int num=pb->num\u人;
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;
}
无效搜索\u pb(结构电话\u簿pb,字符查找\u名称[])
{
INTP;
对于(p=0;p
我有一个主要的函数phone_book.c可以使用,所以我只需要制作上面的函数和一个头文件:

#include <stdio.h>
#include <string.h>
#include "libpb.h"

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”
int main()
{
字符控制;
char find_name[25];
结构电话簿pb;
pb.num_people=0;
结构个人信息人;
printf(“\n*********************************************************\n”);
printf(“\n从输入新联系人开始!\n”);
printf(“\n*********************************************************\n”);
printf(“\N是否要输入新联系人(Y/N):”;
而(pb.num_人<20人)
{
scanf(“%c”,续(&c));
如果(cont=='Y')
{
printf(“输入名字:”);
scanf(“%s”,人称第一);
printf(“输入%s的姓氏:”,person.first);
scanf(“%s”,person.last);
printf(“输入%s的电话号码:”,person.first);
scanf(“%s”,person.phone);
添加人员(&pb,人员);
}
如果(cont=='N')中断,则为else;
如果(cont=='\n')继续,则为else;
else printf(“错误:用户输入了“%c”。必须输入“Y”或“N”\N”,
续);
printf(“\N是否输入新名称(Y/N):”;
}
//按姓名和打印人搜索电话簿
printf(“\n*********************************************************\n”);
printf(“\n现在您可以搜索名称!\n”);
printf(“\n*********************************************************\n”);
printf(“\N是否要搜索名称(Y/N)?”;
而(1)
{
scanf(“%c”,续(&c));
如果(cont=='Y')
{
printf(“输入要搜索的人的姓名:”);
scanf(“%s”,find_name);
//scanf(“%c”和tmp);
搜索\u pb(pb,查找\u名称);
}
如果(cont=='N')中断,则为else;
如果(cont=='\n')继续,则为else;
else printf(“错误:用户输入了“%c”。必须输入“Y”或“N”\N”,
续);
printf(“\N是否要搜索名称(Y/N)?”;
}
返回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[]);
#包括
#包括
#定义最大值20
#定义\u CRT\u SECURE\u NO\u弃用
结构个人信息
{
字符优先[25];
最后一个字符[25];
字符电话[15];
};
结构电话簿
{
结构个人信息个人[MAX];
国际人口;
};
无效添加人(结构电话簿*pb,结构个人信息人);
void search_pb(结构电话簿pb,char find_name[]);

一个快速而肮脏的规避方法是:

void search_pb(struct phone_book pb, char find_name[])
{
    int matches = 0;
    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);

            matches++;
        } 
    }

    if(matches == 0)
    {
        printf("No entries with that name. \n");  
    }
}
void search\u pb(struct phone\u book pb,char find\u name[])
{
int匹配=0;
INTP;
对于(p=0;p

但是,您可以将search_pb()类型更改为int,并在循环之后返回匹配计数,这样您就可以在调用者中打印“no matches”,而不是在函数中打印它们。

如何删除
search_pb
中的
return
?它会停止搜索其他条目。这确实有效,但会导致其他printf出现问题,该printf在名称不在结构中时打印“没有具有该名称的条目”。现在,在说明第二个Albert的详细信息后,它会打印“没有该名称的条目”。然后思考如何防止/规避该问题。我希望你能简单地思考。如果我知道如何回避这个问题,我就不会把这个问题贴在这里了。我几乎没有编程经验。简单地告诉我“思考”并没有多大帮助。一个提示怎么样?一个在循环结束时告诉我们是否找到了一个或多个匹配项的变量怎么样?发出变量为零的消息。我的意思是,这不是很难思考,是吗?