The aim of this project is to implement a simple contacts management system. Use
ID: 3764173 • Letter: T
Question
The aim of this project is to implement a simple contacts management system. Users can search, add, and modify a contact to the initial contacts file. We assume that some contacts are initially stored in a file “Contacts.txt” in the following format: Name# Phone_Number Example of the content of “Contacts.txt”:
John Smith# (313)000-1110
Merry Pierre Laurent # (777)000-3333
Dani Aram# (696)222-5855 Merry Brown#
(222)656-5563 George Green# (896)125-5767
The program should display the following menu: ------------------------ Menu ---------------------------
1. Displayall Contacts 2.
Add a contact 3.
Search for a contact.
4. Modify a contact
5. Help 6.
Quit -----------------------------------------------------
--- Please Enter Your Choice (1-7): Below is a description of each option in the menu:
Option 1 (Display All Contacts): The program calls a function that displays all contacts stored in the file “Contacts.txt”.
Option 2 (Add a contact): The program calls a function that prompts users for the new contact’s Name, phone number and then add it to the file “Contacts.txt”.
Option 3 (Search for a contact): The program calls a function that prompts users for a key word(s) then searches for it in “Contacts.txt” and displays all contacts that match the search criteria. e.g. Given the content of “Contacts.txt”, if user enters the key word “Merry”, your program may display the contacts: Merry Pierre Laurent (777)000-3333 Merry Brown (222)656-5563 1
Option 4 (Modify a contact ): (Start solving this part after we discuss “Arrays” in the class) The program calls a function that prompts users for the desired contact’s name, search for it to display contact information and ask for the modifications and add the new line to the file. To perform this function, the program should: Read the contacts from the file “contacts.txt” and fill out a 2- dimentional array called ‘contacts’. Then perform the modification on the correspondent array element. Finally, it writes all array content back to the file “Contacts.txt
” Option 5 (Help): The program calls a function that displays a detailed message on how to use the program. Option
6 (Quit): The program displays a thank you message and terminates.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void Menu();
void New_Staff();
void Delete_Staff();
void Export_Profile();
struct element{
char id[5];
char name[20];
char phone[10];
}profile;
int main(void){ //The program will continue running until option 4 selected
int a;
for(a=0;;a++){
Menu();
}
}
void Menu() //Main Menu to select option
{
int n;
printf(" **********************************************");
printf(" MAIN MENU");
printf(" **********************************************");
printf(" 1. Add contact");
printf(" 2. Delete a contact");
printf(" 3. Export all contacts to 'output.txt'");
printf(" 4. Exit");
printf(" **********************************************");
printf(" Please enter your option< 1 / 2 / 3 / 4 >: ");
scanf("%d", &n);
switch(n){
case 1:
New_Staff();
break;
case 2:
Delete_Staff();
break;
case 3:
Export_Profile();
break;
case 4:
printf(" *** Thanks for using the program! Goodbye. ***");
exit(0);
break;
default:
printf(" Error! Wrong Number is Entered Please Try Again");
break;
}
}
void New_Staff() //Add New Staff Profile
{
int x;
struct element profile;
printf(" ===Add New contact Profile===");
printf(" Please enter the contact information.");
printf(" Staff ID: ");
scanf("%s", &profile.id);
printf("Name : ");
fflush(stdin);
fgets(profile.name,20,stdin);
for(x=0 ; x<20 ; x++)
{
if(profile.name[x] == ' ')
profile.name[x] = '';
}
printf("Phone : ");
scanf("%s", &profile.phone);
printf(" SYSTEM: New contact is Added Successfully.");
}
void Delete_Staff() //Delete Staff Profile
{
FILE *fRead, *fWrite;
char *TextFile;
char c;
int Delete_Id, temp = 1;
TextFile="output.txt";
fRead = fopen(TextFile, "r");
c = getc(fRead);
while (c != EOF){
printf("%c", c);
c = getc(fRead);
}
rewind(fRead);
printf(" Delete Staff with ID: ");
scanf("%d", &Delete_Id);
Delete_Id=Delete_Id+1;
fWrite = fopen("copy.c", "w");
c = getc(fRead);
while (c != EOF) {
c = getc(fRead);
if (c == ' ')
temp++;
if (temp != Delete_Id){
putc(c, fWrite);
}
}
fclose(fRead);
fclose(fWrite);
remove(TextFile);
rename("copy.c", TextFile);
printf(" The contents of file after being modified are as follows: ");
fRead = fopen(TextFile, "r");
c = getc(fRead);
while (c != EOF) {
printf("%c", c);
c = getc(fRead);
}
fclose(fRead);
}
void Export_Profile() //Export Staff Profile to Text
{
struct element profile;
FILE *fPtr;
fPtr=fopen("output.txt","w");
if (fPtr == NULL)
printf("Error in opening file ");
fprintf(fPtr," %10s %15s %10s ", profile.id, profile.name, profile.phone);
printf(" SYSTEM: All staff profile have been exported to output.txt file");
fclose(fPtr);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.