944,181 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9336
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 21st, 2006
0

C programming - need some help

Expand Post »
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.

  1. void Add()
  2. {
  3. char opt1;
  4. do
  5. {
  6. Contactbook = fopen("Contactbook.txt","a+");
  7. printf("Enter his/her Name:\n");
  8. scanf("%s", contactbook.name);
  9. printf("Enter his/her Birthday:\n");
  10. scanf("%s", contactbook.birthday);
  11. printf("Enter his/her Handphone No:\n");
  12. scanf("%s", contactbook.hp);
  13. printf("Enter his/her Address:\n");
  14. scanf("%s", contactbook.address);
  15. printf("Enter his/her Occupation\n");
  16. scanf("%s", contactbook.occupation);
  17. fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name,
  18. contactbook.birthday, contactbook.hp, contactbook.address,
  19. contactbook.occupation);
  20. fclose(Contactbook);
  21.  
  22. printf("To Continue using this program type Y/y");
  23. scanf("%c", &opt1);
  24. }
  25. }
  26.  
  27. void Del()
  28. {
  29. char opt2;
  30. char Tar [SIZE];
  31. int match = 0;
  32. temp = fopen("temp.txt", "w");
  33. if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  34. printf("----The file is empty----\n");
  35. else
  36. printf("Enter his/her name:");
  37. gets(Tar);
  38.  
  39. while (!feof(Contactbook))
  40. {
  41. fscanf(Contactbook,"%s %s %s %s %s",
  42. contactbook.name, contactbook.birthday, contactbook.hp,
  43. contactbook.address, contactbook.occupation);
  44. if(feof(Contactbook))
  45. break;
  46. if(strcmp(Tar, contactbook.name) !=0)
  47. fprintf(temp,"%s %s %s %s %s\n", contactbook.name,
  48. contactbook.birthday, contactbook.hp, contactbook.address,
  49. contactbook.occupation);
  50. else
  51. {
  52. match = 1;
  53. }
  54. if (!match)
  55. {
  56. printf("The name does not exist\n");
  57. printf("or it might had been deleted\n");
  58. }
  59.  
  60. fclose(Contactbook);
  61. fclose(temp);
  62. remove("Contactbook.txt");
  63. rename("temp.txt","Contacbook.txt");
  64.  
  65. }

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.
Similar Threads
Reputation Points: 19
Solved Threads: 0
Newbie Poster
amano is offline Offline
24 posts
since Aug 2006
Aug 21st, 2006
0

Re: C programming - need some help

You forgot to put [/CODE] at the end of code
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Aug 21st, 2006
0

Re: C programming - need some help

Quote originally posted by andor ...
You forgot to put [/code] at the end of code
Sorry 4 da mistake. Anyway, anybody can give me some ideas please.......
Reputation Points: 19
Solved Threads: 0
Newbie Poster
amano is offline Offline
24 posts
since Aug 2006
Aug 21st, 2006
0

Re: C programming - need some help

Quote originally posted by amano ...
Sorry 4 da mistake. Anyway, anybody can give me some ideas please.......
I'd say first brush up on user input.
  1. scanf("%s", contactbook.name);
  2. gets(Tar);
User Input: Strings and Numbers [C]
  1. while (!feof(Contactbook))
Avoid Loop Control Using eof()

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 22nd, 2006
0

Re: C programming - need some help

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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NAME 500
  6. #define BIRTHDAY 500
  7. #define HP 500
  8. #define ADDRESS 500
  9. #define OCCUPATION 500
  10. #define SIZE 500
  11.  
  12. void Add();
  13. void Del();
  14. void Search();
  15. void Edit();
  16. void List();
  17. void Exit();
  18. char info[SIZE];
  19.  
  20. struct contactbook
  21. {
  22. char name [NAME];
  23. char birthday [BIRTHDAY];
  24. char hp [HP];
  25. char address [ADDRESS];
  26. char occupation [OCCUPATION];
  27. char Tar [NAME];
  28. }
  29. contactbook;
  30.  
  31. FILE *Contactbook;
  32. FILE *temp;
  33.  
  34. int main()
  35. {
  36. int a;
  37. printf("Welcome to BRONTOK X Simple Contact Book\n");
  38. printf("Please Choose your option\n");
  39. printf("1: Adding The Contact\n");
  40. printf("2: Delete The Contact\n");
  41. printf("3: Search The Contact\n");
  42. printf("4: Edit The Contact\n");
  43. printf("5: List The Contact\n");
  44. printf("6: Exit this Program\n");
  45. scanf("%d", &a);
  46. switch(a)
  47. {
  48. case 1:
  49. {
  50. Add();
  51. break;
  52. }
  53. case 2:
  54. {
  55. Del();
  56. break;
  57. }
  58. case 3:
  59. {
  60. Search();
  61. break;
  62. }
  63. case 4:
  64. {
  65. Edit();
  66. break;
  67. }
  68. case 5:
  69. {
  70. List();
  71. break;
  72. }
  73. case 6:
  74. {
  75. Exit();
  76. break;
  77. }
  78. default:
  79. {
  80. printf("Invalid Input");
  81. }
  82. }
  83. }
  84. void Add()
  85. {
  86. char opt1;
  87. do
  88. {
  89. Contactbook = fopen("Contactbook.txt","a+");
  90. printf("Enter his/her Name:\n");
  91. scanf("%s", contactbook.name);
  92. printf("Enter his/her Birthday:\n");
  93. scanf("%s", contactbook.birthday);
  94. printf("Enter his/her Handphone No:\n");
  95. scanf("%s", contactbook.hp);
  96. printf("Enter his/her Address:\n");
  97. scanf("%s", contactbook.address);
  98. printf("Enter his/her Occupation\n");
  99. scanf("%s", contactbook.occupation);
  100. fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name,
  101. contactbook.birthday, contactbook.hp, contactbook.address,
  102. contactbook.occupation);
  103. fclose(Contactbook);
  104.  
  105. printf("To Continue using this program type Y/y");
  106. scanf("%c", &opt1);
  107. }
  108. if(opt1 =='y' || opt1 == 'Y')
  109. main();
  110. }
  111.  
  112. void Del()
  113. {
  114. char opt2;
  115. char Tar [SIZE];
  116. int match = 0;
  117. temp = fopen("temp.txt", "w");
  118. if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  119. printf("----The file is empty----\n");
  120. else
  121. printf("Enter his/her name:");
  122. gets(Tar);
  123.  
  124. while (!feof(Contactbook))
  125. {
  126. fscanf(Contactbook,"%s %s %s %s %s",
  127. contactbook.name, contactbook.birthday, contactbook.hp,
  128. contactbook.address, contactbook.occupation);
  129. if(feof(Contactbook))
  130. break;
  131. if(strcmp(Tar, contactbook.name) !=0)
  132. fprintf(temp,"%s %s %s %s %s\n", contactbook.name,
  133. contactbook.birthday, contactbook.hp, contactbook.address,
  134. contactbook.occupation);
  135. else
  136. {
  137. match = 1;
  138. }
  139. if (!match)
  140. {
  141. printf("The name does not exist\n");
  142. printf("or it might had been deleted\n");
  143. }
  144.  
  145. fclose(Contactbook);
  146. fclose(temp);
  147. remove("Contactbook.txt");
  148. rename("temp.txt","Contacbook.txt");
  149.  
  150. do
  151. {
  152. printf("To Continue using this program type Y/y");
  153. scanf("%c", &opt2);
  154. }
  155. while (opt2 =='y' || opt2 == 'Y');
  156. main();
  157. }
  158. }
  159. void Search()
  160. {
  161. char opt3;
  162. char Tar [SIZE];
  163. int match = 0;
  164. if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  165. printf("----The File is Empty----\n");
  166. else
  167. {
  168. printf("Enter his/her name:\n");
  169. scanf("%s", Tar);
  170. while(!feof(Contactbook) && match ==0)
  171. {
  172. fscanf(Contactbook,"%s %s %s %s
  173. %s",contactbook.name, contactbook.birthday, contactbook.hp,
  174. contactbook.address, contactbook.occupation);
  175. if(strcmp(Tar, contactbook.name) == 0)
  176. match = 1;
  177. }
  178. if(match)
  179. {
  180. printf("His/Her Name is %s\n",contactbook.name);
  181. printf("His/Her Birthday is s\n",
  182. contactbook.birthday);
  183. printf("His/Her Handphone No is %s\n",
  184. contactbook.hp);
  185. printf("His/Her Address is %s\n",
  186. contactbook.address);
  187. printf("His/Her Occupation is %s\n",
  188. contactbook.occupation);
  189. main();
  190. }
  191. else if (!match)
  192. printf("----There is no such entry----");
  193. fclose(Contactbook);
  194. }
  195. do
  196. {
  197. printf("To Continue using this program type Y/y");
  198. scanf("%c", &opt3);
  199. }
  200. while (opt3 =='y' || opt3 == 'Y');
  201. main();
  202. }
  203. void Edit()
  204. {
  205.  
  206. void List()
  207. {
  208. int opt5;
  209. Contactbook = fopen("Contactbook.txt","a+");
  210. do
  211. {
  212. fgets(info,SIZE,Contactbook);
  213. printf("%s\n", info);
  214. }
  215. while(!feof(Contactbook));
  216. fclose(Contactbook);
  217. do
  218. {
  219. printf("Enter y or Y to run the program again\n");
  220. scanf("%c", &opt5);
  221. }
  222. while (opt5 == 'y' ||opt5 == 'Y');
  223. main();
  224. }
  225.  
  226. void Exit()
  227. {
  228. printf("Thank You For Using my Contactbook. Have a Nice
  229. Day");
  230. }
Last edited by Dave Sinkula; Aug 22nd, 2006 at 9:10 pm. Reason: Fixed [code][/code] tags.
Reputation Points: 19
Solved Threads: 0
Newbie Poster
amano is offline Offline
24 posts
since Aug 2006
Aug 22nd, 2006
0

Re: C programming - need some help

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 22nd, 2006
0

Re: C programming - need some help

Check your Edit function. Where it ends?
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Aug 22nd, 2006
0

Re: C programming - need some help

Click to Expand / Collapse  Quote originally posted by andor ...
Check your Edit function. Where it ends?
The Edit function is now become my major problem because i have no idea on how to make it.
Reputation Points: 19
Solved Threads: 0
Newbie Poster
amano is offline Offline
24 posts
since Aug 2006
Aug 22nd, 2006
0

Re: C programming - need some help

Click to Expand / Collapse  Quote originally posted by Lerner ...
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:
OIC. I will change it to loop. But for the above statement could u please explain more. With examples if u don't mind
Reputation Points: 19
Solved Threads: 0
Newbie Poster
amano is offline Offline
24 posts
since Aug 2006
Aug 22nd, 2006
1

Re: C programming - need some help

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 500
  5.  
  6. void Add();
  7. void Del();
  8. //void Search();
  9. //void Edit();
  10. //void List();
  11. //void Exit();
  12. char info[SIZE];
  13.  
  14. typedef struct contactboo
  15. {
  16. char name [SIZE];
  17. char birthday [SIZE];
  18. }contactbook;
  19.  
  20. FILE *Contactbook;
  21. FILE *temp;
  22.  
  23. int main()
  24. {
  25. int a;
  26. char flag = 'y';
  27. while(flag != 'n')
  28. {
  29. printf("Welcome to BRONTOK X Simple Contact Book\n");
  30. printf("Please Choose your option\n");
  31. printf("1: Adding The Contact\n");
  32. printf("2: Delete The Contact\n");
  33. printf("6: Exit this Program\n");
  34. scanf("%d", &a);
  35. switch(a)
  36. {
  37. case 1:
  38. Add();
  39. break;
  40. case 2:
  41. Del();
  42. break;
  43. /*
  44.   case 3:
  45.   {
  46.   Search();
  47.   break;
  48.   }
  49.   case 4:
  50.   {
  51.   Edit();
  52.   break;
  53.   }
  54.   case 5:
  55.   {
  56.   List();
  57.   break;
  58.   }
  59. */
  60. case 6:
  61. flag = 'n';
  62. break;
  63. default:
  64. printf("Invalid Input");
  65. }
  66.  
  67. printf("Good bye!");
  68. getch();
  69. return 0;
  70. }
  71.  
  72. void Add()
  73. {
  74. char opt1;
  75. do
  76. {
  77. Contactbook = fopen("Contactbook.txt","a+");
  78. printf("Enter his/her Name:\n");
  79. scanf("%s", contactbook.name);
  80. printf("Enter his/her Birthday:\n");
  81. scanf("%s", contactbook.birthday);
  82. fprintf(Contactbook, "%s %s\n", contactbook.name,
  83. contactbook.birthday);
  84. fclose(Contactbook);
  85.  
  86. printf("To Continue using this program type Y/y");
  87. scanf("%c", &opt1);
  88. }while(opt1 =='y' || opt1 == 'Y')
  89. }
  90.  
  91. void Del()
  92. {
  93. printf("in Del()");
  94. getch();
  95. /*
  96.  char opt2;
  97.  char Tar [SIZE];
  98.  int match = 0;
  99.  temp = fopen("temp.txt", "w");
  100.  if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  101.   printf("----The file is empty----\n");
  102.  else
  103.   printf("Enter his/her name:");
  104.   gets(Tar);
  105.  
  106.  while (!feof(Contactbook))
  107.  {
  108.   fscanf(Contactbook,"%s %s %s %s %s",
  109. contactbook.name, contactbook.birthday);
  110.  if(feof(Contactbook))
  111.  break;
  112.  if(strcmp(Tar, contactbook.name) !=0)
  113.   fprintf(temp,"%s %s %s %s %s\n", contactbook.name,
  114. contactbook.birthday, contactbook.hp, contactbook.address,
  115. contactbook.occupation);
  116.  else
  117.  {
  118.   match = 1;
  119.  }
  120.  if (!match)
  121.  {
  122.   printf("The name does not exist\n");
  123.   printf("or it might had been deleted\n");
  124.  }
  125.  
  126.  fclose(Contactbook);
  127.  fclose(temp);
  128.  remove("Contactbook.txt");
  129.  rename("temp.txt","Contacbook.txt");
  130.  
  131.  do
  132.  {
  133.   printf("To Continue using this program type Y/y");
  134.   scanf("%c", &opt2);
  135.  }
  136.  while (opt2 =='y' || opt2 == 'Y');
  137.  main();
  138.  }
  139. */
  140. }
  141.  
  142. /*
  143. void Search()
  144. {
  145.  char opt3;
  146.  char Tar [SIZE];
  147.  int match = 0;
  148.  if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
  149.   printf("----The File is Empty----\n");
  150.  else
  151.  {
  152.   printf("Enter his/her name:\n");
  153.   scanf("%s", Tar);
  154.   while(!feof(Contactbook) && match ==0)
  155.   {
  156.   fscanf(Contactbook,"%s %s %s %s
  157. %s",contactbook.name, contactbook.birthday, contactbook.hp,
  158. contactbook.address, contactbook.occupation);
  159.   if(strcmp(Tar, contactbook.name) == 0)
  160.   match = 1;
  161.   }
  162.   if(match)
  163.   {
  164.   printf("His/Her Name is %s\n",contactbook.name);
  165.   printf("His/Her Birthday is s\n",
  166. contactbook.birthday);
  167.   printf("His/Her Handphone No is %s\n",
  168. contactbook.hp);
  169.   printf("His/Her Address is %s\n",
  170. contactbook.address);
  171.   printf("His/Her Occupation is %s\n",
  172. contactbook.occupation);
  173.   main();
  174.   }
  175.   else if (!match)
  176.   printf("----There is no such entry----");
  177.   fclose(Contactbook);
  178.   }
  179.   do
  180.  {
  181.   printf("To Continue using this program type Y/y");
  182.   scanf("%c", &opt3);
  183.  }
  184.  while (opt3 =='y' || opt3 == 'Y');
  185.  main();
  186. }
  187. void Edit()
  188. {
  189.  
  190. void List()
  191. {
  192.  int opt5;
  193.  Contactbook = fopen("Contactbook.txt","a+");
  194.  do
  195.  {
  196.   fgets(info,SIZE,Contactbook);
  197.   printf("%s\n", info);
  198.  }
  199.  while(!feof(Contactbook));
  200.  fclose(Contactbook);
  201.  do
  202.  {
  203.   printf("Enter y or Y to run the program again\n");
  204.   scanf("%c", &opt5);
  205.  }
  206.  while (opt5 == 'y' ||opt5 == 'Y');
  207.  main();
  208. }
  209.  
  210. void Exit()
  211. {
  212.  printf("Thank You For Using my Contactbook. Have a Nice
  213. Day");
  214. }
  215. */
Last edited by Lerner; Aug 22nd, 2006 at 12:38 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: realy fresh to C
Next Thread in C Forum Timeline: Connecting database using C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC