-1

I need to make the second function print "no entries with that name" if the user attempts to search for a name not previously entered in the structure. Everything is working fine except for this part. The current else statement I have works but it outputs the line every time and not just when the name is not entered in the structure. Any help on how to change it?

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

I was given the main function phone_book.c to work with so I just had to make the functions above and a header file:

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;
}

I also already made the necessary header file 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
    Please edit your question for more readable code formatting. I provided an example in the first code quote. – Yunnosch Apr 05 '18 at 21:00

1 Answers1

0

So I entered your code in repl.it (https://repl.it/repls/SphericalFuchsiaIntroductory) and stated narrowing down the problem. From my tests this problem only happens when you enter 2 or more names into the struct, leading me to believe that the function is being called twice or something.

The problem with your code was the else statement inside of the for loop, your loop looped over all contacts and searched for the matching one, if there is only one contact then the else will trigger normally however if there is more than one it will trigger as many times as the loop loops. To fix it on repl.it (click the link if you want to see the full thing) I changed your function to this:

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");
}

As you can see, now your function returns as soon as it found a match and prints the match, otherwise it defaults to printing no entries found. Hope that helped!

Raid55
  • 85
  • 2
  • 7
  • Np, and don't mind the -1 you got on your question, the stack overflow community can be quite a pain, useful but very harsh. – Raid55 Apr 05 '18 at 22:07