Phone book Application using C is a simple application that incorporated most of the features of C. I had discussed each of the features of this application between the code, although most of the code has comments. The phone book application provides features like storing a room/phone number into a database, sorting and finding the room number/phone number, etc. This phone book application uses the graphic library for interaction with users. This application can store up to five hundred entries but is not limited to it, user can increase and decrease as per their need. Users can add, update, delete, search and modify the details of the contact. This application has also the features of delete recovery so that users can recover the details in case of accidentally deleted.
The software code is written and compiled using Borland Turbo C++.
Source Code of A Phone Book Application Using C
The feature main() display a console menu that has the following option:
Add entry
Delete entry
Find room number
Find phone number
List all entries
Display total entries in the database
Sort entries
Load database from file
Exit
Each of these menu options calls the appropriate function for performing its designated operation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
/*********************************************** Program: Phone book Programmer: Krishna Keshab Chaudhary Compiled : Borland Turbo C++ ***************************************/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <graphics.h> #include <ctype.h> #include <string.h> #define MAXDB 500 /*Maximum number of entries in the phone book */ /* List of global variables */ int i; /*global index*/ long int phone[MAXDB+1]; int room[MAXDB+1]; /* Phone_tmp & room_tmo array's are temp storage used for delete recovery */ long int phone_tmp[MAXDB+1]; int room_tmp[MAXDB+1]; void AddEntry(int, long int); int add_count=0; /* master count for entries added */ int current_e_add; /* counter for current entries added within a giventime.*/ int DeleteEntry(int, long int); int FindPhone(long int); int FindRoom(int); int phone_found,room_found; int del_entry; /* count del entry at a given time */ int tot_del_entry=0; /*master del counter */ int ListAll(void); int ShortAll Entries(char); int GeTotal Entries(void); int chhstrdig (char str[], int range); char menu(void); void LoadDB(void); /*load database from file function */ void exitmenu(void); void drawscreen(void); void refreshscreen(void); char dbload[80]; /* loaded database */ void main(void) { char iroom[80],iphone[80],add_quit; char option,sortopt,exit_opt; /* menu,sort and exit option */ int phone_check,room_check,delete_check,sort_check,list_check; int iroom_search,iroom_del; int int_iroom,total_entries; int error_iphone,error_iroom; /*used to check input error's */ long int longint_iphone; long int iphone_search; long int iphone_del; /* init while no valid database files is loaded program will work in RAM! */ strcpy(dbload, "No database file loaded (RAM MODE!)."); /*MAIN MENU */ do { do { option = menu(); if (option == '1') /* AddEntry Option */ { current_e_add=0; /*init current entries added to zero.*/ for (i=add_count; i < MAXDB; i++) { clrscr(); refreshmenu(); drawscreen(); gotoxy(1,4); printf(">> Add Entry <<"); gotoxy(1,25); cprintf("Please Add Your Entry, leave blank to quit ti main menu"); gotoxy(1,6); printf("Enter Room Number[%3d]: ",i+1); gets(iroom); if (iroom[0] == '\0' ) /* user hits enter - quits */ { gotoxy(1,25); cprintf("You chose to quit: Enter %dd was not added to the database.",i+1); getch(); break; } printf("Enter Phone Number[%3d]:",i+1); gets(iphone); if (iphone[0] == '\0') /* user hits enter - quits */ { gotoxy(1,25); cprintf("You chose to quit: Enter %d was not added to the database.",i+1); getch(); break; } /* check the string for valid inputs */ error_iroom = chkstrdig(iroom,4); error_iphone = chkstdig(iphone,8); /* loop's while room input error (out of range/character) */ while(error_iroom != 0) { if (error_iroom == -1) { clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Add Entry <<"); gotoxy(1,25); cprintf("Error: Room Number - Out of Range, Your entry was greater then 4 digits. "); gotoxy(1,6); printtf("Renter Room Number[%3d]: ",i+1); gets(iroom); } if (error_iroom == -2) { clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf("*** Add Entry ***"); gotoxy(1,25); cprintf("Error: Room Number - Character(s) detected, character(s) are not allowed."); gotoxy(1,6);printf("Renter Room Number[%3d]: ",i+1); gets(iroom); }/* checks string room input if valid */ error_iroom = chkstrdig(iroom,4); }/*loop's while phone input error (out of range/character) */ while(error_iphone !=0) { if (error_iphone == -1) { clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Add Entry <<"); gotoxy(1,25); cprint("Error: Phone Number - out of range, Your entry was greater then 8 digits. "); gotoxy(1,6); printf(Room Number[%3d] Entry: %s",i+1,iroom); gotoxy(1,7); printf("Renter Phone Number[%3d]: ",i+1); gets(iphone); } if (error_iphone == -2) { clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Add Entry <<"); gotoxy(1,25); cprintf("Error: Phone Number - Character(s) detected, character(s) are not allowed."); gotoxy(1,6); printf("Room Number[%3d] Entry: %s",i+1,iroom); gotoxy(1,7); printf("Renter Phone Number[%3d]: ",i+1); gets(iphone); }/* checks phone input valid */ error_iphone = chkstrdig(iphone,8); } /*no room or phone input error - addentry */ if (error_iroom == 0 && error_iphone == 0) { int_iroom = atoi(iroom); /* converts string to long int */ current_e_add++; AddEntry(int_iroom,longint_iphone); } } if (add_count == MAXDB) /* database full */ { gotoxy(1,25); cprintf("\aDatabase is full!: %d entries were added, ",add_count); cprintf("that is the Maximum No. I can hold."); getch(); } } else if (option == '2') /* DeleteEntry option */ { del_entry = 0; /* Initialize del_entry country zero */ clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Delete Entry <<"); gotoxy(1,6); cprintf("Error room number to delete: "); scanf("%d",&iroom_del); flushall(); /* clears buffer */ printf("Enter phone number to delete: "); scanf("%ld",&iphone_del); flushall(); delete_check = DeleteEntry(iroom_del , iphone_del); if (delete_check == 0)/*successfully found or deleted entries display */ { gotoxy(1,25); cprintf("Successful: There are currently %d entries in the database,",add_count); cprintf("deleted %d.",del_entry); getch(); } if (delete_check == -1) /* error: doer not delete if db not found */ { gotoxy(1,25); cprintf("Error: The Room No./Phone No. Your looking for was Not Found."); getch(); } } else if (option == '3') /* FindPhone Option */ { phone_found = 0; /*initialize phone no. found to zero */ clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Find Room Number <<"); gotoxy(1,6); printf("Enter the phone number to search for: "); scanf("%ld",&iphone_search); flushall(); /* clear buffer */ phone_check = FindPhone(iphone_search); if (phone_check == o) /* return = 0 phone found */ { gotoxy(1,25); cprint("Successful: There are currently %d entries in the database,",add_count); /* phone_found(globe), count phone no. found(within FindPhone function */ printf("found %d.",phone_found); getch(); } if (phone check == -1) /* return = -1 Phone not found */ { gotoxy(1,25); cprint("Error: The Phone No.Your looking for was Not Found."); getch(); } } else if (option == '4') /* FindRoom Option */ { room_found = 0; /* initialize room no. found to zero */ clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Find Phone Number <<"); gotoxy(1,6); printf("Enter the room number to search for: "); scanf("%d",&iroom_search); flushall(); room_check = FindRoom(iroom_search); if (room_check == 0) /* return = o Room found */ { gotoxy(1,25); cprintf("Successful: There are currently %d entries in the database,",add_count); /* room_found is globe it counts room no. found in FindRoom function */ cprintf("found %d.",room_found); getch(); } if (room_check == -1) /* return = -1 Room was not found */ { gotoxy(1,25); cprintf("Error: The Room No. You Are Looking For Was Not Found."); getch(); } } else if (option == '5') /* ListAll option */ { clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> ListAll <<\n\n"); list_check = ListAll(); if (list_check == 0)/* return 0 if entries are in database */ { gotoxy(1,25); cprint("List Sucuessful); getch(); } if (list_check == -1) /* return -1 -emptylist */ { gotoxy(1,25); cprintf("Empty List"); getch(); } } else if (option == '6') /* Getotalentries option */ { total_entries = GeTotalEntries(); gotoxy(1,25); cprintf("There are currently %d entries stored in the Database.",total_entries); getch(); } else if (option == '7') { clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Sort All Entries <<"); gotoxy(1,6); printf("Press 'A' to sort database in [A]scending order"); gotoxy(1,7); printf("Press 'D' to sort database in [D]escendind order."); gotoxy(1,9); printf("Note: Database is sorted by phone no. entries."); sortopt = getch(); flushall(); sort_check = SortAllEntries(sortopt); getch(); if (sort_check == 0) /*return = 0 - entries, in db &was sorted */ { gotoxy(1,25); cprintf("Database was successfully Sorted."); getch(); } if (sort_check == -1) /*return = -1 - entries, in db &was sorted */ { gotoxy(1,25); cprintf("Database was not Sorted - Database is empty!"); getch(); } } else if (option == '8') /* Load Database from file option */ { clrscr(); refreshscreen(); drawscreeen(); gotoxy(1,4); printf(">> Load Database <<"); LoadDB(); } else if (option == '9') /*exit option */ { gotoxy(1,25); cprintf("Do you really want to exit?, Press 'Y' to confirm, anykey to cancel"); exit_opt = getch(); flushall(); if (exit_opt == 'y' || exit_opt == 'Y') { clrscr(); refreshscreen(); drawscreeen(); printf(">> Exit To system <<\n\n"); exitmenu(); } } else /* user presses an invalid key display msg error */ { gotosy(1,25); cprintf("Error: Invalid option! Select an option between 1 and 9"); getch(); flushall(); /* clear buffer */ } } while (option > '9' || option < '1' ); } while (option != '`'); /* unlimited loop */ } |
Add Entry Function:
Does not return any value it is used to add valid inputs(only) into the database and display the entry which was added. * A valid inputs are positive number only.
- Room no. input with less than or equal to 4 digits only.
- Phone no. input with less than or equal to 8 digits only.
- Input of Zero for room no. or phone no. inputs are invalid.
1 2 3 4 5 6 7 8 9 10 11 |
/*--------------------------------------------------------- AddEntry Function -----------------------------------------------------------*/ void AddEntry(int r, long int p) { room[i] = r; /* store r(room) input into db */ phone[i] = p; /* store r(room) input into db */ add_count++; /* keep track of total entries added. */ printf("\nRoom No. [%-4d]\nPhone No. [%-8ld]\n%d entries added.",r,p,current_e_add); getch(); } |
Delete Entry Function:
- Used to delete entries in the database.
- Return 0 if room no. & phone no. was found in the database.
- Return -1 if room no. & phone no. is not found in the database.
- Note: Auto-Recovery was implemented into this function but was never used.
- room_tmp array contains the deleted data which may be used for recovery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/*----------------------------------------------------------- DeleteEntry function --------------------------------------------------------------*/ int DeleteEntry(int r, long int p) { int k,x,del_found_flag=-1,loop_mov_stop,loop_mov,count_del=0; char del_me; /* Variable to conform delete */ for(k=0; k < add_count; k++) { if (add_count != 0) /* check if database is not empty */ { if (r == room[k] && p == phone[k]) { gotoxy(1,8); printf("Match Found: \n"); printf("Room No. [%-4d]\tPhone No. [%-8ld] was found in record No. [%3d ]\n",rooom[k],phone[k],k+1); del_found_flag = 0; /* when found, set's del_found_flag=0 */ gotoxy(1,25); cprint("Delete record [%3d ]?, Press 'Y' to confirm, any key to cancel.",k+1); del_me = getch(); flushall(); if (del_me == 'y' || del_me == 'Y') { room_tmp[tot_del_entry] === room[k]; /* tmp array storage for room found */ rooom[k] = -1; /* marks -1 for delete */ phone_tmp[tot_del_entry] = phone[k]; phone[k] = -1; del_entry++; /* counter for deleted entry */ tot_del_entry++; /* counter for temp storage */ } } } } if (add_counter !=0) /* if database is not empty process with delete */ {/* keeps looping while move up position is not = to delete entry */ for (x=0; x < del_entry; x++) { for (k=0; k < add_counter; k++) { /* When - 1 is found it moves everything by one */ if (room[k] == -1 && phone[k] == -1) { loop_mov_stop=0; loop_mov =0; count_del++; /* loop_mov_stop calculate mooves needed */ loop_mov_stop = add_count-(k+1); while (loop_mov_stop != loop_mov) { room[k+loop_mov] = room[(k+1)+loop_mov]; phone[k+loop_mov] = phone[(k+1)+loop_mov]; loop_mov++; /* counter for move */ } } } } } /* Calculate total entry */ add_count = add_count - del_entry; if (del_found_flag == o) /* flag is 0 when delete entry input was found */ { return(0); }/* return successful */ else { return(-1); }/* return not found */ } |
FindPhone function:
- Used to search for a phone number in the database.
- Return 0 if the phone no. was found.
- Return -1 if the phone no. is not found.
The FindRroom and FindPhone function simply matches the input parameter to the function with the room and phone array and prints out the values if the match is found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* ------------------------------------------------------------------------- FindPhone function --------------------------------------------------------------------------*/ int FindPhone(long int p) { int k, phone_found_flag= -1; gotoxy(1,8); for(k=0; k < add_count; k++) { if (add_count != 0) /* if database is not empty then run a search */ { if (k != 0 && (k%115) == 0) { gotoxy(1,8); /* moves cursor to beginning when screen is filled */ getch(); } if (p == phone[k]) { printf("Phone No. [%-8ld] was found in record No. [%3d ]\tRoom No. [%-4d]\n",phone[k],k+1,room[k]); phone_found++; phone_foundd_flagn = 0; } } } if (phone_found_flag == 0) /* flag is 0 if record was found */ { return(0); } /* return successful */ else { return(-1); } /* return not found */ } |
FindRoom function:
- Used to search for a Room number in the database.
- Return 0 if room no. was found.
- Return -1 if room no. is not found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/*----------------------------------------------------------------------- FindRoom function -----------------------------------------------------------------------*/ int FindRoom(int r) { int k, room_found_flag=-1; gotoxy(1,8); for(k=0; k < add_count; k++) { if (add_count !=0) /* if database is not empty then run search */ { if (k != 0 && (k%15) == 0) { gotoxy(1,8); /* move cursor to beginning when screen filled */ getch(); } if (r == room[]) { printf("Room No. (%-4d) was found in record No. [%3d ]\tPhone NO. (%-8ld)\n",room[k],k+1,phone[k]); room_found++; room_found_flag = 0; } } } if (room_found_flag == o) { return(0); } /* return successful */ else { return (-1); } /* return not found */ } |
ListAll Function:
The ListAll function iterates through the room and phone array and displays the phone book entries to the user.
- Used for displaying data entered into the database.
- return -1 if the list is empty.
- return 0 if successful (database contains valid entries)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/*---------------------------------------------------------------------- ListAll Function -----------------------------------------------------------------------*/ int ListAll (void) { int k; gotoxy(1,6); for (k=0; k < add_count; k++) { if (k != 0 && (k%17) == 0) { gotoxy(1,6); /* moves cursor to beginning when screen filled */ getch(); } /* double checks - it will not print out delete entries(-1) */ if (room[k] != -1 && phone[k] != -1) { printf("Room Number [%3d ]: %-4d\t",k+1,room[k]); printf("Phone Number[%3d ]: %-8ld\n",k+1,phone[k]); } } if (add_count == 0) { return(-1); } /* Empty List */ else { return(0); } /* successful */ } |
GeTotalEntries function:Â
Used to return total entries added to the database.
1 2 3 4 5 6 7 8 |
/*---------------------------------------------------------------------- GeTotalEntries function ----------------------------------------------------------------------*/ int GeTotalEntries(void) { /* This function is not required as it does nothing but return the total entries that have been added. */ return(add_count); } |
SortAllEntries function:
The SortAllEntries uses the bubble sort method for sorting the phone book entries. The bubble sorting method used a temporary variable to swap the values. Sorting is done both in the ascending as well as in descending order. For the ascending order, each time the previous value is greater than the next value, the two values are swapped. For the ascending order, the same principle is used, only when the previous value is less than the next then the value is swapped. This entire process happens within a loop.
- Sort is done with the use of double sort.
- return 0 if the sort was successful.
- return -1 if the database is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* -------------------------------------------------------------------- SortAllEntries function ---------------------------------------------------------------------*/ int SortAllEntries(char sel) { int k,room_str_tmp,sortalldone; long int phone_str_tmp; if (add_count !=0) /* if list not empty continue with sort */ { do { sortalldone=0; for (k = 0; k < add_count; k++) { /* sort in ascending order */ if ((phone[k] > phone[k + 1])&&(sel == 'a' || sel == 'A')&&(k != add_count -1)) { phone_str_tmp = phone[k]; /* store previous array to phone_str_tmp */ phone[k] = phone[k + 1]; /* copy next array to the previous array before it */ phone[k + 1] = phone_str_tmp; /* Previous array is copied to next array */ /* same process is done here but with room no. */ room_str_tmp = room[k]; room[k] = room[k + 1]; room[k + 1] = room_str_tmp; sortalldone =1; /* sets to 1 if sort is done */ } /* same method used here but sorts in descending order */ if ((phone[k] < phone[k + 1])&&(sel == 'd' || sel == 'D')&&(k != add_count -1)) { phone_str_tmp = phone[k]; phone[k] = phone[k + 1]; phone[k + 1] = phone_str_tmp; room_str_tmp = room[k]; room[k] = room[k + 1]; room[k + 1] room_str_tmp; sortalldone =1; } } } while (sortalldone); } if ((sel == 'a' || sel == 'A')&&add_count !=0) { gotoxy(1,25); printf("You have chosen to sort the database in [A]scending order. "); return(0); /* successfully sorted */ } else if ((sel == 'd' || sel == 'D')&&add_count !=0) { gotoxy(1,25); printf("You have chosen to sort the database in [D]ecending order. "); return(0); /* successfully sorted */ } else if ((sel != 'a' || sel != 'A' || sel != 'd' || sel != 'D')&&add_count !=0) { gotoxy(1,12); printf("Invalid option - database was not sorted!"); } else { return(-1); } /* list empty */ } |
Chkstrdig(Check string for digits):
Used to check string inputs, and length
returning:
- -1 = if the string length is out of range.
- -2 = if a char is found within the string.
- 0 = if successful
NOTE: AddEntry specifies that the Room allows for 4 digits or less.
phone allowing 8 digits or less.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/*--------------------------------------------------------------- Chkstrdig(Check string for digits) ---------------------------------------------------------------*/ int chkstrdig (chr str[], int range) { int length=0,k; length = strlen(str); /* get length of string */ if (length > range) { return(-1); /* out of range */ if (length <= range) /* string is in range */ { for (k=0; k < length; k++) { if (isdigit(str[k]) == 0) { return(-2); }/* char detected return error msg */ } return(0); /* successful - input string was in range and was digits */ } } } |
LoadDB Function:
The LoadDB function loads the phone book entries from a flat-file. The file is opened on line 647 using fopen and the data is loaded into the room and phone arrays (656 – 675).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/*------------------------------------------------------------------- Load up database from file function Used to load a file into the database. -------------------------------------------------------------------*/ void LoadDB(void)*1p2pcarni# { int count,nofilen,dbfilecount=0; char finphone[80]; char finroom[80]; int error_junk; long int l_finphone; int i_finroom; FILE *f1; gotoxy(1,6); printf("Enter the filename of the database: "); gotoxy(1,7); printf("Example: c:\\mydbfile.txt"); gotoxy(37,6); gets(dbload); flushall(); /* clears all buffers */ f1 = fopen (dbload,"r"); /* open file for reading */ if(f1==NULL) { gotoxy(1,25); fprintf(stderr,"There was an error reading your database file!"); strcpy(dbload, "No database file loaded (RAM MODE!)."); getch(); exit(1); } else { for (count=0; count < MAXDB; count++) { if (!feof(f1)) /* if not end of file continue to input data */ { fscan(f1,"%s\t%s\n",&finroom,&finphone); /* Save info to room, phone array */ error_junk = chkstrdig(finroom,4); error_junk = chkstrdig(finphone,8); if (error_junk == -1 || error_junk == -2) { printf("Sorry that was an invalid database\n"); printf("Now working in RAM MODE!"); getch(); strcpy(dbload, "No database file loaded (RAM MODE!)."); break; } i_finroom = atoi(finroom); /* converts string to int */ l_finphone = atoi(finphone); /* Converts string to long int */ room[count] = i_finroom; phone[count] = l_finphone; dbfilecount++; /* count no. of records stored in file */ } } if (error_junk ==0) { gotoxy(1,25); printf("Database %s, was successfully loaded!",dbload); getch(); /* copy no. of records in file into master counter */ add_count = dbfilecount; } } fclose(f1); } |
Menu function:
These functions are used to draw the menu and the exit message on the screen. The ExitMenu function performs the task of saving the data to a flat-file before closing the application. The Menu function displays valid options on the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/************************************************ Menu function ------------------------------------------------*/ char menu(void) { char optrtn; clrscr(); window(1,1,80,25); /* Set position and screen mode */ refreshscreen(); drawscreen(); gotoxy(1,4); printf("[1] - Add entry\n"); printf("[2] - Delete entry\n"); printf("[3] - Find room number\n"); printf("[4] - Find phone number\n"); printf("[5] - List all entries\n"); printf("[6] - Display total entries in database\n"); printf("[7] - Sort entries\n"); printf("[8] - Load database from file\n"); printf("[9] - Exit\n"); gotoxy(1,25); cprintf("Please select an option between 1 and 9."); gotoxy(1,15); printf("Database loaded: %s",dbload); gotoxy(1,14); printf("Select an option: "); optrtn = getch(); return optrtn; } |
ExitMenu function:
While exiting the system, asked the user if he/she wants to save the database into a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/* -------------------------------------------------------- ExitMenu function -------------------------------------------------------------*/ void exitmenu(void) { char filename[20],save_opt; int k; FILE *f1; gotoxy(1,6); printf("Do you want to save database before exiting? "); gotoxy(1,25); cprint("Press 'Y' to confirm, anykey to cancel."); save_opt = getch(); flushall(); if (save_opt =='y' || save_opt == 'Y') { gotoxy(1,8); printf("Please enter the path and filename to save to:"); gotoxy(1,10); printf("Example: c:\\mydbfile.txt"); gotoxy(48,8); /* move cursor back to line 8 */ gets(filename); flushall(); f1 = fopen (filename,"a"); /* open file for appending mode */ if (f1 ==NULL) { gotoxy(1,12); fprintf(stderr, "Error opening file %s. ",filename); gotoxy(1,25); cprintf("Database was not saved!"); getch(); exit; } else { for (k=0; k < add_count; k++) { fprintf(f1, "%d\t%ld\n",room[k],phone[k]);} fclose(f1); gotoxy(1,25); cprintf("Database was successfully saved in %s",filename); getch(); } } else { gotoxy(1,25); cprintf("Database was not saved!"); getch(); } clrscr(); gotoxy(23,10); printf("Thank you for using this program"); gotoxy(23,11); printf("Coded by: Krishna Keshav Chaudhary"); gotoxy(23,13); printf("Website: www.bestengineerigprojects.com"); getch(); exit(0); } |
Drawscreen function: Draws program header.
1 2 3 4 5 6 7 8 9 10 11 12 |
/*---------------------------------------------------------------- Drawscreen function -----------------------------------------------------------------*/ void drawscreen(void) { gotoxy(1,1) cprintf("-------------------------------------------------------------------"); gotoxy(1,2); cprintf(" *** PHONE BOOK *** "); gotoxy(1,3); cprintf("-------------------------------------------------------------------"); } |
Rfreshscreen function: used to refresh the color display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/*--------------------------------------------------------------------- Rfreshscreen function ---------------------- used to refresh color display. ----------------------------------------------------------------------*/ void refreshscreen(void) { clrscr(); textcolor(WHITE); textbackground(BLACK); gotoxy(1,25); cprintf(""); clrscr(); textcolor(WHITE); textbackground(BLUE); gotoxy(1,25); cprintf(""); } |