Hi I am a beginner learning C and welcome opinion / constructive criticism of a phone book program I wrote. If it is legible, needs comments, makes sense , just done plain old wrong 🙂
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Contact{
char name[15];
char surname[15];
char number[15];
}entry;
int const SIZE = 22;
int count;
entry *book;
FILE *store;
int open_store(char *mode){
store = fopen("store.dat", mode);
if (store == NULL){
return 1;
}
return 0;
}
void rewrite_store(){
int index;
open_store("w");
for (index = 0; index < count; index++){
fprintf(store, "\n%s\t%s\t%s", book[index].name, book[index].surname, book[index].number);
}
fclose(store);
}
void load_book(){
while(fscanf(store, "%s", book[count].name) == 1){
fscanf(store, "\t%s\t%s\n", book[count].surname, book[count].number);
count++;
}
}
void print_book(){
int loop = count;
int index = 0;
while (loop > 0){
printf("#-%d:%s %s:%s\n", index, book[index].name, book[index].surname, book[index].number);
loop--;
index++;
}
}
void sort_book(){
int index, index_1, index_2;
for(index_1 = 0; index_1 < count; index_1++){
for(index_2 = index_1 + 1; index_2 < count; index_2++){
if (strcmp(book[index_1].surname, book[index_2].surname) > 0){
book[count + 1] = book[index_1];
book[index_1] = book[index_2];
book[index_2] = book[count + 1];
}
}
}
book[count + 1] = book [count + 2];
rewrite_store();
}
void search(){
char temp [15];
int index;
int bin_index = 0;
int bin[10];
printf("Enter last Name:");
scanf("%s", temp);
for (index = 0; index < count + 1; index++){
if (strcmp(temp, book[index].surname) == 0){
bin[bin_index] = index;
bin_index++;
}
}
if (bin_index > 0){
int i;
for (i = 0; i < bin_index; i++){
printf("#-%d:%s %s:%s\n",bin[i], book[bin[i]].name, book[bin[i]].surname, book[bin[i]].number);
}
}
}
void new_entry(){
if (count > SIZE - 3){
printf("Phone Book Full\n");
return;
}
printf("First Name:");
scanf("%s", book[count].name);
printf("Last Name:");
scanf("%s", book[count].surname);
printf("Number:");
scanf("%s", book[count].number);
count++;
rewrite_store();
}
void delete_entry(){
int select;
int confirm;
print_book();
printf("Enter Index # To Delete:");
scanf("%d", &select);
printf("Delete #- %d:%s %s:%s\n", select, book[select].name, book[select].surname, book[select].number);
printf("ReEnter Index # To Confirm:");
scanf("%d", &confirm);
if (confirm == select){
int i;
for (i = select; i < count; i++){
book[i] = book[i + 1];
}
count--;
rewrite_store();
}
}
int menu(){
int selection;
printf("\nMENU\n");
printf("EXIT - 9 \tShow All-1 \tAdd New - 2\n");
printf("Sort - 3 \tSearch - 4 \tDelete Contact - 5\n");
scanf("%d", &selection);
switch (selection){
case 1:
print_book();
return 1;
case 2:
new_entry();
return 1;
case 3:
sort_book();
return 1;
case 4:
search();
return 1;
case 5:
delete_entry();
return 1;
case 9:
break;
default:
break;
}
return 0;
}
int main(){
book = (entry*) malloc(sizeof(entry)* SIZE);
int state = 1;
state = open_store("r");
if (state == 1){
state = open_store("w");
fclose(store);
state = open_store("r");
}
if (state == 1){
printf("Error Opening Phone Book");
return 0;
}
load_book();
fclose(store);
state = 1;
printf("*******Phone Book*******\n");
while (state == 1){
state = menu();
}
free(book);
return 0;
}