ADELCHELSEA
ADELCHELSEA
//initializing the header files
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
//defining the properties of the fields used in the program
#define NAMES 50
#define EMAIL 20
#define PHONE 20
#define ADDRESS 8
#define TOWN 10
#define SIZE 500
//declaring the functions
void welcome_note();
void record();
void view();
void search();
void del_rec();
char info[SIZE];
//using stuctures that represent the fields in the program
struct addressbk
{
char name [NAMES] ;
char sname [NAMES] ;
char emailadd ;
char address [ADDRESS];
char town [TOWN];
char phone [PHONE];
char postcode [PHONE];
char country[TOWN];
} addressbk;
//initializing the files used in the program
FILE *Records;
FILE *rectemp;
//initializing of the main function
/****************************************************************************************************/
void main()
{
int choice=0;
//using the system() statement to clear the screen
system("cls");
//using the sysytem (color) to create a user interface
system("color 17");
fflush(stdin);
//initializing the welcome note
welcome_note();
printf("\nEnter your Choice here \t");
scanf("%i",&choice);
fflush(stdin);
//!!using the switch statement to give an option to initialize the functions
switch (choice)
{
case 1:
{
system("cls");
view();
break;
}
case 2:
{
system("cls");
record();
break;
}
case 3:
{ system("cls");
search();
break;
}
case 4:
{ system("cls");
del_rec();
break;
}
case 5:
{
system("cls");
break;
}
default:
{
printf("Please Only Choose Between NO. 1-5\n"); system("pause");
system("cls");
main();
}
}
}
/******************************************************************************************************/
//initializing the record function
void record()
{
char choice2;
do
{ //opening the Records file
Records = fopen("Records.txt","a+");
printf("Enter Name\n");
fflush(stdin);
scanf("%s",addressbk.name);
printf("Enter Second Name\n");
fflush(stdin);
scanf("%s",addressbk.sname);
printf("Enter E-mail address\n");
fflush(stdin);
scanf("%s",addressbk.emailadd);
printf("Enter address\n");
fflush(stdin);
scanf("%s",addressbk.address);
printf("Enter town\n");
fflush(stdin);
scanf("%s",addressbk.town);
printf("Enter Country\n");
fflush(stdin);
scanf("%s",addressbk.country);
printf("Enter Post Code no\n");
fflush(stdin);
scanf("%s",addressbk.postcode);
printf("Enter Phone Number no\n");
fflush(stdin);
scanf("%s",addressbk.phone);
//printing all the entries to the main file (Records.txt)
fprintf(Records,"%s %s %s %s %s %s %s %s \n",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.town,addressbk.country,addressbk.postcode,addressbk.phone);
fflush(stdin);
//closure of the records file.
fclose(Records);
//initializing the choice character to give on optin to continue
printf("WOULD LIKE TO CONTINUE? Y/N \n");
scanf("%c",&choice2);
fflush(stdin);
if (choice2=='n'||choice2=='N')
{
system("cls");
main();
}
}while(choice2=='y'||choice2=='Y');
}
/****************************************************************************************************************/
//the view function
void view()
{
char choice3;
//opening the Records file
Records = fopen("Records.txt","a+");
do
{
fgets(info,SIZE,Records);
printf("%s\n",info);
}while(!feof(Records));
fclose(Records);
system("pause");
printf("Search for records??(Y/N)\n");
scanf("%c",&choice3);
fflush(stdin);
if (toupper(choice3)=='Y')
{
search();
}
else if(toupper(choice3)=='N')
{
fclose(Records);
system("pause");
system("cls");
main();
}
else
{
fclose(Records);
system("cls");
}
}
/********************************************************************************************/
//the main menu/welcome function
void welcome_note()
{
//this statement displays the system time and date
printf("\t\t ADDRESS BOOK\n\n\n");
printf("\t\t programmer: Elham Efranian \n\n\n");
printf("\t\t ----------------------------------------\n");
printf("\t\t - CHOOSE ONE BETWEEN (1-5) -\n");
printf("\t\t ----------------------------------------\n");
printf("\t\t - 1 : VIEW RECORDS -\n\n");
printf("\t\t - 2 : MAKE NEW ENTRIES -\n\n");
printf("\t\t - 3 : SEARCH RECORDS -\n\n");
printf("\t\t - 4 : DELETE RECORDS -\n\n");
printf("\t\t - 5 : EXIT -\n\n");
printf("\t\t ----------------------------------------\n");
}
/***********************************************************************************************/
//the search funtion
void search()
{
//declaring the target variable Target
char Target[SIZE];
//
int Found=0;
if((Records=fopen("Records.txt","r"))==NULL)
printf("NO RECORDS THE FILE EMPTY!!!\n\n");
else
{
system("cls");
printf("\n\n");
printf("\t----------------- SEARCH --------------");
printf("\n\n");
printf("\tEnter Name to search:");
fflush(stdin);
scanf("%s",Target);
while(!feof(Records)&& Found==0)
{
fscanf(Records,"%s %s %s %s %s %s %s %s ",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.town,addressbk.country,addressbk.postcode,addressbk.phone);
if(strcmp(Target,addressbk.name)==0)
Found=1;
}
if(Found)
{
printf(".......FILE FOUND\n");
system("pause");
system("cls");
printf("1 FILE FOUND : \n\n\n");
printf("First Name: %s\n",addressbk.name);
printf("Second Name: %s\n",addressbk.sname);
printf("E-mail: %s\n",addressbk.emailadd);
printf("P.O. BOX : %s\n",addressbk.address);
printf("Town: %s\n",addressbk.town);
printf("Country: %s\n",addressbk.country);
printf("Post Code: %s\n",addressbk.postcode);
printf("Phone Number: %s\n",addressbk.phone);
system("pause");
main();
}
else if(!Found)
{
printf("SORRY FILE NOT FOUND\n");
system("pause");
main();
}
fclose(Records);
}
}
/***************************************************************************************************/
void del_rec()
{
char Target[SIZE];
int Found=0;
system("cls");
rectemp=fopen("rectemp.txt","w");
if((Records=fopen("Records.txt","r"))==NULL)
printf("NO RECORDS FILE EMPTY!!!\n\n");
else{
printf("\n\n");
printf("\t--------------- DELETE ----------------");
printf("\n\n");
printf("\tEnter Name to Delete: ");
fflush(stdin);
gets(Target);
while(!feof(Records))
{
fscanf(Records,"%s %s %s %s %s %s %s %s ",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.town,addressbk.country,addressbk.postcode,addressbk.phone);
if(feof(Records))
break;
if(strcmp(Target,addressbk.name)!=0)
fprintf(rectemp,"%s %s %s %s %s %s %s %s \n",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.town,addressbk.country,addressbk.postcode,addressbk.phone);
else {
Found=1;
printf(".......FILE FOUND\n");
system("pause");
printf("First Name:%s\n",addressbk.name);
printf("Second Name:%s\n",addressbk.sname);
printf("E-mail:%s\n",addressbk.emailadd);
printf("P.O. BOX : %s\n",addressbk.address);
printf("Town: %s\n",addressbk.town);
printf("Country: %s\n",addressbk.country);
printf("Post Code: %s\n",addressbk.postcode);
printf("Phone Number: %s\n",addressbk.phone);
fflush(stdin);
system("pause");
}
}
if (!Found)
{
printf("\tRECORD NOT FOUND\n");//incase no files are located
}
printf("\tRECORD DELETED!!\n");
fclose(Records);
fclose(rectemp);
remove("Records.txt");
rename("rectemp.txt","Records.txt");
main(); }
}