User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 384,015 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,238 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Apr 3rd, 2008
Views: 1,918
Hi all, here is a small program I have worked on over these last two days, I have submitted it in the hopes of some constructive criticism and suggestions (please be kind, this is one of my earlier C projects lol). This is a small console application that simulates a contact management database. The data that a user inputs is saved to an external dat file (contactdatabase.dat) which is created the first time a user saves and exits. This project also employs the use of Malloc(), which is used to allocate memory on the fly (at runtime) for dynamic data structures, in this case a single linked list. Upon entering a new contact, a unique ID account number is allocated to that contact. Note that at present, if this contact is removed, that ID account is NOT freed up for use by an additional (new) contact. I hope you all find this interesting and useful, I think the comments explain pretty well what is going on in the program, so those who are new to C will appreciate this most:
P.S. Sorry if the formatting does not look that tidy, I have tried to EDIT it so now It looks better.
Last edited : Apr 3rd, 2008.
c Syntax
  1. /*Contact Management Database using a dynamic - single linked list
  2. Author : P J Wells
  3. Compiled on Dev-C++, Bloodshed Software.
  4. Date : 03/04/08
  5. */
  6.  
  7. /*----------------------------------------------------------------------------*/
  8. /* Define libraries to be included */
  9. #include <stdio.h>
  10. #include <malloc.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. /*----------------------------------------------------------------------------*/
  14. /* Define Functions*/
  15. void clearInput(void);
  16. void addNewcontact(void);
  17. void listAll(void);
  18. void deletecontact(void);
  19. void modifycontact(void);
  20. int findcontact(void);
  21. int prompt(void);
  22. int findnum (int);
  23.  
  24. /*----------------------------------------------------------------------------*/
  25. /* Define Structures*/
  26.  
  27. typedef struct contact {
  28. int number; /*unique account number*/
  29. char name[20]; /*contains name*/
  30. char phone[15]; /*contains phone number*/
  31. char email[20]; /*contains email address*/
  32. struct contact *next; /*next is used to navigate through structures.*/
  33. int count; /*count is used to input comments into array*/
  34. } Contact;
  35. Contact *firstc,*currentc,*newc; /*pointers*/
  36. /* firstc is used to point to first record in list
  37. currentc points to current record in list
  38. newc contains address of new structure/node
  39. */
  40. int cnum = 0; /*gives unique account numbers*/
  41.  
  42.  
  43. /*----------------------------------------------------------------------------*/
  44. /* Main Function */
  45.  
  46. int main()
  47. {
  48. FILE *datafile;
  49. char *filename = "contactdatabase.dat";/*declare file name*/
  50. char ch;
  51. firstc = NULL;
  52.  
  53. datafile = fopen(filename,"r");/* open file for reading*/
  54.  
  55. if(datafile)
  56. {
  57. firstc = (struct contact *)malloc(sizeof(struct contact));
  58. /*use of malloc to set aside memory relative to size of structure contact*/
  59. currentc = firstc; /*make first record current*/
  60. while(1) /*endless while loop. a NULL pointer in final node ends loop*/
  61. {
  62. newc = (struct contact *)malloc(sizeof(struct contact));
  63. fread(currentc,sizeof(struct contact),1,datafile);
  64. if(currentc->next == NULL) /* NULL indicates end of node list*/
  65. break;
  66. currentc->next = newc; /* pointer referencing next node*/
  67. currentc->count=0; /* initiates count for comments*/
  68. currentc = newc; /* make current record new*/
  69. }
  70. fclose(datafile); /* close file - good practice to cloe files after use*/
  71. cnum = currentc->number;
  72.  
  73. }
  74.  
  75. do
  76. {
  77. fflush(stdin);
  78. puts("\nWelcome To The Contact Database");/* print menu messages*/
  79. puts("-- -----------------------------");
  80. puts("1 - Add a new contact");
  81. puts("2 - Delete contact");
  82. puts("3 - List all contacts");
  83. puts("4 - Modify contact");
  84. puts("5 - Find a contact by name");
  85. puts("-- -----------------------------");
  86. puts("Q - Save and quit\n");
  87. printf("\tYour choice:");
  88. ch = getchar();
  89. ch = toupper(ch);/*changes user input case to upper case*/
  90. switch(ch) /*stores in ch variable.*/
  91. {
  92. case '1':
  93. puts("Add a new contact\n");
  94. fflush(stdin);
  95. addNewcontact();//call addNewcontact function
  96. break;
  97. case '2':
  98. puts("Delete a contact\n");
  99. deletecontact();
  100. break;
  101. case '3':
  102. puts("List all contacts\n");
  103. listAll();
  104. break;
  105. case '4':
  106. puts("Modify a contact\n");
  107. modifycontact();
  108. break;
  109. case '5':
  110. puts("Find a contact by name\n");
  111. findcontact();
  112. break;
  113. case 'Q':
  114. puts("Save and quit\n");
  115. default:
  116. break;
  117. }
  118. }
  119. while(ch != 'Q');
  120.  
  121. /*
  122.  * Save the records to disk
  123.  */
  124. currentc = firstc;
  125.  
  126. if(currentc == NULL)
  127. return(0); /*no data to write*/
  128.  
  129. datafile = fopen(filename,"w"); /*open file to write*/
  130.  
  131. if(datafile == NULL)
  132. {
  133. printf("Error writing to %s\n",filename);
  134. return(1);
  135. }
  136. /* Write each record to disk*/
  137. while(currentc != NULL)
  138. {
  139. fwrite(currentc,sizeof(struct contact),1,datafile);
  140. currentc = currentc->next;
  141. }
  142. fclose(datafile); /*closes data file*/
  143. return(0);
  144. }
  145. /*----------------------------------------------------------------------------*/
  146. void addNewcontact(void) /* add new contact function*/
  147. {
  148. newc = (struct contact *)malloc(sizeof(struct contact));
  149. /*allocates memory for new structure.*/
  150.  
  151. /*
  152.  * Checks to see whether this is the first record in file
  153.  * If so, then all pointers are initialized to this record,
  154.  */
  155.  
  156. if(firstc==NULL)
  157. firstc = currentc = newc;
  158.  
  159. /*
  160.  * if not, end of structure list is obtained
  161.  */
  162.  
  163. else
  164. {
  165. currentc = firstc; /* make the first record the current one*/
  166.  
  167.  
  168. while(currentc->next != NULL)currentc = currentc->next;
  169. /* and loop through all records*/
  170. currentc->next = newc; /* pointer to next node */
  171. currentc = newc; /* make current record the new one*/
  172. }
  173.  
  174. /* update the structure */
  175.  
  176. cnum++;
  177. printf("%27s: %5i\n","contact number",cnum);
  178. currentc->number = cnum; /*cnum is used to give unique account numbers*/
  179.  
  180. printf("%27s: ","Enter contact name");
  181. gets(currentc->name);
  182.  
  183. printf("%27s: ","Enter contact Phone number");
  184. gets(currentc->phone);
  185.  
  186. printf("%27s: ","Enter contact email");
  187. gets(currentc->email);
  188. printf("contact added!");
  189. currentc->count=0;
  190.  
  191. /*
  192.  * gives the new record a NULL pointer
  193.  * to show it's the last record:
  194.  */
  195.  
  196. currentc->next = NULL;
  197. }
  198. /*----------------------------------------------------------------------------*/
  199. void listAll(void) /* list all contacts function*/
  200. {
  201. if(firstc==NULL)
  202. puts("There are no contacts to display!"); /*prints message*/
  203.  
  204. else
  205. {
  206. printf("%6s %-20s %-15s %-15s\n","Acct#","Name","Phone","Email");
  207. puts("------ -------------------- ------------- -------------------");
  208. /*prints table titles*/
  209. currentc=firstc;
  210.  
  211. do
  212. {
  213.  
  214. printf("%6d: %-20s %-15s %-20s\n",\
  215. currentc->number,\
  216. currentc->name,\
  217. currentc->phone,\
  218. currentc->email);
  219. /*prints values of number, name, phone and email*/
  220. }
  221.  
  222. while((currentc=currentc->next) != NULL);
  223. }
  224. }
  225. /*----------------------------------------------------------------------------*/
  226. void deletecontact(void) /*delete contact function */
  227. {
  228. int record;
  229. struct contact *previousa;
  230.  
  231. if(firstc==NULL)
  232. {
  233. puts("There are no contacts to delete!");
  234. return;
  235. }
  236.  
  237. listAll(); /* show all records*/
  238. printf("Enter contact account number to delete: ");
  239. scanf("%d",&record);
  240.  
  241. currentc = firstc;
  242.  
  243. while(currentc != NULL)
  244. {
  245. if(currentc->number == record)
  246. {
  247. if(currentc == firstc) /*if record to be deleted is the first record*/
  248. firstc=currentc->next; /*reset firstc to point at next record as first*/
  249. else
  250. previousa->next = currentc->next;/*previous pointer used if record*/
  251. /*to delete is not the first*/
  252. free(currentc); /*frees memory <deletes>*/
  253. printf("contact %d deleted!\n",record);
  254. return;
  255. }
  256.  
  257. else
  258. {
  259. previousa = currentc;
  260. currentc = currentc->next;
  261. }
  262. }
  263. printf("contact %d not found!\n",record);
  264. }
  265. /*----------------------------------------------------------------------------*/
  266. void modifycontact(void) /*modify contact function*/
  267. {
  268. int record, result;
  269.  
  270. if(firstc==NULL)
  271. {
  272. puts("There are no contacts to modify!");
  273. return;
  274. }
  275.  
  276. listAll(); /* show all records */
  277. printf("Enter contact account number to modify or change: ");
  278. scanf("%d",&record); /*scan user input to record*/
  279.  
  280. result = findnum(record);
  281.  
  282. if( result >0 ){
  283. printf("Contact %d:\n",currentc->number);
  284. printf("Name: %s\n",currentc->name);
  285. if(prompt())
  286. gets(currentc->name);
  287. printf("Phone: %s\n",currentc->phone);
  288. if(prompt())
  289. gets(currentc->phone);
  290. printf("Email: %s\n",currentc->email);
  291. if(prompt())
  292. gets(currentc->email);
  293. return;
  294. }
  295. printf("contact %d was not found!\n",record);
  296. }
  297. /*----------------------------------------------------------------------------*/
  298. int findnum (int recordnum)
  299. {
  300. int record;
  301. record = recordnum;
  302. currentc = firstc;
  303. while(currentc != NULL)
  304. {
  305.  
  306. if(currentc->number == record)
  307. {
  308. return 1;
  309. }
  310.  
  311. else
  312. {
  313. currentc = currentc->next;
  314. }
  315. }
  316. return -1;
  317. }
  318. /*----------------------------------------------------------------------------*/
  319. int findcontact(void) /* find contact function*/
  320. {
  321. char buff[20];
  322.  
  323. if(firstc==NULL)
  324. {
  325. puts("There are no contacts to find!");
  326. return 1;
  327. }
  328.  
  329. printf("Enter contact name: ");
  330. fflush(stdin);/*clears any text from the input stream*/
  331. gets(buff);
  332.  
  333. currentc = firstc;
  334. while(currentc != NULL)
  335. {
  336. if( strcmp(currentc->name, buff) == 0 )
  337. {
  338. printf("%6s %-20s %-15s %-15s\n","Acct#","Name","Phone","Email");
  339. /*prints table titles*/
  340. printf("%6d: %-20s %-15s %-20s\n",\
  341. currentc->number,\
  342. currentc->name,\
  343. currentc->phone,\
  344. currentc->email);
  345. /*prints values of number, name, phone and email*/
  346.  
  347. return 0;
  348. }
  349. else
  350. {
  351. currentc = currentc->next;
  352. }
  353. }
  354. printf("contact %s was not found!\n",buff);
  355. return 1;
  356. }
  357. /*----------------------------------------------------------------------------*/
  358. int prompt(void)
  359. {
  360. char ch;
  361.  
  362. fflush(stdin);
  363. printf("Update? (Y to update any other key to not)");
  364. ch = getchar();
  365. ch = toupper(ch);
  366. fflush(stdin);
  367. if(ch == 'Y')
  368. {
  369. printf("Enter new value: ");
  370. return(1);
  371. }
  372. else
  373. return(0);
  374. }
  375. /*----------------------------------------------------------------------------*/
  376.  
  377. /* END OF PROGRAM */
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 4:41 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC