C语言的基本结构帮助

时间:2018-04-05 20:56:41

标签: c

如果用户试图搜索之前未在结构中输入的名称,我需要使第二个函数打印“没有该名称的条目”。除了这一部分,一切都很好。我当前的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[]);

1 个答案:

答案 0 :(得分:0)

所以我在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");
}

正如您所看到的,现在您的函数在找到匹配项后立即返回并打印匹配项,否则默认为打印未找到任何条目。 希望有所帮助!

相关问题