hi, i'm supposed to create a very simple address program in C & not in C++. my program works but my problem is how to "search & edit a record" and how to "search & delete a record", that is what i lack. this program is really giving me a headache. im also confused on how to use the "strcmp" or "stricmp".
i hope someone can help me. thnx!
this is what i've done so far:

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

struct person
{
	char fname[20];
	char lname[15];
	char address[50];
	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])
{
	char fn[20], ln[15], addy[30], tphone[10];
	int i;
	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;
}

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

		if ( choice=='d' )
			Display(prson, i);

	} while ( choice!='e' );
	printf("\n\n\n       Good Bye!");
	getch();
}

Recommended Answers

All 2 Replies

after line 65 you should clear the prson array by setting everything to 0 -- you can use memset() to do that very quickly

memset(prson,0, sizeof(prson));

to search for a given person you will, of course, first have to ask for the person's first and last names. Then create a loop and look at each prson object to compare last and first names in the structure with what you entered. you can use strcmp() to do that. In this code snippet assume i is the loop counter, lname is the last name you entered from the keyboard and fname is the first name you entered from the keyboard. Since strcmp() returns 0 when the two strings are identical you can test like this:

if( strcmp(lname,prson[i].lname) == 0 && strcmp(fname,prson[i].fname) == 0)
{
   // blabla
}

To delete an element from the array just memset() it to 0 again

memset(&prson[i], 0, sizeof(struct person));

Use of gets(): such as in line 31. Use fgets() instead because gets() will allow you to enter more keys than the buffer can hold. For example if you enter 30 character for last name then the program will crash because gets() will scribble all over memory with the extra characters. fgets() does not have that problem because you tell it the maximum number of characters that the buffer will hold.

Line 30: you are using variable i before it has been initialized with anything. All it contains at that point is some random number. To correct that initialize it to 0 on the same line where it is declared (line 29).

As a way of explanation:
When you pass an argument to a function, that variable is always a copy of the one passed. Except pointers and strings made out of char arrays which are pass as a reference and not a value.

Said that, let's take this function as a example:

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

char Selection(char choice)
That choice variable is a copy of the same declared at main. It has some use, but the value passed is unnecessary, since it doesn't matter at all in the execution of the function.
You could have writen it like this and still work:

int Selection()
{
    int result = 0;

    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: ");
    
    return ( result = getchar() );
}

Been declared and called at main like this:

int choice = 0;

choice = Selection();

knowing this, think of how many struct variables you are making copies of in you functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.