the only problem i face here i want an array for names and one for mobile numbers which i did in addphonenumber(); funtion now i need to take name from file into serchphonebook(); function and compare it with what user search for but the problem that i face is that fgets(); take full name with number so i cant compare name of what stored in phonebook with what user searched for NOTES: the names need to be full like "andy ramsy kamal" with spaces , and if user searchd for "an" for example the code need to find all names start with "an" with there phone numbers
#include<stdio.h>
void MainMenu();
int AddPhoneNumber();
void SearchPhoneBook();
void DeletePhoneNumber();
void DispalyPhoneBook();
void main()
{
MainMenu();
}
void MainMenu()
{
int Choice;
printf("\t****Main Menu****\n");
printf("1)Add Phone Number\n");
printf("2)Search Phone Book\n");
printf("3)Delete Phone Number\n");
printf("4)Dispaly Phone Book");
printf("Choose your Number\n");
scanf("%d", &Choice);
switch (Choice)
{
case 1:
AddPhoneNumber();
break;
case 2:
SearchPhoneBook();
break;
case 3:
DeletePhoneNumber();
break;
case 4:
DispalyPhoneBook();
break;
case 5:
exit(0);
default:
MainMenu();
}
}
int AddPhoneNumber()
{
FILE *ph;
ph = fopen("C:/Users/ec/Desktop/PhoneBook.txt", "a");
char Name[3][50];
char temp;
char Mobile[3][11];
for (int i = 0; i < 3; i++)
{
printf("Enter Name\n");
scanf("%c", &temp);//temp statment to clear buffer,temp will store sapce taken after pressing number 1
scanf("%[^\n]", Name[i]);//beacuse i hit enter when i choosed number 1 the compiler stored enter or null into string first and that why i use temp to avoid the problem
printf("Enter Mobile Number");
scanf("%s", &Mobile[i]);
fprintf(ph, "%s %s\n", Name[i],Mobile[i]);
}
printf("%s %s\n", Name[0],Mobile[0]);
printf("%s %s\n", Name[1], Mobile[1]);
printf("%s %s\n", Name[2], Mobile[2]);
fclose(ph);
MainMenu();
}
void SearchPhoneBook()
{
FILE *ph;
ph = fopen("C:/Users/ec/Desktop/PhoneBook.txt", "r");
printf("Enter the name you want to search for\n");
char Name[3][50];
double Mobile[11];
char temp;
scanf("%c", &temp);
char Search[20];
scanf("%s", Search);
for (int i = 0; i < 3; i++)
{
fgets(Name[i], 25, ph);
}
printf("%s", Name);
MainMenu();
}
void DeletePhoneNumber()
{
printf("Works");
MainMenu();
}
void DispalyPhoneBook()
{
printf("Works");
MainMenu();
}