need help on simple turbo C program.. thx

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 2
Reputation: ellenski is an unknown quantity at this point 
Solved Threads: 0
ellenski ellenski is offline Offline
Newbie Poster

need help on simple turbo C program.. thx

 
-1
  #1
Aug 18th, 2007
My program is a very simple address book program. The program works but i don't know how to make it display the same names. Like for example, the name "Mark Lee", if there are more than one Mark Lee, i would like to display all of those Mark Lee w/ its corresponding address & telephone no. How do i do that? What do i have to change in my program? I'm really very confused.
This is what my program looks like right now.


  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. struct person
  7. {
  8. char fname[20];
  9. char lname[15];
  10. char address[10];
  11. char telephone[10];
  12. };
  13.  
  14. char Selection(char choice)
  15. {
  16. printf("a. Data Entry");
  17. printf("\nb. Search and Edit a Record");
  18. printf("\nc. Search and Delete a Record");
  19. printf("\nd. Display all Records");
  20. printf("\ne. Exit from the program");
  21. printf("\n\nEnter your choice: ");
  22. scanf(" %c",&choice);
  23. return choice;
  24. }
  25.  
  26. int Data_Entry (struct person prson[20], int i)
  27. {
  28. char fn[20], ln[15], addy[50], tphone[10];
  29.  
  30. printf("Enter info for student no. %d",i+1); gets("\n");
  31. printf("\nFirst Name: "); gets(fn);
  32. strcpy(prson[i].fname, fn);
  33. printf("Last Name: "); gets(ln);
  34. strcpy(prson[i].lname, ln);
  35. printf("Address: "); gets(addy);
  36. strcpy(prson[i].address, addy);
  37. printf("Telephone No.: "); gets(tphone);
  38. strcpy(prson[i].telephone, tphone);
  39. i++;
  40. return i;
  41. }
  42.  
  43. int Data_Edit(struct person prson[20], int i)
  44. {
  45. int j=0, k=3, found=0; char ans; char last[15]; char first[20];
  46. char lastn[15]; char firstn[20]; char addr[10]; char tele[10];
  47.  
  48. printf("Enter last name: "); gets("\n"); gets(last);
  49. printf("\nEnter first name: "); gets(first);
  50.  
  51. for ( j=0;j<i;j++ )
  52. {
  53. if ( stricmp(last, prson[j].lname)==0 &&
  54. stricmp(first, prson[j].fname)==0 )
  55. {
  56. found=1;
  57. clrscr();
  58. printf("Name Address Tel. No.");
  59. gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname);
  60. gotoxy(30,k);printf("%s",prson[j].address);
  61. gotoxy(60,k);printf("%s",prson[j].telephone);
  62.  
  63. gotoxy(1,k+3);printf("Continue editing?(Y/N): ");
  64. scanf(" %c",&ans); gets("\n");
  65. if ( ans=='y' || ans=='Y' )
  66. {
  67. printf("\n\nEnter last name: "); gets(lastn);
  68. strcpy(prson[j].lname, lastn);
  69. printf("\nEnter first name: "); gets(firstn);
  70. strcpy(prson[j].fname, firstn);
  71. printf("\nAddress: "); gets(addr);
  72. strcpy(prson[j].address, addr);
  73. printf("\nTelephone No.: "); gets(tele);
  74. strcpy(prson[j].telephone, tele);
  75. }
  76. }
  77. }
  78. if ( found==0 )
  79. {
  80. printf("\n\nThe name you entered cannot be found.");
  81. getch();
  82. }
  83.  
  84. }
  85.  
  86. int Data_Delete(struct person prson[20], int i)
  87. {
  88. int a=0, j=0, k=3, found=0; char ans2; char lstn[15];
  89. char fstn[20]; char addre[10]; char telle[10];
  90.  
  91. printf("Enter last name: "); gets("\n"); gets(lstn);
  92. printf("\nEnter first name: "); gets(fstn);
  93. for ( j=0;j<i;j++ )
  94. {
  95. if ( stricmp(lstn, prson[j].lname)==0 &&
  96. stricmp(fstn, prson[j].fname)==0 )
  97. {
  98. found=1;
  99. clrscr();
  100. printf("Name Address Tel. No.");
  101. gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname);
  102. gotoxy(30,k);printf("%s",prson[j].address);
  103. gotoxy(60,k);printf("%s",prson[j].telephone);
  104.  
  105. gotoxy(1,k+3);printf("Continue deleting?(Y/N): ");
  106. scanf(" %c",&ans2); gets("\n");
  107. if ( ans2=='y' || ans2=='Y' )
  108. {
  109. strcpy(prson[j].lname, prson[j+1].lname);
  110. strcpy(prson[j].fname, prson[j+1].fname);
  111. i--;
  112. }
  113. }
  114. }
  115. if ( found==0 )
  116. {
  117. printf("\n\nThe name you entered cannot be found.");
  118. getch();
  119. }
  120. return i;
  121. }
  122.  
  123. Display(struct person prson[20], int i)
  124. {
  125. int j, k=3;
  126. clrscr();
  127. printf("Name Address Tel. No.");
  128. for ( j=0;j<i;j++ )
  129. {
  130. gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname);
  131. gotoxy(30,k);printf("%s",prson[j].address);
  132. gotoxy(60,k);printf("%s",prson[j].telephone);
  133. k++;
  134. }
  135. getch();
  136. }
  137.  
  138. typedef struct person prson;
  139.  
  140. main()
  141. {
  142. int i=0;
  143. struct person prson[20];
  144. char choice='';
  145. do
  146. {
  147. clrscr();
  148. choice=Selection(choice);
  149. if ( choice=='a' )
  150. {
  151. clrscr();
  152. i=Data_Entry(prson,i);
  153. }
  154. if ( choice=='b' )
  155. {
  156. clrscr();
  157. Data_Edit(prson, i);
  158. }
  159. if ( choice=='c' )
  160. {
  161. clrscr();
  162. i=Data_Delete(prson,i);
  163. }
  164. if ( choice=='d' )
  165. {
  166. Display(prson, i);
  167. }
  168. } while ( choice!='e' );
  169. printf("\n\n\n Good Bye!");
  170. getch();
  171. }
Last edited by Ancient Dragon; Aug 19th, 2007 at 6:12 am. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: need help on simple turbo C program.. thx

 
0
  #2
Aug 19th, 2007
>>The program works
No it doesn't.

you use gets() all over the place. Try entering some text that is longer than the buffer can hold and you will find that your program doesn't work afterall. gets() will just scribble all over memory with the extra characters and cause your program to crash at some point. The solution is to use fgets() instead to limit data input.

line 30: parameter to gets() is wrong.

in main() variable i is apparently being used to mark the next available slot in the prson array. But then you use i again for several other reasons. I think you need to set up a different variable for that purpose and not use it for anything else. You also need to verify that you don't try to enter more than 20 people because that's all the array can hold -- if you add the 21st person your program will crash.

As for your original question -- I think you need another menu option to show records for only a specific person then prompt for that person's name. Then its a simple loop to search the array for the person and print it when found.
Last edited by Ancient Dragon; Aug 19th, 2007 at 6:21 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: need help on simple turbo C program.. thx

 
0
  #3
Aug 19th, 2007
I want to say only that, use some more modern compiler than turbo c, like gcc (mingw or cygwin in windows). Turbo c is an extinct compiler, somewhat hopelessly outdated, and you cannot find libraries for it etc. Otherwise, using conio is perfectly ok, there is conio for windows, and there is also conio for linux, so we may say it's a cross-platform library, and at that very easy to implement. There are really no better alternatives for windows, because windows console is so primitive that it cannot accept ansi terminal escape sequences. And it doesn't make sense to learn curses, as today it doesn't make sense any more to write user interfaces in text mode, but gui is a bit too complicated for the most simple programs.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC