Firstly, please have a look at my question and the coding:
Question(unable to post image and link due to insufficient post count, sorry):
h t t p ://img198.imageshack.us/img198/9537/questionqmc.jpg
(remove the spaces)
Problem:
1. How to delete part of the entry from the address book?
2. Problem retreving address as string from file, if my address contain spaces, it will only read the 1st value (eg. Road 32, it will only read Road, then 32 will assign to the next variable).
3. How to sort the address by name or last time when they are being display?
4. How to compare string and how to change string to upper case (fail to use toupper(VARIABLE))?
5. Is there any weakness of my code and can be improved?
Coding:
...
int main(void)
{
FILE* ABook;
int cont=0,status,content,closeresult;
char fname[30],lname[30],gender[10],addr[20],street[30],city[30],zcode[20],phone[20],email[50];
greeting();
content=selection();
do
{
if(cont==2)
content=selection();
switch(content)
{
case 1:
do
{
ABook=fopen("Add_Book.txt","r");
ofile(ABook);
for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))
printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);
closeresult=fclose(ABook);
cfile(ABook,closeresult);
cont=conti();
if(cont==3)
{
end();
exit(301);
}
}while(cont==1);
break;
case 2:
do
{
ABook=fopen("Add_Book.txt","a");
ofile(ABook);
printf("\n");
insert(ABook);
closeresult=fclose(ABook);
cfile(ABook,closeresult);
cont=conti(cont);
if(cont==3)
{
end();
exit(301);
}
}while(cont==1);
break;
case 3:
do
{
ABook=fopen("Add_Book.txt","r");
ofile(ABook);
search(ABook);
cfile(ABook,closeresult);
cont=conti();
if(cont==3)
{
end();
exit(301);
}
}while(cont==1);
break;
case 4:
do
{
ABook=fopen("Add_Book.txt","r");
ofile(ABook);
printf("\nin process\n");
cfile(ABook,closeresult);
cont=conti();
if(cont==3)
{
end();
exit(301);
}
}while(cont==1);
break;
case 5:
end();
exit(301);
break;
}
}while(cont==2);
return 0;
}
......
void insert(FILE* ABook)
{
int z;
printf("First Name: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("Last Name: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("Gender[M/F]: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("Address: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("Street: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("City: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("Zip Code: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("Phone Number: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
printf("Email Address: ");
while((z = getchar()) != EOF)
fputc(z, ABook);
}
void search(FILE* ABook)
{
int search,status,display=0;
char fname[30],lname[30],gender[10],addr[20],street[30],city[30],zcode[20],phone[20],email[50],surname[30],code[20],pnum[20];
printf("\nSearch by:");
printf("\n1 => Surname:");
printf("\n2 => Zip Code:");
printf("\n3 => Phone Number:");
printf("\n[1/2/3]: ");
scanf("%d",&search);
while(search!=1&&search!=2&&search!=3)
{
printf("\nError!!! Please enter correct code!!!");
printf("\nSearch by:");
printf("\n1 => Surname:");
printf("\n2 => Zip Code:");
printf("\n3 => Phone Number:");
printf("\n[1/2/3]: ");
scanf("%d",&search);
}
if(search==1)
{
printf("Enter the surname: ");
scanf("%s",surname);
for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))
if(strcmp(surname,lname)==0)
{
printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);
display++;
}
if(display==0)
printf("Surname (%s) cannot be found in the Address book!!!\n",surname);
}
else if(search==2)
{
printf("Enter the Zip Code: ");
scanf("%s",code);
for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))
if(strcmp(code,zcode)==0)
{
printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);
display++;
}
if(display==0)
printf("Zip Code (%s) cannot be found in the Address book!!!\n",code);
}
else if(search==3)
{
printf("Enter the Phone Number: ");
scanf("%s",pnum);
for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))
if(strcmp(pnum,phone)==0)
{
printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);
display++;
}
if(display==0)
printf("Phone Number (%s) cannot be found in the Address book!!!\n",pnum);
}
}
......
}


Sign In
Create Account

Back to top









