Reputation: 11
I was confused on the concept of passing a structure of arrays in C programming. Most examples involve passing an array of structures with the values already provided. I had to ponder, reread, and it clicked when I saw someone take input and put the struct members in each individual element. It got my brain moving and I created the program below. I needed to push the structure array through user-defined functions. I got it to work on CodeBlocks but not in Cygwin. Input is always welcomed.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NAME 50
#define PHONE 10
#define MAX 5
// Declare a global structure to pass
// it to a function
struct contact{
char name[NAME];
char phone[PHONE];
};
//Array of structure
struct contact * friend[MAX]= {0};
//Declaration of the function
void loadMenu(void);
void insertEntry(struct contact *friend);
void displayEntry(struct contact *friend);
int main(){
loadMenu();
return 0;
}
void loadMenu(){
int choice;
printf("\t\tWelcome to Your PhoneBook \n");
printf("\t\n 1> Insert new Entry");
printf("\t 2> Display Entry");
printf("\t 3> Exit\n");
printf("\n Enter your choice <1-3>: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insertEntry(friend);
break;
case 2:
displayEntry(friend);
break;
case 3:
exit(0);
break;
default:
printf("**Invalid Input.**\n");
}//end switch
} //end loadMenu
void insertEntry(struct contact *friend){
char choice1;
FILE *pWrite;
int i;
do{
if(i == 0){
//opening the phoneBook file
pWrite = fopen("phoneBook.txt", "w");
} else {
pWrite = fopen("phoneBook.txt", "a+");
}
// Make sure do not enter more than MAX number of records
if(i >= MAX){
printf("Reached maximum number of entries that can be filled\n");
break;
}
while (1){
printf("Enter friend's Name\n");
scanf("%s", friend[i].name);
if(strlen(friend[i].name) == 0){
printf("Name cannot be empty\n");
} else {
break;
}
}
while (1){
printf("Enter friend's Phone number\n");
scanf("%s", friend[i].phone);
if(strlen(friend[i].phone) == 0){
printf("Phone number cannot be empty\n");
} else {
break;
}
}
fprintf(pWrite, "%s\t%s \n", friend[i].name, friend[i].phone);
i++;
fclose(pWrite);
fflush(stdin);
printf("Do you to insert another entry? Y or N \n");
scanf("%c", &choice1);
} while (choice1 == 'y' || choice1 == 'Y');
loadMenu();
}// end insertEntry
void displayEntry(struct contact *friend){
//Print the phone book's valid current entries. Do not display phone book entries that are invalid or NULL (0).
FILE *pRead;
int i;
char reRun;
do{
pRead = fopen("phoneBook.txt", "r");
if (pRead != NULL){
printf("\nName\t Phone Number\n\n");
fscanf(pRead, "%s %s", friend[i].name, friend[i].phone);
while (!feof(pRead)){
printf("%s\t%s\n", friend[i].name, friend[i].phone);
fscanf(pRead, "%s%s", friend[i].name, friend[i].phone);
}
fclose(pRead);}
else{
printf("\n File not opened\n");
}
fflush(stdin);
printf("Press Y to Execute the Program Again \n");
scanf("%c", &reRun);
loadMenu();
} while (reRun == 'y' || reRun == 'Y');
}//end displayEntry
Upvotes: 1
Views: 537
Reputation: 124997
Your problem is that you're not passing anything at all into your various functions. Look at this:
void insertEntry( ) {
struct contact * con[MAX]= {0};
That means, whenever insertEntry()
is called, it'll create a local variable con
that is a pointer to an array of contact
structures. And when that function exits, poof, con
disappears and anything in it goes away. Same thing for your other functions, e.g.:
void displayEntry(){
struct contact * con[MAX]= {0};
What you want to do is to create that array once, and then pass it into your functions as aa parameter. Those ()
at the ends of your function names are meant for parameters. So you might have:
void insertEntry(struct contact * con) {
int i = 0;
//...
Create the array in your main()
function:
struct contact * friends[MAX]= {0};
and then when you call insertEntry
include it as a parameter:
insertEntry(friends);
(I've called the array friends
just to give it a different name from the parameter.) The idea here is that insertEntry
shouldn't create its own array, but rather use the one that you tell it about.
BTW, you can clean up the code some by giving the type struct contact *
a name using typedef
. I'll leave that alone for now, but you'll surely find examples of how to do that in your textbook.
Upvotes: 1