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

Recommended Answers

All 6 Replies

That's because not always all the variables are initiated with a value of 0 or NULL. So you must start your code initiating all the variables of your struct as 0 or NULL, or the value that you want. In your case, when you receive an empty input, it will initiate your array as "\0", something different from null. For that I recommend you to initiate your arrays like this:

array[0]='\0';

and check if its empty like this:

if(array[0]=='\0'){
    //blah
}

first your code is not readable (this problem you must learn to make your code readable by using spaces and tabs in your code)

you can check if array is empty or not

using:

strlen(friend1[contact_checker].name1)==0;

or

strcmp(friend1[contact_checker].name1,"")==0;

or 

 friend1[0].name1=='\0';

your code will be that after using one condition from three condition:

#include<stdio.h>
#include<conio.h>
#include<string.h>


typedef struct{
char name1[15];
char name2[15];
char ph_number[15];
char email[25];
}contact;

contact friend1[3];

int menu(void);
void contact_check(int);


int main()
{

     int select1,a=0;
     clrscr();

      select1=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++;
                  select1=menu();

                }while (select1!=2);

                 break;
         }
      case 2:

                contact_check(a);
                 break;
    }

         contact_check(a);

getch();
return 0;
}

int menu()
{
    int select1;

    printf("\t\t\tPhonebook\n");
    printf("\t\t\t=========\n");
    printf("1:New contact\n2:Contact list\n");
    scanf ("%d",&select1);

    return select1;
}

void contact_check(int a)
{
  int contact_checker=0,contact_print;

   if(strlen(friend1[contact_checker].name1)==0&&strlen(friend1[contact_checker].name2)==0     &&strlen(friend1[contact_checker].ph_number)==0&&strlen(friend1[contact_checker].email)==0)
   {
     printf ("No contacts in your phonebook");
   } 
  else
   {
      for (contact_print=0;contact_print<a;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);
         }
   }


}   

i had remove all global variables and put them into main function and functions

i had change main function in your code

#include<stdio.h>
#include<conio.h>
#include<string.h>


typedef struct{
char name1[15];
char name2[15];
char ph_number[15];
char email[25];
}contact;

//int select2;
contact friend1[3];

int menu(void);
void contact_check(int);


int main()
{

     int select,a=0;
     clrscr();

     do
     {
        select=menu();

         switch(select)
         {
            case 1:
             {
                  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++;
                  break;      
             }
             case 2:
             {
                 contact_check(a);
                 break;  
             }
         }

     }while(select!=3)


getch();
return 0;
}

int menu()
{
    int select1;

    printf("\t\t\tPhonebook\n");
    printf("\t\t\t=========\n");
    printf("1:New contact\n2:Contact list\n3:Exit\n");
    scanf ("%d",&select1);

    return select1;
}

void contact_check(int a)
{
  int contact_checker=0,contact_print;


   if(strlen(friend1[contact_checker].name1)==0&&strlen(friend1[contact_checker].name2)==0
   &&strlen(friend1[contact_checker].ph_number)==0&&strlen(friend1[contact_checker].email)==0)
   {
     printf ("No contacts in your phonebook");
   } 
  else
   {
      for (contact_print=0;contact_print<a;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);
         }
   }


}   
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.