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();
}