954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help on simple turbo C program.. thx

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.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

struct person
{
   char fname[20];
   char lname[15];
   char address[10];
   char telephone[10];
};

char Selection(char choice)
{
   printf("a. Data Entry");
   printf("\nb. Search and Edit a Record");
   printf("\nc. Search and Delete a Record");
   printf("\nd. Display all Records");
   printf("\ne. Exit from the program");
   printf("\n\nEnter your choice: ");
   scanf(" %c",&choice);
   return choice;
}

int Data_Entry (struct person prson[20], int i)
{
   char fn[20], ln[15], addy[50], tphone[10];

   printf("Enter info for student no. %d",i+1);  gets("\n");
   printf("\nFirst Name: ");  gets(fn);
   strcpy(prson[i].fname, fn);
   printf("Last Name: ");  gets(ln);
   strcpy(prson[i].lname, ln);
   printf("Address: ");  gets(addy);
   strcpy(prson[i].address, addy);
   printf("Telephone No.: ");  gets(tphone);
   strcpy(prson[i].telephone, tphone);
   i++;
   return i;
}

int Data_Edit(struct person prson[20], int i)
{
   int j=0, k=3, found=0;  char ans;  char last[15];  char first[20];
   char lastn[15];  char firstn[20];  char addr[10];  char tele[10];

   printf("Enter last name: ");  gets("\n");  gets(last);
   printf("\nEnter first name: ");  gets(first);

   for ( j=0;j<i;j++ )
   {
      if ( stricmp(last, prson[j].lname)==0 &&
	   stricmp(first, prson[j].fname)==0 )
      {
	 found=1;
	 clrscr();
	 printf("Name                     Address                   Tel. No.");
	 gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname);
	 gotoxy(30,k);printf("%s",prson[j].address);
	 gotoxy(60,k);printf("%s",prson[j].telephone);

	 gotoxy(1,k+3);printf("Continue editing?(Y/N): ");
	 scanf(" %c",&ans);  gets("\n");
	 if ( ans=='y' || ans=='Y' )
	 {
	    printf("\n\nEnter last name: ");  gets(lastn);
	    strcpy(prson[j].lname, lastn);
	    printf("\nEnter first name: ");  gets(firstn);
	    strcpy(prson[j].fname, firstn);
	    printf("\nAddress: ");  gets(addr);
	    strcpy(prson[j].address, addr);
	    printf("\nTelephone No.: ");  gets(tele);
	    strcpy(prson[j].telephone, tele);
	 }
      }
   }
   if ( found==0 )  
   {
      printf("\n\nThe name you entered cannot be found.");
      getch();
   }

}

int Data_Delete(struct person prson[20], int i)
{
   int a=0, j=0, k=3, found=0;  char ans2;  char lstn[15];
   char fstn[20];  char addre[10];  char telle[10];

   printf("Enter last name: ");  gets("\n");  gets(lstn);
   printf("\nEnter first name: ");  gets(fstn);
   for ( j=0;j<i;j++ )
   {
      if ( stricmp(lstn, prson[j].lname)==0 &&
	   stricmp(fstn, prson[j].fname)==0 )
      {
	 found=1;
	 clrscr();
	 printf("Name                     Address                   Tel. No.");
	 gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname);
	 gotoxy(30,k);printf("%s",prson[j].address);
	 gotoxy(60,k);printf("%s",prson[j].telephone);
	 
	 gotoxy(1,k+3);printf("Continue deleting?(Y/N): ");
	 scanf(" %c",&ans2);  gets("\n");
	 if ( ans2=='y' || ans2=='Y' )
         {
	    strcpy(prson[j].lname, prson[j+1].lname);
	    strcpy(prson[j].fname, prson[j+1].fname);
	    i--;
	 }
      }
   }
   if ( found==0 )
   {
      printf("\n\nThe name you entered cannot be found.");
      getch();
   }
   return i;
}

Display(struct person prson[20], int i)
{
   int j, k=3;
   clrscr();
   printf("Name                     Address                   Tel. No.");
   for ( j=0;j<i;j++ )
   {
      gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname);
      gotoxy(30,k);printf("%s",prson[j].address);
      gotoxy(60,k);printf("%s",prson[j].telephone);
      k++;
   }
   getch();
}

typedef struct person prson;

main()
{
   int i=0;
   struct person prson[20];
   char choice='';
   do
   {
      clrscr();
      choice=Selection(choice);
      if ( choice=='a' )
      {
	 clrscr();
	 i=Data_Entry(prson,i);
      }
      if ( choice=='b' )
      {
	 clrscr();
	 Data_Edit(prson, i);
      }
      if ( choice=='c' )
      {
	 clrscr();
	 i=Data_Delete(prson,i);
      }
      if ( choice=='d' )
      {
	 Display(prson, i);
      }
   } while ( choice!='e' );
   printf("\n\n\n       Good Bye!");
   getch();
}
ellenski
Light Poster
38 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 

#include
#include

int main()
{

shiju8831
Newbie Poster
1 post since May 2010
Reputation Points: 9
Solved Threads: 1
 

Your main function contains a while loop in which you check for which selection is made. But you do not increment the value of i. So if I were to chose option a 5 times, it will enter data in prson[0] only.

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

Welcome shiju8831

If you have any questions please ask. You are welcome to start your own threads. Please do not resurrect threads that are years old. By doing so you run the risk of confusing current posters.

Thread Closed.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You