0

Hi I am currently having a problem with my program. When i enter a phone number char, and compare it with a different phone number char, the answer comes back false.

Here my function searches the "findContact" function for a exact number. The getTenDigitPhone is the function to get the phone number. I end up getting the * Contact NOT FOUND * regardless if it matches or not

void searchContacts(const struct Contact contact[], int size) {
    char phone[11];
    int searchIndexContact;
    printf("Enter the cell number for the contact: ");

    getTenDigitPhone(phone);
    searchIndexContact = findContactIndex(contact, size, phone);

    if (searchIndexContact > -1) {
        printf("\n");
        printf("Contact found:\n");
        displayContact(&contact[searchIndexContact]);


    }
    else {
        printf("*** Contact NOT FOUND ***\n");
    }
}

** Here is the getTenDigitPhone function

void getTenDigitPhone(char telNum[11])
{
    int needInput = 1;

    while (needInput == 1) {
        scanf("%10s", telNum);
        clearKeyboard();

        // (String Length Function: validate entry of 10 characters)
        if (strlen(telNum) == 10)
            needInput = 0;
        else
            printf("Enter a 10-digit phone number: ");
    }
}

And here is the findContactIndex (to find out if the numbers match)

int findContactIndex(const struct Contact contacts[], int size, const char cellNum[])
{

    int i;
    int value = 0;
    for (i = 0; i < size; i++) {
        if (contacts[i].numbers.cell ==  cellNum);{
            printf(" %s    %s",contacts[i].numbers.cell , cellNum);
            value == 1;

        }

    }

    if (value == 1) {
        return value;
    }
    if (value == 0) {
        return -1;
    }

}
Andrew
  • 3
  • 1
  • you need to use [`strcmp`](https://linux.die.net/man/3/strcmp) to compare strings.. right now you're comparing pointers, and the pointers are different – yano Jan 12 '18 at 03:27
  • @yano for findContactIndex? – Andrew Jan 12 '18 at 03:29
  • yes, any time you want to compare if 2 strings are the same string, you use `strcmp` and friends (or roll your own). Take a look [here](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) as well. – yano Jan 12 '18 at 03:31

1 Answers1

2

Enable your compiler's warnings! It would have found your problems. For example, with gcc, use at least

gcc -Wall -Wextra -pedantic ...

In findContactIndex,

value == 1;

is wrong. You were probably going for

value = 1;

but it should be

value = i;
break;

or just

return i;

since the function should return the index of the match. That means

int value = 0;
...
if (value == 1) {
    return value;
}
if (value == 0) {
    return -1;
}

should be

int value = -1;
...
return value;

or just

return -1;

Unrelated to your question, in findContactIndex,

if (...);{

should be

if (...) {

As you currently have it, the result of the conditional expression is disregarded, then the following block is executed unconditionally.


Unrelated to your question, in findContactIndex,

contacts[i].numbers.cell == cellNum

should be

strcmp(contacts[i].numbers.cell, cellNum) == 0

You are currently comparing the pointers instead of the strings.


Fixed:

int findContactIndex(const struct Contact contacts[], int size, const char cellNum[])
{
    int i;
    for (i=0; i<size; ++i) {
        if (strcmp(contacts[i].numbers.cell, cellNum) == 0) {
            return i;
        }
    }

    return -1;
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • The compiler doesn't care what the prof gave. The order of the two needs to be the same. You'll need to change the order of the values in the literals, or you'll need to change the order of the fields in `struct Address`. – ikegami Jan 12 '18 at 04:12
  • We should have made a chat for this X_X. So let's delete our comments. – ikegami Jan 12 '18 at 04:18