It's like this. Using C programming, we were told to make a phonebook where we can add entries, sort by first name, sort by last name, and delete entries. It should also contain the name, address, telephone number and mobile number. Our teacher told us to use data structures. The thing is, we never got that far in our discussion in class, and we are supposed to pass this on October 9. We're working on the program now, but help is really appreciated...Thanks in advance! :D
Can anyone help me with this C programming problem?
this might help, just need some addition and revision, at least you know the logic how to create it.
#include %26lt;ctype.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;io.h%26gt;
#include %26lt;conio.h%26gt;
#define PHONE_MIN 7
// the info structure
struct strc{
int id;
char name[20];
char address[25];
char phone[20];
}info;
void getentry();
void putentry(int);
void delentry(int);
int countentry();
int readentry(int,int);
void searchentry();
int main(void)
{
register char ch;
register int sz;
int id;
char intr[]="################################...
"## Insert Entry (I) || call by ID (C) || See All (A)#\n"
"## Edit Entry (T) || delete (D) || search (S) || Exit (X) #\n"
"#####################################...
//Inserting the Main Menu
puts(intr);
do{
ch=toupper(ch=getch());
fflush(stdin);
switch(ch)
{
case 'I':
//Inserting a new Entry
printf("Inserting Entry Number: %d\n", (countentry()+1));
//Get the new Entry from the user
getentry();
//Insert that entry in the file
putentry(0);
puts("Done");
puts(intr);
break;
case 'C':
//Calling an entry by its ID
//checking if we have the file
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
//getting the entry ID which should be not greater than the entries total
printf("You have %d Entries\n", countentry());
puts("Enter the ID of your entry");
do{
scanf("%d", %26amp;id);
fflush(stdin);
if(id%26gt;sz) printf("You have only %d entries\ntry again\n", sz);
}while(id%26gt;sz);
//showing the chosen entry
puts("+---+----------------------+---...
printf("|ID | %-20s| %-25s| %-20s|\n", "NAME","ADDRESS","PHONE #");
readentry(id,1);
puts("+---+----------------------+---...
puts("Done");
puts(intr);
break;
case 'A':
//showing all the entries
//checking if we have the file
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
//showing all entries
puts("Displaying All Entries");
puts("+---+----------------------+---...
printf("|ID | %-20s| %-25s| %-20s|\n", "NAME","ADDRESS","PHONE #");
for(id=1; id%26lt;=sz ; id++)
{
readentry(id,1);
//pause every 20 entries
if(id==20)
{
puts("+---+----------------------+-...
puts("Press any key to continue..");
getch();
}
}
puts("+---+----------------------+---...
puts("Done");
puts(intr);
break;
case 'T':
//editing an entry
//checking if we have the file
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
puts("Editing Entry");
puts("Enter ID of your entry");
//getting the entry ID which should be not greater than the entries total
do{
scanf("%d", %26amp;id);
fflush(stdin);
if(id%26gt;sz) printf("You have only %d entries\ntry again\n", sz);
}while(id%26gt;sz);
//diplaying the entry
puts("+---+----------------------+---...
printf("|ID | %-20s| %-25s| %-20s|\n", "NAME","ADDRESS","PHONE #");
readentry(id,1);
puts("+---+----------------------+---...
//Get the new Entry from the user
getentry();
//Insert that entry in the file in the same place
putentry(id);
puts("Done");
puts(intr);
break;
case 'D':
//Deleting an entry
//checking if we have the file
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
puts("Deleting Entry");
puts("Enter ID of your entry");
//getting the entry ID which should be not greater than the entries total
do{
scanf("%d", %26amp;id);
fflush(stdin);
if(id%26gt;sz) printf("You have only %d entries\ntry again\n", sz);
}while(id%26gt;sz);
//diplaying the entry
puts("+---+----------------------+---...
printf("|ID | %-20s| %-25s| %-20s|\n", "NAME","ADDRESS","PHONE #");
readentry(id,1);
puts("+---+----------------------+---...
//deleting
delentry(id);
puts("Done");
puts(intr);
break;
case 'S':
//Searching for an entry
//checking if we have the file
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
//searching
searchentry();
puts(intr);
break;
case 'X':
//Exit
printf("%c\n",ch);
printf("Ending...\n\n");
default:
;
}
}while(ch!='X');
return 0;
}
//getting the user's input
void getentry()
{
register int i;
//using fgets to get the user's input
//so we avoud the array overflow
puts("Full Name:");
fgets(info.name, 20, stdin);
//must flush the input stream before useing fgets again
fflush(stdin);
//checking if the user passed this input
if(info.name[0]=='\n') info.name[0]='\0';
//removing the newline from the string
else strtok(info.name, "\n");
puts("address:");
fgets(info.address, 25, stdin);
fflush(stdin);
if(info.address[0]=='\n') info.address[0]='\0';
else strtok(info.address, "\n");
do{
puts("phone #:");
fgets(info.phone, 20, stdin);
fflush(stdin);
//user must enter some number
if(info.phone[0]=='\n') puts("You should enter a phone #");
else {
strtok(info.phone,"\n");
//the number shouldn't contain characters
for(i=0;info.phone[i];i++)
if(!isdigit(info.phone[i]))
{
puts("You can only enter digits");
info.phone[0]='\n';
break;
};
//the number is less than 7 numbers
if(i%26lt;PHONE_MIN %26amp;%26amp; info.phone[0]!='\n')
{
puts("The Phone # should contain more than 6 digits");
info.phone[0]='\n';
}
}
}while(info.phone[0]=='\n');
//getting the entry id
if(info.id=countentry()) info.id++;
else info.id=1;
}
//inserting the entry to the file
void putentry(register int id)
{
FILE* fp;
register int sz;
//checking if the file exists
if(!(sz=countentry()))
{
//creat one and close it
fp=fopen("list.phn", "a");
fclose(fp);
id=1;
}
//open it again for binary reading and writing
if(!(fp=fopen("list.phn", "r+b")))
puts("Couldn't find the entry file");
else
{
if(id)
{
//if we have and id so we're editing an entry
//so we need to seek to the same place
info.id=id;
fseek(fp, --id*sizeof(struct strc), SEEK_SET);
}else fseek(fp, sz*sizeof(struct strc), SEEK_SET);//we don't have id seek to end
//writing the structure
fwrite(%26amp;info, sizeof(struct strc), 1, fp);
fclose(fp);
}
}
//that's for checking the if the file ex
// ists and return the number of entires
int countentry()
{
FILE* fp;
register int sz;
//opening binary readonly
if(!(fp=fopen("list.phn", "rb")))
return 0;
else{
//dividing the file size over the size of the structure
//to get the total
sz=filelength(fileno(fp));
sz=sz/sizeof(struct strc);
fclose(fp);
return sz;
}
}
//read the entry by its id and displayin
// g result on demand
int readentry(register int id,register int display)
{
FILE* fp;
//opening binary readonly
if(!(fp=fopen("list.phn", "rb")))
return 0;
else{
//if we have an id seek to it
if(id%26gt;1) fseek(fp, --id*sizeof(struct strc), SEEK_SET);
fread(%26amp;info, sizeof(struct strc), 1, fp);
//if we need to print the result
if(display)
{
puts("+---+----------------------+---...
printf("|%-3d| %-20s| %-25s| %-20s|\n", info.id,info.name,info.address,info.phon...
}
fclose(fp);
return id;
}
}
//deleting the file
void delentry(register int id)
{
FILE* tmpfp;
register int sz=countentry(), i, j;
char in;
//make sure user wants to delete
puts("are you sure you want to delete?");
in=toupper(in=getchar());
fflush(stdin);
if(in=='N')
{
puts("Entry wasn't removed");
return;
}
puts("YES");
//creating a temp file
tmpfp=tmpfile();
//coping all entries from the phonebook file
//to the temp file except for the deleted one
for(i=1,j=1; i%26lt;=sz;i++)
{
readentry(i,0);
//passing the deleted entry and inserting a new id=j
if(i!=id)
{
info.id=j;
fwrite(%26amp;info, sizeof(struct strc), 1, tmpfp);
j++;
}
}
//seeking to the beginning of the file
rewind(tmpfp);
//deleting the old phonebook file
remove("list.phn");
//coping the entries from the temp file to the new phonebook file
for(id=1;id%26lt;j;id++)
{
fread(%26amp;info, sizeof(struct strc), 1, tmpfp);
putentry(id);
}
puts("Entry deleted successfully");
_rmtmp();
}
//searching the phonebook
void searchentry()
{
register char ch;
char key[25], *result, search_str[100];
register int i, id, sz=countentry();
//Which part user likes to search
puts("Do you like to search by NAME:(N),or ADDRESS:(A),or PHONE#(P)?");
do{
ch=toupper(ch=getchar());
fflush(stdin);
}while(ch!='N' %26amp;%26amp; ch!='A' %26amp;%26amp; ch!='P');
//getting the searvh string
puts("Enter your search:");
strtok(fgets(search_str, 100, stdin),"\n");
fflush(stdin);
//starting our search
for(id=1, i=1; id%26lt;=sz; id++)
{
//loading current entry inside our structure
readentry(id,0);
//what are we going to search
switch (ch)
{
case 'N': strcpy(key,info.name); break;
case 'A': strcpy(key,info.address); break;
case 'P': strcpy(key,info.phone); break;
default : ;
}
//checking the current string
result=strstr(strlwr(key), strlwr(search_str));
if(result)
{
//we found an Entry %26amp; printing it
if(i==1)
{
printf("The Entries containing \"%s\" are :\n", search_str);
puts("+---+----------------------+--...
printf("|ID | %-20s| %-25s| %-20s|\n", "NAME","ADDRESS","PHONE #");
//pause every 20 result
}
readentry(id,1);
if(i==20)
{
puts("+---+----------------------+-...
puts("Press any key to continue..");
getch();
}
i++;
}
}
if(--i)
{
//inserting footer for ending our search
puts("+---+----------------------+----...
printf("Found %d Result\n", i);
}else
//didn't find any
puts("No Results found");
}
Reply:Its better taking your teacher's help instead of checking all people's answers and deciding which one will suit you
BEST OF LUCK
Reply:If your teacher has office hours available where you can go over the project one-on-one, I highly suggest you take advantage of it.
Reply:U can do this using single linked list... its not very tough however takes time to write down the answer..
I have a program that takes,,,,name, address and roll number .... and this porgram can
1) add entries
2) delete entries
3) search entries
4) modify entries
5) traverse list
If u want i can post it here and u can make little bit of updations.. lemme know
/*Program for single linked list in c*/
#include%26lt;stdio.h%26gt;
#include%26lt;malloc.h%26gt;
struct list
{
char name[25];
char roll[4];
char addr[25];
struct list *next;
}*start;
struct list *search();
main()
{
char code[4],ch=' ';
struct list *ptr;
clrscr();
while(ch!='6')
{
printf("\n\n1. add items");
printf("\n2. modify items");
printf("\n3. search items");
printf("\n4. delete items");
printf("\n5. display items");
printf("\n6. exit");
printf("\n\nenter ur choice\n");
scanf("%c",%26amp;ch);
fflush(stdin);
if(ch=='1')
{
add();
}
if(ch=='2')
{
mod();
}
if(ch=='3')
{
printf("\nEnter the roll no to be searched for\n");
scanf("%s",%26amp;code);
fflush(stdin);
ptr=search(code);
if(ptr)
{
printf("\nThe roll no is %s",ptr-%26gt;roll);
printf("\nThe name is %s",ptr-%26gt;name);
printf("\nThe address is %s",ptr-%26gt;addr);
}
else
{
printf("\nThe roll no does not exists\n");
}
}
if(ch=='4')
{
del();
}
if(ch=='5')
{
display();
}
if(ch=='6')
{
exit();
}
else
{
continue;
}
}
}
struct list *getdata()
{
struct list *new;
new=(struct list *)malloc(sizeof(struct list));
printf("\nEnter the name\n");
scanf("%s",new-%26gt;name);
fflush(stdin);
printf("\nEnter the roll no\n");
scanf("%s",new-%26gt;roll);
fflush(stdin);
printf("\nEnter the address\n");
scanf("%s",new-%26gt;addr);
fflush(stdin);
return new;
}
add()
{
struct list *ptr,*prev,*new;
new=getdata();
if(start==NULL)
{
start=new;
new-%26gt;next=NULL;
return;
}
for(ptr=start;(ptr)%26amp;%26amp;strcmp(new-%26gt;roll...
if(strcmp(new-%26gt;roll,ptr-%26gt;roll)==0)
{
printf("\nThe roll no already exists\n");
return;
}
if(ptr!=start)
{
prev-%26gt;next=new;
new-%26gt;next=ptr;
}
else
{
new-%26gt;next=start;
start=new;
}
}
mod()
{
struct list *ptr;
char code[4];
printf("\nEnter the roll no\n");
scanf("%s",code);
ptr=search(code);
if(ptr==NULL)
{
printf("\nThe roll no does not exists\n");
return;
}
printf("\nEnter the address\n");
scanf("%s",ptr-%26gt;addr);
fflush(stdin);
}
struct list *search(char *s)
{
struct list *ptr;
for(ptr=start;(ptr)%26amp;%26amp;strcmp(s,ptr-%26gt;ro...
if(strcmp(s,ptr-%26gt;roll)==0)
{
return ptr;
}
else
{
return NULL;
}
}
del()
{
struct list *prev,*ptr;
char code[4];
printf("\nEnter the roll to be deleted\n");
scanf("%s",code);
fflush(stdin);
for(ptr=start;(ptr)%26amp;%26amp;strcmp(code,ptr-...
{
prev=ptr;
ptr=ptr-%26gt;next;
}
if(!ptr) /*valid if ptr==null*/
{
printf("\nThe roll no does not exists\n");
return;
}
if(ptr==start)
{
start=ptr-%26gt;next;
free(ptr);
return;
}
prev-%26gt;next=ptr-%26gt;next;
free(ptr);
return;
}
display()
{
struct list *ptr;
for(ptr=start;(ptr);ptr=ptr-%26gt;next)
{
printf("\n\nThe roll no is %s",ptr-%26gt;roll);
printf("\nThe name is %s",ptr-%26gt;name);
printf("\nThe address is %s",ptr-%26gt;addr);
}
}
Reply:You are going to have to use arrays, to store the information,
First read about Arrays!!!! You may also need to know something about pointers. I will help u wen i get time.
Reply:Does not look easy. May be you can contact a C programming expert at websites like http://oktutorial.com/
bouquet
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment