View Single Post
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Please help with this menu

 
0
  #1
Jan 8th, 2009
Okay, so I have a menu and a linked list, but I have three problems. First of, if you select option one and enter a name it keeps prompting you to enter another and another. It won't accept any other prompt to do anything else like, for instance, exit the program.

Next, I want to be able to insert a name into the list or modify a name. I just can't figure out how to do those two. Any help would be greatly appreciated. The current code that I have generates the following errors:

error C2062: type 'void' unexpected
error C3861: 'modify': identifier not found

If I remove void insertend(void), void modify(void); those errors disappear, but I included them in anticipation of getting the insert and modify features working.

Here is the code:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct names {
  6. char name[30];
  7. struct names *next; /* pointer to next entry */
  8. struct names *prior; /* pointer to previous record */
  9. };
  10.  
  11. struct names *start; /* pointer to first entry in list */
  12. struct names *last; /* pointer to last entry */
  13. struct names *find(char *);
  14.  
  15. void enter(void);
  16. void load(void), list(void);
  17. void insertend(void), void modify(void);
  18. void dls_store(struct names *i, struct names **start,
  19. struct names **last);
  20. void inputs(char *, char *, int), display(struct names *);
  21. int menu_select(void);
  22.  
  23. int main(void)
  24. {
  25. start = last = NULL; /* initialize start and end pointers */
  26.  
  27. for(;;) {
  28. switch(menu_select()) {
  29. case 1: enter(); /* enter a name */
  30. break;
  31. case 2: insertend(); /* add a name to the list*/
  32. break;
  33. case 3: modify(); /* modify a name on the list*/
  34. break;
  35. case 5: list(); /* display the list */
  36. break;
  37. case 6: load(); /* read from disk */
  38. break;
  39. case 7: exit(0);
  40. }
  41. }
  42. return 0;
  43. }
  44.  
  45. /* Select an operation. */
  46. int menu_select(void)
  47. {
  48. char s[80];
  49. int c;
  50.  
  51. printf("1. Create a list of names\n");
  52. printf("2. Insert a name into the list.\n");
  53. printf("3. Modify a name on list.\n");
  54. printf("4. Delete a name from list.\n");
  55. printf("5. Display the list\n");
  56. printf("6. Load the file\n");
  57. printf("6. Quit\n");
  58. do {
  59. printf("\nEnter your choice: ");
  60. gets(s);
  61. c = atoi(s);
  62. } while(c<0 || c>7);
  63. return c;
  64. }
  65.  
  66. /* Enter names. */
  67. void enter(void)
  68. {
  69. struct names *info;
  70.  
  71. for(;;) {
  72. info = (struct names *)malloc(sizeof(struct names));
  73. if(!info) {
  74. printf("\nout of memory");
  75. return;
  76. }
  77.  
  78. inputs("Enter name: ", info->name, 30);
  79. if(!info->name[0]) break; /* stop entering */
  80.  
  81. dls_store(info, &start, &last);
  82. } /* entry loop */
  83. }
  84.  
  85. /* This function will input a string up to
  86.   the length in count and will prevent
  87.   the string from being overrun. It will also
  88.   display a prompting message. */
  89. void inputs(char *prompt, char *s, int count)
  90. {
  91. char p[255];
  92.  
  93. do {
  94. printf(prompt);
  95. fgets(p, 254, stdin);
  96. if(strlen(p) > count) printf("\nToo Long\n");
  97. } while(strlen(p) > count);
  98.  
  99. p[strlen(p)-1] = 0; /* remove newline character */
  100. strcpy(s, p);
  101. }
  102.  
  103. /* Create a doubly linked list in sorted order. */
  104. void dls_store(
  105. struct names *i, /* new element */
  106. struct names **start, /* first element in list */
  107. struct names **last /* last element in list */
  108. )
  109. {
  110. struct names *old, *p;
  111.  
  112. if(*last==NULL) { /* first element in list */
  113. i->next = NULL;
  114. i->prior = NULL;
  115. *last = i;
  116. *start = i;
  117. return;
  118. }
  119. p = *start; /* start at top of list */
  120.  
  121. old = NULL;
  122. while(p) {
  123. if(strcmp(p->name, i->name)<0){
  124. old = p;
  125. p = p->next;
  126. }
  127. else {
  128. if(p->prior) {
  129. p->prior->next = i;
  130. i->next = p;
  131. i->prior = p->prior;
  132. p->prior = i;
  133. return;
  134. }
  135. i->next = p; /* new first element */
  136. i->prior = NULL;
  137. p->prior = i;
  138. *start = i;
  139. return;
  140. }
  141. }
  142. old->next = i; /* put on end */
  143. i->next = NULL;
  144. i->prior = old;
  145. *last = i;
  146. }
  147.  
  148. /* Remove an element from the list. */
  149. void mldelete(struct names **start, struct names **last)
  150. {
  151. struct names *info;
  152. char s[80];
  153.  
  154. inputs("Enter name: ", s, 30);
  155. info = find(s);
  156. if(info) {
  157. if(*start==info) {
  158. *start=info->next;
  159. if(*start) (*start)->prior = NULL;
  160. else *last = NULL;
  161. }
  162. else {
  163. info->prior->next = info->next;
  164. if(info!=*last)
  165. info->next->prior = info->prior;
  166. else
  167. *last = info->prior;
  168. }
  169. free(info); /* return memory to system */
  170. }
  171. }
  172.  
  173. /* Find a name. */
  174. struct names *find( char *name)
  175. {
  176. struct names *info;
  177.  
  178. info = start;
  179. while(info) {
  180. if(!strcmp(name, info->name)) return info;
  181. info = info->next; /* get next name */
  182. }
  183. printf("Name not found.\n");
  184. return NULL; /* not found */
  185. }
  186.  
  187. /* Display the entire list. */
  188. void list(void)
  189. {
  190. struct names *info;
  191.  
  192. info = start;
  193. while(info) {
  194. display(info);
  195. info = info->next; /* get next name*/
  196. }
  197. printf("\n\n");
  198. }
  199.  
  200. /* This function prints the fields. */
  201. void display(struct names *info)
  202. {
  203. printf("%s\n", info->name);
  204. printf("\n\n");
  205. }
  206.  
  207. /* Load the file. */
  208. void load()
  209. {
  210. struct names *info;
  211. FILE *fp;
  212.  
  213. fp = fopen("mlist", "rb");
  214. if(!fp) {
  215. printf("Cannot open file.\n");
  216. exit(1);
  217. }
  218.  
  219. /* free any previously allocated memory */
  220. while(start) {
  221. info = start->next;
  222. free(info);
  223. start = info;
  224. }
  225.  
  226. /* reset top and bottom pointers */
  227. start = last = NULL;
  228.  
  229. printf("\nLoading File\n");
  230. while(!feof(fp)) {
  231. info = (struct names *) malloc(sizeof(struct names));
  232. if(!info) {
  233. printf("Out of Memory");
  234. return;
  235. }
  236. if(1 != fread(info, sizeof(struct names), 1, fp)) break;
  237. dls_store(info, &start, &last);
  238. }
  239. fclose(fp);
  240. }
Reply With Quote