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