Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. /*Divine's command line phonebook application */
  2. /*written in the C programming language w/vs */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. char name[25];
  8. int number[25];
  9. lookup();
  10. add();
  11. add2();
  12. dump_line();
  13. noname();
  14.  
  15. main()
  16. {
  17. int x=0;
  18.  
  19.  
  20.  
  21. printf(" *********************PHONE BOOK********************\n\n");
  22. printf("1. Lookup\n2. Add new entry\n3. Exit\n\n");
  23.  
  24. scanf("%d",&x);
  25.  
  26. while (x != 1 && x != 2 && x != 3){
  27. dump_line(stdin);
  28. scanf("%d",&x);
  29. }
  30.  
  31. if (x == 1 )
  32. {
  33. system("cls");
  34. lookup();
  35. }
  36.  
  37. else if (x ==2)
  38. {
  39. system("cls");
  40. add();
  41. }
  42. else{
  43. printf("Exiting phonebook\n");
  44. return 0;
  45. }
  46.  
  47. getch();
  48.  
  49. } /********END OF MAIN FUNCTION***********/
  50.  
  51. lookup()
  52. {
  53. int x,y=5;
  54. FILE *pt;
  55. char findname[25];
  56. char words[25];
  57.  
  58.  
  59.  
  60. pt = fopen ("phonebook.txt","r");
  61. if (pt == NULL)
  62. {
  63. printf("File not found!\n");
  64. return 0;
  65. }
  66.  
  67. printf(" *****************PHONE BOOK LOOKUP*****************\n\n\n");
  68. printf("Type \"all\" to list all entries\n\n");
  69. printf("Enter a name: \n");
  70.  
  71. scanf("%25s",name);
  72.  
  73. if ( stricmp(name,"all") != 0 ){
  74. do{
  75. if (y == EOF){
  76. printf("Name doesn't exist\n\n");
  77. getch();
  78. noname();
  79. }
  80. y = fscanf(pt,"%s",findname);
  81.  
  82. }while (stricmp(name,findname) != 0);
  83.  
  84. fgets (number,25,pt);
  85. printf("\n\nName: %s\nNumber:%s",findname,number);
  86. }
  87.  
  88. else if ( stricmp (name,"all") == 0 )
  89. {
  90. x = 1;
  91. printf("\n");
  92. do {
  93. x = fscanf(pt,"%s",words);
  94. if (x == EOF)
  95. break;
  96. printf("Name: %s\n",words);
  97. x = fscanf(pt,"%s",words);
  98. printf("Number: %s\n",words);
  99. printf("\n---------------------------\n");
  100. } while (x != EOF);
  101.  
  102.  
  103.  
  104. }
  105. getch();
  106.  
  107. printf("\n\nWould you like to look up another name? (y//n)\n\n");
  108.  
  109. do {
  110. x = getch();
  111. } while (x != 'y' && x != 'n');
  112.  
  113. if (x == 'y'){
  114. system("cls");
  115. lookup();
  116. }
  117.  
  118. else if (x == 'n'){
  119. system("cls");
  120. main();
  121. }
  122.  
  123.  
  124.  
  125.  
  126. fclose (pt);
  127.  
  128.  
  129.  
  130. } /*******END OF LOOKUP FUNCTION************/
  131.  
  132. add()
  133. {
  134. FILE *pt;
  135. printf(" *****************ADD ENTRY*****************\n\n\n");
  136. printf("Add a name and a number now:\n\n");
  137. pt = fopen ("phonebook.txt","a");
  138. if (pt == NULL)
  139. {
  140. printf("NO PHONEBOOK FOUND!\n");
  141. return 0;
  142. }
  143.  
  144.  
  145.  
  146.  
  147. scanf("%s %s",name,number);
  148. fprintf (pt,"%s %s\n",name,number);
  149.  
  150. printf("\nNumber added successfully!\n\n");
  151. getch();
  152. add2();
  153.  
  154. } /*******END OF ADD FUNCTION********/
  155.  
  156.  
  157. add2()
  158. {
  159. int option;
  160.  
  161.  
  162. printf("Would you like to add another number? (y//n)\n");
  163. do {
  164. option = getch();
  165. } while (option != 'y' && option != 'n');
  166.  
  167. if (option == 'y'){
  168. system ("cls");
  169. add();
  170. }
  171.  
  172. else {
  173. printf("\nReturning to main menu\n\n");
  174. system ("cls");
  175. main();
  176. }
  177.  
  178. } /***end of add2 function***/
  179.  
  180. noname()
  181. {
  182. int option;
  183. printf("Would you like to look up another name? (y//n)\n\n");
  184.  
  185. do{
  186. option = getch();
  187. } while (option != 'y' && option != 'n');
  188.  
  189. if (option == 'y'){
  190. system("cls");
  191. add();
  192. }
  193.  
  194.  
  195. else {
  196. printf("Returning to main menu\n\n");
  197. system ("cls");
  198. main();
  199. }
  200. } /***end of noname function****/
  201.  
  202.  
  203. dump_line(FILE *fp)
  204. {
  205. int ch;
  206.  
  207. while( (ch = fgetc(fp)) != EOF && ch != '\n' )
  208. /* null body */;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement