I'm having trouble understanding how to get my phonebook to print out it's entries. If I just have one entry, it will correctly display it but if I have 2 or more, it leaves out chunks of that entry's data.
Any help is appreciated and thanks in advance.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct entry{
char firstN[50];
char lastN[50];
char phoneN[50];
}phone;//end struct
void AddNew (phone * );
void ShowBook (phone * );
int total=0;
int main(void){
int choice=0;
phone *phonebook = (phone*) calloc(1, sizeof(phone));
if (phonebook == NULL){
printf("No remaining memory, now exiting!");
return 1;
}
while(choice != 3){
printf("\nWhat would you like to do?\n\n");
printf("1.) Add Friend\n");
printf("2.) Show Phone Book\n");
printf("3.) Exit\n\n");
scanf("%d", &choice);
switch(choice){
case 1:
AddNew(phonebook);
break;//end case 1
case 2:
ShowBook(phonebook);
break;//end case 2
case 3:
printf("Goodbye!");
free(phonebook);
return 0;
break;//end case 3
default:
printf("Choice not in range. Please select a valid option.\n");
break;//end default
}//end switch
}//end while loop
}//end main
void AddNew (phone *phonebook){
total++;
char *firstname=(char *)calloc(50, sizeof(char));
char *lastname=(char *)calloc(50, sizeof(char));
char *phonenumber=(char *)calloc(50, sizeof(char));
phone *temp = (phone*) realloc(phonebook, sizeof(phone*)*total);
phonebook = temp;
printf("What is your friend's first name?\n");
scanf("%s", firstname);
printf("What is your friend's last name?\n");
scanf("%s", lastname);
printf("What is your friend's number?\n");
scanf("%s", phonenumber);
strcpy(phonebook[total-1].firstN, firstname);
strcpy(phonebook[total-1].lastN, lastname);
strcpy(phonebook[total-1].phoneN, phonenumber);
}
void ShowBook (phone * phonebook){
printf("Your Entries- \n");
int x=0;
for(x=0;x<total;x++){
printf("%d.\t", x+1);
printf("%s, %s\t %s\n", phonebook[x].lastN, phonebook[x].firstN, phonebook[x].phoneN);
}
}