Trouble with Structure Array and For Loop

Hello! I am doing an assignment for class where I have to give users the option to input details of a phone book structure, and then print them out. You also get the option to remove them, and the structure array will shrink down after you remove the element. However, as of right now, the information isn't being retained in the array and it won't print out. I was wondering if you guys could assist me? Thank you!

The whole problem is at the top of the code, but right now I'm only focused on a-c!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
 /* Write a program that uses an array of structures to hold contact information (first name, last name, 
address, phone number and email) for your friends. The program should allow the user to enter as many 
friends as the user wants. In this program do the following:
a. Create a function to add entries in the phone book.
b. Create a function to delete a specific entry (lookup based on the first name, assuming 
every entry has unique names) from the phone book. 
(Hint: Use Dynamic Memory Allocation to shrink the array after deletion.)
c. Create a function to print valid phone book entries. Do not display phone book entries 
that are invalid or NULL (0). You can assume that all people have unique names.
d. Create a function to randomly select a friend from the phonebook for you to call. */


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

//STRUCTURE FOR PHONE BOOK ENTRIES
typedef struct phone {
       char fname[10];
       char lname[10];
       char addr[30];
       char phonum[15];
       char email[30];
             }emp;

//INITIALIZING FUNCTIONS
void add(emp *, int);
void print(emp *, int);
void remove(emp *, int);

//MAIN FUNCTION
main()

{

int e; //amount of entries in phone book
int sel=0; // user input                                                 
emp emp1[20]={'\0'};

printf("How many entries do you want in your phone book?\n");
scanf("%d", &e);
fflush(stdin);

while(sel!=5)
{
printf("\n\nSelect what you want to do to the phone book:\n\n"); //shows user different options and allows user to enter input
printf("1:   Add an entry\n");
printf("2:   Delete an entry\n");
printf("3:   Print all entries\n");
printf("4:   Randomly select a friend to call\n");
printf("5:   Exit program\n");
printf("\n     Enter number: ");
scanf("%d", &sel);



switch(sel) //switch that executes proper function depending on user input
     {
       case 1:
            add(emp1, e);
            break;    
       case 2:
            remove(emp1, e);
            break;  
       case 3:
            print(emp1, e);
            break;
       default:
            sel=5;
            printf("\nYou are leaving the program!\n\n");
           
     }

} 
      
system("PAUSE");
return 0;      
      
}

//FUNCTION TO ADD ENTRY
void add(emp * emp1, int n)
{
char fname[10]; //declaring variables for this function
char lname[10];
char addr[30];
char phonumm[15];
char email[30];
int i = 0; //test for if element is filled later on in this functin. 0 means no and i means yes.

char t[3]={'\0'}; //established to allow strcmp to compare to a null value

printf("\n\nYou are entering a new phone book entry!\n\n"); //user assigning value to each variable
printf("Enter first name: ");
scanf("%s", &fname);
fflush(stdin);
printf("\nEnter last name: ");
scanf("%s", &lname);
fflush(stdin);
printf("\nEnter address: "); 
scanf("%s", &addr);
fflush(stdin);
printf("\nEnter phone number: ");
scanf("%s", &phonumm);
fflush(stdin);
printf("\nEnter email: ");
scanf("%s", &email);    
fflush(stdin);

for(int c = 0; i=0; c++) //goes through whole array
 {
 if(strcmp(emp1[c].fname, t)==0) //makes sure it enters data only if element is empty
  {
   strcpy(emp1[c].fname, fname); //copies user input into necessary part of structure array
   strcpy(emp1[c].lname, lname);
   strcpy(emp1[c].addr, addr);
   strcpy(emp1[c].phonum, phonumm);
   strcpy(emp1[c].email, email);
   i = 1;
  }
 }  
}


//FUNCTION THAT PRINTS OUT ADDRESSES IN FUNCTIONS
void print(emp * emp1, int n)
{
     
  char t[3]={'\0'}; //established so it can be used to compare each element to 'null'
  printf("\n\nYou are printing out your phonebook!");
  
  for(int x=0; x < n; x++) //for loop to print out correct number of entries. goes through whole array.
    {
      if(strcmp(emp1[n].fname, t)!=0) //makes sure it skips over empty arrays
        {
         printf("\n\n Entry #%d", x+1);
         printf("\n    First Name:   %s", emp1[x].fname);
         printf("\n    Last Name:    %s", emp1[x].lname);
         printf("\n    Address:      %s", emp1[x].addr);
         printf("\n    Phone Number: %s", emp1[x].phonum);
         printf("\n    E-mail:       %s", emp1[x].email);
         } 
    }
     
}


//FUNCTION THAT ALLOWS USER TO REMOVE AN ENTRY
void remove(emp * emp1, int n)
{
 
char out[10]; //initializes variables
char t[3]={'\0'};
int c;

 
printf("\n\nYou can now remove a name from the phonebook!"); //allows user to input name to remove
printf("\nEnter the first name of the entry you'd like to remove: ");     
scanf("%s", &out);
fflush(stdin);

for(int x=0; x<n; x++) //for loop that goes through all the structures
  {
    if(strcmp(out, emp1[x].fname)==0) //frees up space if user input matches the array
      {
       free(emp1[x].fname);    
       free(emp1[x].lname);
       free(emp1[x].addr);
       free(emp1[x].phonum);
       free(emp1[x].email);        
      }      
        
  }


for(c=0; c<20; c++); //for loop to shrink array
  {
   if(strcmp(emp1[n].fname, t)==0) //if element matches null, replaces element with the next function up
     {
       strcpy(emp1[c].fname, emp1[c+1].fname); 
       strcpy(emp1[c].addr, emp1[c+1].addr);
       strcpy(emp1[c].phonum, emp1[c+1].phonum);
       strcpy(emp1[c].email, emp1[c+1].email);
     }
  }
     
     
}
I've messed around with this program a bit, and I'm still having issues. I tried isolating the problem and I think it might be the strcmp or strcpy function tat's the issue? Are those the proper ones to use?
Topic archived. No new replies allowed.