HTMLify
contacts.c
Views: 1 | Author: abh
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[128];
char phone[16];
char email[128];
} Contact;
void print_contact(Contact contact) {
printf("Name: %s\n", contact.name);
printf("Phone: %s\n", contact.phone);
printf("Email: %s\n", contact.email);
}
int main() {
Contact contacts[1024];
int contact_count = 0, choice, id;
char ss = '|';
while (0x1) {
printf("0: Exit\n");
printf("1: Add Contact\n");
printf("2: Print Contact\n");
printf("3: List Contacts\n");
printf(": ");
scanf("%d", &choice);
switch (choice) {
case 0:
exit(0);
break;
case 1:
Contact contact;
printf("Enter the name for the contact \n");
scanf("%s", contact.name);
printf("Enter the phone number for the contact \n");
scanf("%s", contact.phone);
printf("Enter Email address for the contact \n");
scanf("%s", contact.email);
contacts[contact_count] = contact;
contact_count++;
printf("New contact with ID %d created succsesfully\n", contact_count);
print_contact(contact);
break;
case 2:
printf("Enter ID for the contact\n");
scanf("%d", &id);
if (id > contact_count || id < 1) {
printf("Contact with this id does not exists \n");
continue;
}
print_contact(contacts[id-1]);
break;
case 3:
printf("|-|-----------------------\n");
for (int i=0; i<contact_count; i++) {
int id = i+1;
if (id == 1000 || id == 100 || id == 10) {
ss = '\\';
} else {
ss = '|';
}
printf("|%d%c%s\n", id, ss, &contacts[i].name);
}
printf("|----|-----------------------\n");
break;
default:
printf("Not a valid choice\n");
}
}
}