I’m new to C and I’m working on trying to create the typical beginners phonebook app. I’ve seen a lot of different links on google that show examples of the phonebook app, but nothing that shows what I’m trying to accomplish. Basically what I’m wanting to do is create a structure that has first name, last name, and 2 phone numbers, then I want to create 2 different functions for each of those. For each variable in the structure I would have one that sets the input, and then another one that reads the input. This gives me 8 functions, but then I want to add three more functions, one for adding 11each new contract to the structure, one for deleting a contact and then one for viewing the phonebook. Right now I’m focusing on the adding function and viewing function. My issue is that I am getting very weird output.
#include<stdio.h>
#include<conio.h>
typedef struct friends_contact{
char First_Name[10];
char Last_Name[10];
char home[15];
char cell[15];
}fr;
void menu(fr*friends ,int* counter,int user_entry, char* nullStr,int i);
void setFirst(fr*,int *,int i);
char getFirst(fr*,int*,char*,int i);
void setLast(fr*friends, int* counter, int i);
char getLast(fr*friends ,int* counter,char *nullStr, int i);
void add_contact(fr*friends,int* counter,int i);
void show_contact(fr*friends ,int* counter,char *nullStr, int i);
int main()
{
int user_entry=0;
fr friends[5];
int counter=0;
char nullStr[21] = {"\0"};
int i=0;
menu(friends, &counter,user_entry,nullStr,i);
getch();
return 0;
}
void menu(fr*friends,int* counter,int user_entry,char *nullStr,int i)
{
do{
printf("Phone Book Application\n");
printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show phonebook\n5)Exit);
scanf("%d", &user_entry);
if(user_entry==1){
add_contact(friends,counter,i);
}
if(user_entry==4){
show_contact(friends, counter,nullStr,i);
}
}while(user_entry!=5);
}
void setFirst(fr*friends, int* counter, int i)
{
(*counter)++;
printf("Enter a first name \n");
scanf("%s",&friends[*counter-1].First_Name);
}
void setLast(fr*friends, int* counter, int i)
{
(*counter)++;
printf("Enter a first name \n");
scanf("%s",&friends[*counter-1].Last_Name);
}
char getFirst(fr*friends ,int* counter,char *nullStr, int i)
{
for(i=0; i<*counter; i++)
{
if (strcmp(nullStr, friends[i].First_Name) != 0)
{
printf("%s\n", friends[i].First_Name);
}
}
return *friends[i].First_Name;
}
char getLast(fr*friends ,int* counter,char *nullStr, int i)
{
for(i=0; i<*counter; i++)
{
if (strcmp(nullStr, friends[i].Last_Name) != 0)
{
printf("%s\n", friends[i].Last_Name);
}
}
return *friends[i].Last_Name;
}
void add_contact(fr*friends,int* counter,int i)
{
setFirst(friends,counter,i);
setLast(friends,counter,i);
}
void show_contact(fr*friends ,int* counter,char *nullStr, int i)
{
getFirst(friends,counter,nullStr,i);
getLast(friends,counter,nullStr,i);
}
Here is the output that I get now:
Enter a first name
john
Enter a first name
smith
Phone Book Application
1) Add friend
2) Delete friend
3) Show a friend
4) Show phone book
5) Exit
4
john
╝ ♠
smith
my results are equally as weird when i add another name to the list. Whats weird is I only started having these problems after I created the last name function. When I entered just first names it worked fine. Any ideas?
OK, I made some changes and I paste you the whole code (including the main)
I got rid of the variable nullStr. When you want to see if a name is balnk you can use strlen(string) which gives you the size of that string. If the size is 0 it means the string is blank.
Here is the code: