Contact Management System In C Language

Prajukta Dey
5 min readNov 7, 2021

--

Part I

Hello, my anxious self is not doing okay. Jokes apart, this is my first ever medium post so I guess I am a little bit nervous.

I did this project not long back. Having learnt the basics of the C language, my father asked me to do a project, the project being none other than a contact management system. It deals heavily on the file handling part of the C language and this very first blog post of mine would deal on the construction of this project from the very scratch.

Before we dive right into this project, I would take a moment to introduce myself as it is my very first blog post. Hello there, I am Prajukta Dey. Currently studying Bachelor Of Technology and in my first year at Kalinga Institute of Industrial Technology. Thank you for bearing with me and without any further ado, let’s just dive right in.

Before you start working on this project, you should have a basic knowledge of C programming including file handling. We would be using a number of in-built library files for the easy working of this project.

The different header files required for this project

Take a look at the different library files, we will be working with in this project.

  1. The <stdio.h> header file stands for Standard Input Output.
  2. The <conio.h> header file is used because it includes inbuilt functions like getch() and clrscr().
  3. The <string.h> header file is used for string functions.
  4. The <process.h> header file which contains function declarations and macros used in working with threads and processes.
  5. The <stdlib.h> header defines four variable types, several macros, and various functions for performing general functions.
  6. The <dos.h> header file contains functions for handling interrupts, producing sound, date and time functions etc.
  7. The ctype. h header file declares several functions that are useful for testing and mapping characters.

Now that we have a thorough understanding as to which header file serve what kind of function, let’s dive into the code.

Structures

The structure is defined next. I have taken a long variable ‘ph’ for storing the phone number, a character array name, address and email for storing name, address and email respectively.

I have also taken a character array query and a name array. FILE *fp here refers to a file pointer which you already know if you have gone through the basics of file handling in C. I have also taken certain variables which I’ll be using soon.

You can do this project in many ways, some might prefer to write the different functions i.e. creating a new contact, deleting a contact etc. in the switch case itself. I on the other hand have created separate functions and called those functions using the the different cases of the switch case structure.

The main method should be written keeping certain points in mind.

Main Menu format

One of the points is, the menu should be repeatedly displayed on the screen after the completion of the said task unless the user chooses to exit from the program i.e. choose number 5 from the above displayed menu.

Remember that even if the user chooses ‘0’ the program should display an error message and display the menu again for the user to choose a number among the numbers displayed in the menu.

I have created a separate function for the ‘MAIN MENU’ of my Contact Management System.

Code for the Main Menu

I am really sorry that the print function for the main menu is not visible. I am attaching the line below for your better understanding.

printf(“\t\t\t\tMAIN MENU\n\n\t\t\t[1] Add a new Contact\n\t\t\t[2] List all Contacts\n\t\t\t[3] Search for contact\n\t\t\t[4] Edit a Contact\n\t\t\t[5] Delete a Contact\n\t\t\t[0] Exit\n”);

Now, I am pretty sure you are thinking, “How am I supposed to make this menu display repetitively?” Don’t you worry, I have got yourself covered.

I have made the magic happen in my main function.

How to make the main menu display repetitively

The ch is the number or choice that the user is supposed to give. Now, if you take a quick look in our menu, you’ll be able to see that the number 0 or the choice 0 means “The user wants to exit from the system and does not want to perform any task” which loosely translates to “You do not need to display the Main Menu if the user choose 0.” The above code does the same thing.

In line 179: while(1) loosely translates to while the given condition is false. It means as long as the user does not chooses 0 or wishes to exit from the system, the Main Menu will keep on displaying itself.

Now, let’s take a quick look at the switch case structure before looking at the functionality of the Contact Management System.

Switch case structure displaying cases 1–3
Switch case structure displaying cases 4–5 and default

Please do not get confused by looking at the different functions such as editContact() etc. I will be talking about these functions soon.

The switch case structure is rather very simple to understand. The user will give a number based on the numbers written in the main menu and the based on that number, the program would do the desired task.

The project is a bit long and I think it’s only fair if I divide it into two parts. The second part would comprise of all the different functions associated with a basic Contact Management System namely:

  1. Creating a new contact.
  2. Listing all the contacts.
  3. Searching for a contact.
  4. Editing a contact.
  5. Deleting a contact.

That being said, I will be uploading the next part soon. Stay tuned and happy coding!

--

--

Prajukta Dey

A student living in India currently into generative ai among many other things.