Wednesday, November 1, 2023

Chapter 1: Contact Management in C | Flow Chart and Functions

<<Previous Chapter                                                                                                            Next Chapter>> 

Application Flow Chart:




Functions: 
We will divide the above application into two kinds of functions. The first type of function is user functions which interact with the user to get input and display output, the second type of function is operational functions which do the actual tasks. 

The list of user functions is as follows:
  • mainMenu(): It displays the options to the user. 
  • viewItems(): It displays the list of contact information on the screen. 
  • addItems(): It requests the user to input contact information added to the file.
  • updateItems(): It requests the user to update the contact information in the file.
  • deleteItems(): It requests the user to delete the contact information in the file.
  • readData(): It requests the user to input contact information.
The list of operational functions is as follows:
  • view(): It fetches the data from the file.
  • add(): It adds the data into the file.
  • update(): It updates the existing data in the file.
  • del(): It deletes the existing data from the file.
  • searchByNum(): It searches the data in the file based on unique data elements. In this case, it is a phone number.
  • getString(): It reads the data input and saves it into the data structure.
Please find the source code up to this part below.

// Define C standard headers
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include "gotoxy.h"

// Define Constant Variables
#define ESC 27

// Declare functions
void mainMenu();
void viewItems();
void addItems();
void updateItems();
void deleteItems();

// C main function
int main(){
    mainMenu();
    return 0;
}

//Start function definitions

//Display user menu
void mainMenu(){


    char choice;

    system("cls");

    gotoxy(35,6);
    printf("----Welcome to Contact Management System----");
    gotoxy(35,8);
    printf("Please select the any option v, a, u, d, e or ESC");
    gotoxy(35,10);
    printf("V) View All Contact");
    gotoxy(35,12);
    printf("A) Add Contact");
    gotoxy(35,14);
    printf("U) Update Contact");
    gotoxy(35,16);
    printf("D) Delete Contact");
    gotoxy(35,18);
    printf("E) Exit application");
    gotoxy(35,20);
    printf("Please enter ESC to exit application");

    choice=tolower(getch());

    switch(choice){
        case 'v':
            viewItems();
            break;
        case 'a':
            addItems();
            break;
        case 'u':
            updateItems();
            break;
        case 'd':
            deleteItems();
            break;
        case 'e':
            exit(0);
        case ESC:
            exit(0);
        default:
            gotoxy(35,30);
            printf("Please enter options V, A, U, D, E or ESC");
            if(getch())
                mainMenu();
    }
}

//Display contacts only 15 items
void viewItems(){

    system("cls");
    gotoxy(35,6);
    printf("----Welcome to Contact Management System----");
    gotoxy(35,8);
    printf("View Contact function selected");

    gotoxy(35,27);
    printf("Please enter ESC key to go back.");
    if(getch()==ESC){
        mainMenu();
    } else {
        viewItems();
    }

}

//Add contacts
void addItems(){

    system("cls");
    gotoxy(35,6);
    printf("----Welcome to Contact Management System----");
    gotoxy(35,8);
    printf("Add Contact function selected");

    gotoxy(35,27);
    printf("Please press any key to continue or enter ESC key to go back.");
    if(getch()==ESC){
        mainMenu();
    } else {
        addItems();
    }
}

//Update contact details
void updateItems(){

    system("cls");
    gotoxy(35,6);
    printf("----Welcome to Contact Management System----");
    gotoxy(35,8);
    printf("Update Contact function selected");

    gotoxy(35,27);
    printf("Please enter any key to continue or enter ESC key to go back.");
    if(getch()==ESC){
        mainMenu();
    } else {
        updateItems();
    }
}

//Delete contacts details
void deleteItems(){

    system("cls");
    gotoxy(35,6);
    printf("----Welcome to Contact Management System----");
    gotoxy(35,8);
    printf("Delete Contact function selected");

    gotoxy(35,27);
    printf("Please enter any key to continue or enter ESC key to go back.");

    if(getch()==ESC){
        mainMenu();
    } else {
        deleteItems();
    }
}
Explaining the Code:

There isn't much to explain the above code, most part of it is self-explanatory. It will display the options to the user and based on the user input the respective functions are called to do a specific task. After completing tasks user can go back to the main screen by pressing the ESC key, and select another option, or exit the application.

Also, I am using gotoxy(x,y) function to align the text. You will see this function throughout my articles. Please find the my article gotoxy in Code::blocks to learn about it.

We will add file handling code and explain it in detail in the next chapter.

You can also watch a video about this project on YouTube.


No comments:

Post a Comment