Help in Deleting a Record

Hi guys, can anyone help me in deleting a record? I can't figure out my deleterec function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <string.h>

#define size 100
#define namesize 20

struct record {
	char name[namesize];
	int phone;
};

struct record data[size];
int idx = 0;


int input_option() {
	int option;

	printf("\n[*] Select an option\n\n");
	printf("1. Add Record\n");
	printf("2. Search Record\n");
	printf("3. Display All Records\n");
	printf("4. Delete a Record\n");
	printf("5. Exit\n\n>> ");

	scanf("%d", &option);
	return option;
}

void addrecord(struct record d[], int *ind) {
	printf("\n[~] Enter Name: ");
	fflush(stdin);
	fgets((d[*ind].name), sizeof(d[*ind].name), stdin);
 	printf("[~] Enter Phone: ");
 	scanf("%i", &d[*ind].phone);
	(*ind)++;
}

void displayrecord(struct record d[], int ind) {
	int i;
	for (i = 0; i < ind; ++i) printf("\n[+] Name: %s\n[+] Phone: %i\n\n", d[i].name, d[i].phone);

}

void search(struct record d[], int ind) {
	char key[namesize];
	int i;
	printf("Enter your name to search: "); fflush(stdin);
	fgets(key, sizeof(key), stdin);
	for (i = 0; i < ind; ++i)
		if(strcmp(key,d[i].name)==0)
			printf("\n[+] Name: %s\n[+] Phone: %i\n\n", d[i].name, d[i].phone);

}

void deleterec(struct record d[], int *ind) {
	char key[namesize];
	char t[20] = {'\0'};
	int i;
	fflush(stdout);
	printf("[?] Enter your name to delete: "); fflush(stdin);
	fgets(key, sizeof(key), stdin);
	for (i = 0; i <= *ind; ++i) {
			if(strcmp(key, d[i].name)== 0) {
				strcpy(d[i].name, t);
				//d[i].phone = 0;
				(*ind)--;
				printf("\n[!] Entry Deleted\n");
				break;
			}

	}
	printf("\n[-] Entry Not Found\n");

}

int
main () {
	//int opt;
	while (1) {
		switch(input_option()) { // while ((opt=input_option())!=4)
			case 1:addrecord(data, &idx); break;
			case 2:search(data, idx); break;
			case 3:displayrecord(data, idx); break;
			case 4:deleterec(data, &idx); break;
			case 5:return 0;
			default: printf("\nEnter a valid option");
		}


	}

}
Your data is being stored in an array, so technically there is no "delete". It looks like you are maintaining the index of the last element of the array in idx, so after "deleting" an element from the array, idx should be one less than it was originally.

You will also need to shift all of the elements in your array back one space from the location of your deleted record to effectively overwrite the record you are removing and make the last record line up with the new idx. You need a for loop starting at the deleted index to idx-1 doing something like this d[i]=d[i+1] ...
I am a bit confused in that part. Can you write me that function?
I haven't tested this at all, but something along these lines should do the trick. You can't have holes in your array, so you need to compact the array to fill the hole from the record you are deleting.

example:
array = [ a, b, c, d, e ]
idx is index 4 (record 'e')

delete record 'c'
array = [ a, b, d, e ]
idx is now index 3 (still record 'e')
a and b haven't moved, but d and e (the elements after the one you are deleting) have now shifted back by 1 each. You have to do this by copying d over c, then e over the old d.
If you were to look at index 4 at this point, it would still contain the contents from 'e' that you just copied, but it is not accessible because idx is 3, so any new loops will stop before reaching the old record 'e'.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void deleterec(struct record d[], int *ind) {
	char key[namesize];
	//char t[20] = {'\0'}; //not sure what this is used for...
	//int i; //moved this into your for loop
	fflush(stdout);
	printf("[?] Enter your name to delete: "); fflush(stdin);
	fgets(key, sizeof(key), stdin);
        //add a check to determine if something was found/deleted
        int deleted = 0; //initialize it to 0 (assume nothing gets deleted)

	for (int i = 0; i <= *ind; ++i) {
			if(strcmp(key, d[i].name)== 0) {
                                (*ind)--;
                                i--; //only do this if you need to delete more than one record
                                     //i-- here will rescan the index that was just removed and
                                     //now contains a new record.
                                deleted = 1; //flag that something was deleted
                                for(int j=i; j<*ind; j++) {
                                  d[j] = d[j+1];
                                }
				//strcpy(d[i].name, t);
				//d[i].phone = 0;
				//(*ind)--;
				printf("\n[!] Entry Deleted\n");
                                //only break here if you want to delete the first instance only
                                //Not sure how you want to handle multiple records with the same name
				//break;
			}

	}
        if(!deleted) {
	     printf("\n[-] Entry Not Found\n");
        }

}

Hey thanks! i figured this out by your first comment, even though it some time :) thanks again!
Last edited on
Topic archived. No new replies allowed.