Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Opinion's of Small program

Arthur

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

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom