Hi people, I am a beginner C learner and I'm having a bit of trouble checking a structure to see if its empty or not.
I am writing a program for a phonebook/contact book and I have everything working actually but I want my program to check all arrays in each structure, and if they are empty, it prints "No contacts in your phonebook" when the user prompts it by pressing 2. I tried using an if statement to check if all arrays are NULL, then it should print the message but its not, thus indicating that they aren't NULL or empty. I say that because the program prints the alternate (else statement, which is empty spaces). Is there a way I can fix this or do it simpler. Any help would be appreciated. Thanks and codes is below.
#include <stdio.h>
typedef struct{
char name1[15];
char name2[15];
char ph_number[15];
char email[25];
}contact;
int select1,select2,contact_checker,contact_print,a;
void menu();
void contact_check();
contact friend1[3];
main()
{
menu();
switch (select1)
{
case 1:
{
do{
printf ("First name\n");
scanf ("%s", friend1[a].name1);
printf ("\nLast name\n");
scanf ("%s", friend1[a].name2);
printf ("\nPhone number\n");
scanf ("%s", friend1[a].ph_number);
printf ("\nE-mail\n");
scanf ("%s", friend1[a].email);
a++;
menu();}while (select1!=2);
break;
}
case 2:
contact_check();
break;}
contact_check();
}
void menu()
{ printf("\t\t\tPhonebook\n");
printf("\t\t\t=========\n");
printf("1:New contact\n2:Contact list\n");
scanf ("%d", &select1);
}
void contact_check()
{for (contact_checker=0;contact_checker<3;contact_checker++)
if ((friend1[contact_checker].name1==NULL)&&(friend1[contact_checker].name2==NULL)&&(friend1[contact_checker].ph_number==NULL)&&(friend1[contact_checker].email==NULL))
{printf ("No contacts in your phonebook");
}else{for (contact_print=0;contact_print<3;contact_print++)
{ printf ("\n%s %s\n", friend1[contact_print].name1,friend1[contact_print].name2);
printf ("%s\n", friend1[contact_print].ph_number);
printf ("%s\n", friend1[contact_print].email);}
}
}