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