-4

I'm creating a "phonebook" program for school. Basically I have to get a user to input the names and phone numbers of their contacts into two different arrays. I got that part down, but after I have to give the user an option to search the contacts either via name or phone number and I'm running into some issues.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int main()
{
    int arraysize;
    printf("How many phone numbers will you be entering: ");
    scanf("%d", &arraysize);

    int * phoneNumbers = malloc(arraysize);
    char * names = malloc(arraysize);

    for (int i = 0; i < arraysize; ++i)
    {
        printf("Please enter the name of person %d: ", i + 1);
        scanf("%s", &names[i]);
        printf("Please enter %s's phone number: ", &names[i]);
        scanf("%d", &phoneNumbers[i]);
    }
    char * searchOption;

    printf("Would you like to seach via name or phone number? ");
    scanf("%s", &searchOption);

    if (strcmp(searchOption, "name") == 0)
    {
        char * searchName;
        int element;
        bool stop = false;
        while (stop = false)
        {
            searchName = NULL;
            printf("\nPlease enter the name you wish to search for: ");
            scanf("%s", &searchName);
            for (int i = 0; i < arraysize; ++i)
            {
                if (strcmp(searchName, names[i]) == 0)
                {
                    element = i;
                    stop = true;
                    break;
                }
            }
            if (stop = false)
            {
                printf("\nname not found, please search again!");
            }
        }
    }

I'm pretty sure it's crashing at the strcmp() call but I have no idea why

zman419
  • 39
  • 1
  • 1
  • 5
  • `while (stop = false)` -- That's an assignment. It should be `while (stop == false)`. Actually, no, it shouldn't; it should be `while (!stop)`. Same thing for the `if` near the end. – Keith Thompson Feb 18 '16 at 22:31
  • Did you enable compiler warnings? I imagine the compiler would have choice words for your use of `char *searchOption[256];`. – EOF Feb 18 '16 at 22:32
  • @EOF: Passing it to `strcmp` should have caused a warning (or a fatal error). Passing it to `scanf` is equally incorrect, but won't necessarily trigger a warning from an insufficiently clever compiler. (gcc is sufficiently clever). – Keith Thompson Feb 18 '16 at 22:36
  • You changed the code in your question in response to the answers, without mentioning that you changed it. **Please don't do that.** Originally you had `char * searchOption;`. You changed it to `char * searchOption[256];`, an array of 256 pointers. Don't try to "fix" the code in your question; doing so can invalidate both the question and the answers. I'm going to roll back your latest edit. – Keith Thompson Feb 18 '16 at 22:39
  • @EOF: The code was edited. I've rolled it back. – Keith Thompson Feb 18 '16 at 22:41
  • Not it's crashing at the string compare with names[i]. I've edited the program to include all the code because it's declared at the very begining – zman419 Feb 18 '16 at 22:54
  • @zman419: `char *names` is *probably* not what you want (you certainly don't use it correctly). You want `char **names`, because every name is a separate string. – EOF Feb 18 '16 at 23:20

3 Answers3

1

The problem is, in

scanf("%s", &searchOption);

you're doing it wrong.

You need to

  • First allocate memory to searchOption pointer (or make it an array).
  • pass searchOption, not &searchOption to scanf().

Same issue appears for char * searchName; also later in the code.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The crash is not directly caused by the strcmp() call.

This line is at fault:

scanf("%s", &searchOption);

First of all, there is a type mismatch. For %s you have to pass an argument of type char*, not char** as you did. To fix this, remove the take-address (&) operator.

Additionally, you need to allocate memory to scan into. The scanf function doesn't do that for you. As a quick and dirty fix, you can simply make searchOption an array. Replace its declaration with

char searchOption[100];

or such to fix this. You also want to limit the number of characters scanf() attempts to read:

scanf("%100s", searchOption);

so there can't be a buffer overflow.

An even better approach is to use fgets() instead of scanf(). The fgets() function is suited better for what you want to do. Read the manual for how to use fgets().

As Alter Mann said, the same problem occurs a second time further down. You need to repair that, too.

fuz
  • 88,405
  • 25
  • 200
  • 352
0

Also i think in strcmp you have used "name" whereas you declared it as names before.If you correct this maybe program works