| | |
C programming - need some help
![]() |
•
•
Join Date: Aug 2006
Posts: 24
Reputation:
Solved Threads: 0
Hello I'm newbie in C programming. Iwant to make a simple contact book that have add, delete, edit, and display function. 2 out of 4 i've done which is add and delete. So I need some help from all of you in edit and list function. Here is my function for add and delete. I refer this from acidburn code and changed it a little bit and i don't want to use ctype.h because not familiar with it yet.
So i need some help on edit and list function. At 1st i think i want to use the same code as delete but change remove to rename but it can't be done that's way. So your helps are highly appreciated. Ok. Thanks
C Syntax (Toggle Plain Text)
void Add() { char opt1; do { Contactbook = fopen("Contactbook.txt","a+"); printf("Enter his/her Name:\n"); scanf("%s", contactbook.name); printf("Enter his/her Birthday:\n"); scanf("%s", contactbook.birthday); printf("Enter his/her Handphone No:\n"); scanf("%s", contactbook.hp); printf("Enter his/her Address:\n"); scanf("%s", contactbook.address); printf("Enter his/her Occupation\n"); scanf("%s", contactbook.occupation); fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); fclose(Contactbook); printf("To Continue using this program type Y/y"); scanf("%c", &opt1); } } void Del() { char opt2; char Tar [SIZE]; int match = 0; temp = fopen("temp.txt", "w"); if((Contactbook = fopen("Contactbook.txt","r")) == NULL) printf("----The file is empty----\n"); else printf("Enter his/her name:"); gets(Tar); while (!feof(Contactbook)) { fscanf(Contactbook,"%s %s %s %s %s", contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); if(feof(Contactbook)) break; if(strcmp(Tar, contactbook.name) !=0) fprintf(temp,"%s %s %s %s %s\n", contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); else { match = 1; } if (!match) { printf("The name does not exist\n"); printf("or it might had been deleted\n"); } fclose(Contactbook); fclose(temp); remove("Contactbook.txt"); rename("temp.txt","Contacbook.txt"); }
So i need some help on edit and list function. At 1st i think i want to use the same code as delete but change remove to rename but it can't be done that's way. So your helps are highly appreciated. Ok. Thanks
Last edited by Dave Sinkula; Aug 21st, 2006 at 8:24 pm. Reason: Fixed [code][/code] tags.
•
•
•
•
Originally Posted by amano
Sorry 4 da mistake. Anyway, anybody can give me some ideas please.......
C Syntax (Toggle Plain Text)
scanf("%s", contactbook.name); gets(Tar);
C Syntax (Toggle Plain Text)
while (!feof(Contactbook))
And it might be helpful to post a program that attempts to use/test these functions rather than leaving that up to others. Presumeably you're writing test code that uses them? Share your "secret".
Use good formatting. Use correct spelling, punctuation, and grammar. Use code tags correctly. In short, make your post as easy to answer as you can -- or else get used to longer waits.
Last edited by Dave Sinkula; Aug 21st, 2006 at 9:53 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Aug 2006
Posts: 24
Reputation:
Solved Threads: 0
Thanks Dave for your advice. Sorry 4 not posting my whole code. Here's my full code. When run it, it didn't seems to run well.
I will seriously have a look your points and try to fix it. Anyway I'm still newbie, there's a lot of things to be learn. Your guidance will highly be appreciated. Actually this thing is my homework my teacher gave it to me and my friend, kind of group work. But I stucked at edit function. Thank You.
I will seriously have a look your points and try to fix it. Anyway I'm still newbie, there's a lot of things to be learn. Your guidance will highly be appreciated. Actually this thing is my homework my teacher gave it to me and my friend, kind of group work. But I stucked at edit function. Thank You.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NAME 500 #define BIRTHDAY 500 #define HP 500 #define ADDRESS 500 #define OCCUPATION 500 #define SIZE 500 void Add(); void Del(); void Search(); void Edit(); void List(); void Exit(); char info[SIZE]; struct contactbook { char name [NAME]; char birthday [BIRTHDAY]; char hp [HP]; char address [ADDRESS]; char occupation [OCCUPATION]; char Tar [NAME]; } contactbook; FILE *Contactbook; FILE *temp; int main() { int a; printf("Welcome to BRONTOK X Simple Contact Book\n"); printf("Please Choose your option\n"); printf("1: Adding The Contact\n"); printf("2: Delete The Contact\n"); printf("3: Search The Contact\n"); printf("4: Edit The Contact\n"); printf("5: List The Contact\n"); printf("6: Exit this Program\n"); scanf("%d", &a); switch(a) { case 1: { Add(); break; } case 2: { Del(); break; } case 3: { Search(); break; } case 4: { Edit(); break; } case 5: { List(); break; } case 6: { Exit(); break; } default: { printf("Invalid Input"); } } } void Add() { char opt1; do { Contactbook = fopen("Contactbook.txt","a+"); printf("Enter his/her Name:\n"); scanf("%s", contactbook.name); printf("Enter his/her Birthday:\n"); scanf("%s", contactbook.birthday); printf("Enter his/her Handphone No:\n"); scanf("%s", contactbook.hp); printf("Enter his/her Address:\n"); scanf("%s", contactbook.address); printf("Enter his/her Occupation\n"); scanf("%s", contactbook.occupation); fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); fclose(Contactbook); printf("To Continue using this program type Y/y"); scanf("%c", &opt1); } if(opt1 =='y' || opt1 == 'Y') main(); } void Del() { char opt2; char Tar [SIZE]; int match = 0; temp = fopen("temp.txt", "w"); if((Contactbook = fopen("Contactbook.txt","r")) == NULL) printf("----The file is empty----\n"); else printf("Enter his/her name:"); gets(Tar); while (!feof(Contactbook)) { fscanf(Contactbook,"%s %s %s %s %s", contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); if(feof(Contactbook)) break; if(strcmp(Tar, contactbook.name) !=0) fprintf(temp,"%s %s %s %s %s\n", contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); else { match = 1; } if (!match) { printf("The name does not exist\n"); printf("or it might had been deleted\n"); } fclose(Contactbook); fclose(temp); remove("Contactbook.txt"); rename("temp.txt","Contacbook.txt"); do { printf("To Continue using this program type Y/y"); scanf("%c", &opt2); } while (opt2 =='y' || opt2 == 'Y'); main(); } } void Search() { char opt3; char Tar [SIZE]; int match = 0; if((Contactbook = fopen("Contactbook.txt","r")) == NULL) printf("----The File is Empty----\n"); else { printf("Enter his/her name:\n"); scanf("%s", Tar); while(!feof(Contactbook) && match ==0) { fscanf(Contactbook,"%s %s %s %s %s",contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); if(strcmp(Tar, contactbook.name) == 0) match = 1; } if(match) { printf("His/Her Name is %s\n",contactbook.name); printf("His/Her Birthday is s\n", contactbook.birthday); printf("His/Her Handphone No is %s\n", contactbook.hp); printf("His/Her Address is %s\n", contactbook.address); printf("His/Her Occupation is %s\n", contactbook.occupation); main(); } else if (!match) printf("----There is no such entry----"); fclose(Contactbook); } do { printf("To Continue using this program type Y/y"); scanf("%c", &opt3); } while (opt3 =='y' || opt3 == 'Y'); main(); } void Edit() { void List() { int opt5; Contactbook = fopen("Contactbook.txt","a+"); do { fgets(info,SIZE,Contactbook); printf("%s\n", info); } while(!feof(Contactbook)); fclose(Contactbook); do { printf("Enter y or Y to run the program again\n"); scanf("%c", &opt5); } while (opt5 == 'y' ||opt5 == 'Y'); main(); } void Exit() { printf("Thank You For Using my Contactbook. Have a Nice Day"); }
Last edited by Dave Sinkula; Aug 22nd, 2006 at 9:10 pm. Reason: Fixed [code][/code] tags.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 264
I would encourage you to do just one function at a time.
Since your first function is Add(), I would comment all the other functions bodies and just have a statement like printf("in Del()"); etc. until you get there. Within Add() I would note:
1) each keyword do needs a keyword while.
2) Never call main() from within your program. Use loops to accomplish what you want. In this case you should use a loop in main() to cycle throught the menu and a loop in Add() to allow sequential Add()s without going through the menu, if that is your intent.
Since your first function is Add(), I would comment all the other functions bodies and just have a statement like printf("in Del()"); etc. until you get there. Within Add() I would note:
1) each keyword do needs a keyword while.
2) Never call main() from within your program. Use loops to accomplish what you want. In this case you should use a loop in main() to cycle throught the menu and a loop in Add() to allow sequential Add()s without going through the menu, if that is your intent.
•
•
Join Date: Aug 2006
Posts: 24
Reputation:
Solved Threads: 0
OIC. I will change it to loop. But for the above statement could u please explain more. With examples if u don't mind
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 264
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 500 void Add(); void Del(); //void Search(); //void Edit(); //void List(); //void Exit(); char info[SIZE]; typedef struct contactboo { char name [SIZE]; char birthday [SIZE]; }contactbook; FILE *Contactbook; FILE *temp; int main() { int a; char flag = 'y'; while(flag != 'n') { printf("Welcome to BRONTOK X Simple Contact Book\n"); printf("Please Choose your option\n"); printf("1: Adding The Contact\n"); printf("2: Delete The Contact\n"); printf("6: Exit this Program\n"); scanf("%d", &a); switch(a) { case 1: Add(); break; case 2: Del(); break; /* case 3: { Search(); break; } case 4: { Edit(); break; } case 5: { List(); break; } */ case 6: flag = 'n'; break; default: printf("Invalid Input"); } printf("Good bye!"); getch(); return 0; } void Add() { char opt1; do { Contactbook = fopen("Contactbook.txt","a+"); printf("Enter his/her Name:\n"); scanf("%s", contactbook.name); printf("Enter his/her Birthday:\n"); scanf("%s", contactbook.birthday); fprintf(Contactbook, "%s %s\n", contactbook.name, contactbook.birthday); fclose(Contactbook); printf("To Continue using this program type Y/y"); scanf("%c", &opt1); }while(opt1 =='y' || opt1 == 'Y') } void Del() { printf("in Del()"); getch(); /* char opt2; char Tar [SIZE]; int match = 0; temp = fopen("temp.txt", "w"); if((Contactbook = fopen("Contactbook.txt","r")) == NULL) printf("----The file is empty----\n"); else printf("Enter his/her name:"); gets(Tar); while (!feof(Contactbook)) { fscanf(Contactbook,"%s %s %s %s %s", contactbook.name, contactbook.birthday); if(feof(Contactbook)) break; if(strcmp(Tar, contactbook.name) !=0) fprintf(temp,"%s %s %s %s %s\n", contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); else { match = 1; } if (!match) { printf("The name does not exist\n"); printf("or it might had been deleted\n"); } fclose(Contactbook); fclose(temp); remove("Contactbook.txt"); rename("temp.txt","Contacbook.txt"); do { printf("To Continue using this program type Y/y"); scanf("%c", &opt2); } while (opt2 =='y' || opt2 == 'Y'); main(); } */ } /* void Search() { char opt3; char Tar [SIZE]; int match = 0; if((Contactbook = fopen("Contactbook.txt","r")) == NULL) printf("----The File is Empty----\n"); else { printf("Enter his/her name:\n"); scanf("%s", Tar); while(!feof(Contactbook) && match ==0) { fscanf(Contactbook,"%s %s %s %s %s",contactbook.name, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation); if(strcmp(Tar, contactbook.name) == 0) match = 1; } if(match) { printf("His/Her Name is %s\n",contactbook.name); printf("His/Her Birthday is s\n", contactbook.birthday); printf("His/Her Handphone No is %s\n", contactbook.hp); printf("His/Her Address is %s\n", contactbook.address); printf("His/Her Occupation is %s\n", contactbook.occupation); main(); } else if (!match) printf("----There is no such entry----"); fclose(Contactbook); } do { printf("To Continue using this program type Y/y"); scanf("%c", &opt3); } while (opt3 =='y' || opt3 == 'Y'); main(); } void Edit() { void List() { int opt5; Contactbook = fopen("Contactbook.txt","a+"); do { fgets(info,SIZE,Contactbook); printf("%s\n", info); } while(!feof(Contactbook)); fclose(Contactbook); do { printf("Enter y or Y to run the program again\n"); scanf("%c", &opt5); } while (opt5 == 'y' ||opt5 == 'Y'); main(); } void Exit() { printf("Thank You For Using my Contactbook. Have a Nice Day"); } */
Last edited by Lerner; Aug 22nd, 2006 at 12:38 pm.
![]() |
Similar Threads
- Good books on Shell Programming/Perl (Perl)
- Shell Programming (Shell Scripting)
- Programming (C++)
- Where to get started with Web Programming (IT Professionals' Lounge)
- Programming in Linux? (*nix Software)
- Help Me on Symbian(OS) Programming with c++ (C++)
- Efficient Programming (Computer Science)
- New To Programming = ME!! (C++)
Other Threads in the C Forum
- Previous Thread: realy fresh to C
- Next Thread: Connecting database using C
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h





