Advertisement
Guest User

Summer

a guest
Jun 16th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*The program below demonstrate a simple hospital management system that
  2.   allows user,doctor and admin to monitor it and perform features such
  3.             as adding,editing and editing patient's details*/
  4.  
  5. //  Project Title:     HOSPITAL MANAGEMENT SYSTEM
  6. //  Module Code:       AAPP005-4-2-PSPD
  7. //  Module Title:      Problem Solving & Program Designing using C
  8. //
  9. //  Name:              KHO ZHI YUEN
  10. //  TP Number:         TP038123
  11. //  Intake Code:       UCDF1505ICT(SE)
  12.  
  13.  
  14. /* ------------------------ Include Headers ------------------------*/
  15. #define _CRT_SECURE_NO_WARNINGS
  16. #include <stdio.h>      // Include the Standard Input/Output Header
  17. #include <stdlib.h>     // Include the Standard Library Header
  18. #include <unistd.h>     // Include the POSIX Standard Header
  19. #include <string.h>     // Include the Strings Header
  20. #include <ctype.h>      // Include the Header to Handle Characters
  21. #include <time.h>       // Include the Header to Manipulate Date and Time
  22. #include <stdbool.h>    // Include the Header for Boolean
  23. #include <windows.h>    // Include the Header for graphics
  24. #include <tchar.h>
  25.  
  26. //ASCII CODE
  27. #define ENTER 13
  28. #define BKSP 8
  29. #define ESC 27
  30.  
  31. // ------------------------ Structures ------------------------//
  32. struct patient{
  33.     char pid[20];
  34.     char lname[50];
  35.     char fname[50];
  36.     char fullname[100];
  37.     char gender[10];
  38.     char age[3];
  39.     char address[30];
  40.     char contact[20];
  41. }patientdets,patienttemp; //patientdets hold patient data, patienttemp to hold temp data during transfer
  42.  
  43. struct app{
  44.     char pid[20];
  45.     char date[25];
  46.     char time[15];
  47.     char location[40];
  48.     char doctor[30];
  49.     char apptype[40];
  50. }appdets;               //appdets hold main appointment data
  51.  
  52. struct user{
  53.     char u_name[25];
  54.     char u_pass[25];
  55. }u;
  56.  
  57. int choice;
  58. char *token;            //pointer for token to split up strings
  59. char *tokenA;           //pointer for token to split up strings in appointment
  60.  
  61. // ------------------------ Functions ------------------------//
  62. //MAIN MENU
  63. int receptionist();
  64. int doctor();
  65. int user();
  66. char *dataEntry(int type, int sizelimit);
  67. int main_menu();
  68. //SUB MENUS
  69. void receptionist_menu();   //Receptionist Menu
  70. void doctor_menu();         //Doctor Menu
  71. void receptionist_pat_menu(); //Receptionist Patient Menu
  72. void receptionist_app_menu(); //Receptionist Appointment Menu
  73. void doctor_pat_menu();       //Doctor Patient Menu
  74. void doctor_app_menu();       //Doctor Appointment Menu
  75. int patMenu();          //Patient's Menu
  76. int appMenu();          //Appointment's Menu
  77. //PATIENT MENU
  78. void addPat();          //Add Patient
  79. void viewPat();         //View Patient
  80. void searchPat();       //Search Patient
  81. void editPat();         //Edit Patient
  82. void deletePat();       //Delete Patient
  83. //APPOINTMENT MENU
  84. int addApp();           //Add Appointment
  85. void viewApp();         //View Appointment
  86. int searchApp();       //Search Appointment
  87. void editApp();       //Edit Appointment
  88. void deleteApp();       //Delete Appointment
  89. //SEARCH MENU
  90. int searchMAIN();       //Main coding for search functions
  91. void searchPID();       //Search via Patient's ID
  92. void searchNAME();      //Search via Patient's Name
  93. //FEEDBACK FORM
  94. void feedBack(){};     //To retrieve feedback from users
  95. //LOGIN FUNCTIONS
  96.  
  97. int  loginval();
  98. //VALIDATION FUNCTIONS
  99. int  escExit(char askey);
  100. int  backMenu(char askey);
  101. int  isalphaVal(char input[]);
  102. int  isdigitVal(char input[]);
  103. char *dataEntry(int type, int sizeLimit);
  104. //AUTO GENERATE PID FUNCTIONS
  105. int getLine();          //Get number of lines
  106. int getPID();           //Get PID
  107. //GOTOXY FUNCTION
  108. int usertype;
  109.  
  110. void delay();
  111.  
  112. void gotoxy(int x, int y);
  113. COORD coord = {0,0};
  114. void gotoxy(int x, int y)
  115. {
  116.     coord.X = x;
  117.     coord.Y = y;
  118.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  119. }
  120. // ------------------------ Validations ------------------------//
  121. int escExit(char askey)
  122. {
  123.     char selection='Y';
  124.     if(askey==ESC)
  125.     {
  126.         do
  127.         {
  128.             if(!(toupper(selection)=='Y' || toupper(selection)=='N'))
  129.             {
  130.                 printf("\aINVALID INPUT!\n");
  131.             }
  132.             printf("\aDo you want to exit?\n");
  133.             selection = getch();
  134.         } while(!(toupper(selection)=='Y' || toupper(selection)=='N'));
  135.  
  136.             if(toupper(selection)=='Y')
  137.             {
  138.                 system("cls");
  139.                 exit(0);
  140.             }
  141.             else if(toupper(selection)=='N')
  142.             {
  143.                 system("cls");
  144.                 return 1;
  145.             }
  146.     }
  147. }
  148.  
  149. void delay(unsigned int mseconds)
  150. {
  151.     clock_t goal = mseconds + clock();
  152.     while (goal > clock());
  153. }
  154.  
  155. int backMenu(char askey)
  156. {
  157.     int result;
  158.  
  159.     if(askey==BKSP)
  160.     {
  161.         result = 1;
  162.     }
  163.     return result;
  164. }
  165.  
  166. char *data_entry(int type, int sizeLimit)
  167. {
  168.     char input[30], p=' ';
  169.     int i=0;
  170.  
  171.     while(i<=sizeLimit)
  172.     {
  173.         input[i] = _getch();
  174.         p = input[i];
  175.  
  176.         if(i==sizeLimit)
  177.         {
  178.             if(p==ENTER)
  179.                 break;
  180.             else if((p>=48 && p<=57)||(p>=65 && p<=90)||(p>=97 && p<=122))
  181.                 {
  182.                 i--;
  183.                 printf("\b \b");
  184.                 }
  185.         }
  186.  
  187.         if(p!=13 && p!='\b' && !((p>=48 && p<=57)||(p>=65 && p<=90)||(p>=97 && p<=122)))
  188.         {
  189.             p='\0';
  190.         }
  191.  
  192.         else if(p==ENTER)
  193.         {
  194.             p='\0';
  195.             break;
  196.         }
  197.  
  198.         else if(p==BKSP)
  199.         {
  200.             i--;
  201.             if(i!=-1)
  202.             {
  203.                 p='\0';
  204.                 printf("\b \b");
  205.             }
  206.             else
  207.             {
  208.                 p='\0';
  209.                 i++;
  210.             }
  211.         }
  212.  
  213.         else
  214.         {
  215.             if(type==1)
  216.                 printf("%c", input[i]);
  217.             else if(type==2)
  218.                 printf("*");
  219.             else
  220.                 printf("*");
  221.             i++ ;
  222.         }
  223.     }
  224.     input[i] = '\0' ;
  225.     return input;
  226. }
  227.  
  228. int isalphaVal(char input[])
  229. {
  230.     int val,i;
  231.  
  232.     for(i=0;i<(strlen(input));i++)
  233.     {
  234.         if(isalpha(input[i])||(isspace(input[i])))
  235.             val=1;
  236.         else
  237.         {
  238.             val=0;
  239.             break;
  240.         }
  241.     }
  242.  
  243.     return val;
  244. }
  245.  
  246. int isdigitVal(char input[])
  247. {
  248.     int val, i;
  249.  
  250.     for(i=0;i<(strlen(input));i++)
  251.     {
  252.         if(input[i]>=48 && input[i]<=57)
  253.             val=1;
  254.         else
  255.         {
  256.             val=0;
  257.             break;
  258.         }
  259.     }
  260.     return val;
  261. }
  262.  
  263. void main()
  264. {
  265. int color = 10;
  266. SYSTEMTIME time;
  267.  
  268.     HANDLE hConsole;
  269.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  270.     while(!kbhit())
  271.     {
  272.         system("cls");
  273.  
  274.         if(color==14)
  275.             color = 10;
  276.         SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE) , color);
  277.         gotoxy(25, 9);
  278.         printf("WELCOME TO SUMHEALTH");
  279.         gotoxy(25, 11);
  280.         printf("SUMMER KHO ZHI YUEN");
  281.  
  282.         while (1)
  283.         {
  284.         GetLocalTime(&time);
  285.         gotoxy(25, 12);
  286.         wprintf(L"The Local Time: %02d:%02d:%02d", time.wHour, time.wMinute, time.wSecond);
  287.         Sleep(2500);
  288.         system("cls");
  289.         fflush(stdin);
  290.         main_menu();
  291.         }
  292.         color++;
  293.  
  294.  
  295.     }
  296.  
  297.     system("COLOR 0B");
  298.     gotoxy(25, 13);
  299.     system("pause");
  300. }
  301.  
  302. int main_menu()
  303. {
  304.     int a, b;
  305.     while(1)
  306.     {
  307.     do
  308.     {
  309.         system("cls");
  310.         system("color F0");
  311.         gotoxy(21,7);
  312.         printf("***************Welcome to SUMHEALTH**************");
  313.         gotoxy(33, 11);
  314.         printf("1. PRESS 1 FOR DOCTOR\n");
  315.         gotoxy(33, 12);
  316.         printf("2. PRESS 2 FOR RECEPTIONIST\n");
  317.         gotoxy(33, 13);
  318.         printf("3. PRESS 3 FOR USER");
  319.         gotoxy(21, 17);
  320.         printf("***************************************************");
  321.         gotoxy(33, 14);
  322.         printf("Enter your choice:");
  323.         scanf("%d", &usertype);
  324.         switch (usertype)
  325.         {
  326.             case 1: a = doctor();
  327.                     if (a != 0)
  328.                     doctor_menu();
  329.                     break;
  330.             case 2: b = receptionist();
  331.                     if (b != 0)
  332.                     receptionist_menu();
  333.                     break;
  334.             case 3: user();
  335.                     break;
  336.         }
  337.     }
  338.     while (usertype <1  ||  usertype > 3);
  339. }
  340. }
  341.  
  342.  
  343.  
  344. int login_validation()
  345. {
  346.     FILE *fpass;
  347.  
  348.     char input_name[30], input_pass[30], action;
  349.     int login = 0, fail = 0;
  350.  
  351.     do{
  352.  
  353.         if(usertype == 1)
  354.             fpass = fopen("password1.txt", "r+");
  355.         else if(usertype == 2)
  356.             fpass = fopen("password2.txt", "r+");
  357.  
  358.             rewind(fpass);
  359.  
  360.         if(fpass != NULL)
  361.         {
  362.             system("cls");
  363.  
  364.         if(fail ==1)
  365.         {
  366.             gotoxy(25, 13);
  367.             printf("Error!");
  368.             gotoxy(25, 14);
  369.             printf("Please Enter Again!");
  370.         }
  371.  
  372.         system("color F1");
  373.         gotoxy(25, 8);
  374.         printf("************* WELCOME!****************");
  375.         int checker=0;
  376.         gotoxy(25, 12);
  377.         printf("Username: ");
  378.         fflush(stdin);
  379.         gets(input_name);
  380.         gotoxy(25, 13);
  381.         printf("Password: ");
  382.         strcpy(input_pass, data_entry(2, sizeof(input_name)));
  383.  
  384.             fscanf(fpass, "%s %s",u.u_name,u.u_pass);
  385.  
  386.             printf("%s",u.u_name);
  387.             printf("%s",u.u_pass);
  388.             fclose(fpass);
  389.             if((strcmp(u.u_name, input_name)==0) && (strcmp(u.u_pass, input_pass)==0))
  390.             {
  391.                 checker=1;
  392.             }
  393.  
  394.  
  395.         if(checker==1)
  396.             {
  397.                 printf("\a\n\nLogin Successful!");
  398.                 login=1;
  399.             }
  400.         else
  401.             fail=1;
  402.  
  403.  
  404.         }
  405.  
  406.         }while(login==0);
  407.         fclose(fpass);
  408.  
  409.         return 1;
  410.  
  411. }
  412. int doctor()
  413. {
  414.     int a = 0;
  415.     do
  416.     {
  417.  
  418.         system("cls");
  419.         fflush(stdin);
  420.         fflush(stdin);
  421.         a=login_validation();
  422.     }
  423.     while (a == 0);
  424.     return a;
  425. }
  426.  
  427. int receptionist()
  428. {
  429.     int a = 0;
  430.     do
  431.     {
  432.         system("cls");
  433.         fflush(stdin);
  434.         fflush(stdin);
  435.         a=login_validation();
  436.     }
  437.     while (a == 0);
  438.     return a;
  439. }
  440.  
  441. void doctor_menu()
  442. {
  443.     do
  444.     {
  445.         while(1)
  446.         {
  447.             system("cls");
  448.             system("color F5");
  449.             gotoxy(25, 8);
  450.             printf("----------DOCTOR MENU----------");
  451.             gotoxy(25, 10);
  452.             printf("1. Patient\n");
  453.             gotoxy(25, 11);
  454.             printf("2. Appointment\n");
  455.             gotoxy(25, 12);
  456.             printf("3. Logout");
  457.             gotoxy(25, 13);
  458.             printf("4. Exit");
  459.             gotoxy(25, 15);
  460.             printf("Enter your choice:");
  461.             scanf("%d", &choice);
  462.             switch (choice)
  463.             {
  464.                 case 1: doctor_pat_menu();
  465.                         break;
  466.                 case 2: doctor_app_menu();
  467.                         break;
  468.                 case 3: return 1;
  469.                 case 4: exit(0);
  470.  
  471.             }
  472.         }
  473.     }
  474.     while (choice <1  ||  choice > 4);
  475. }
  476.  
  477. int user()
  478. {
  479.     char doc_name[30];
  480.  
  481.     system("cls");
  482.     gotoxy(25, 11);
  483.     printf("Hello!");
  484.     gotoxy(25, 12);
  485.     printf("Please Enter Your Doctor Name:");
  486.     fflush(stdin);
  487.     gets(doc_name);
  488.     gotoxy(25, 14);
  489.     printf("1. Excellent");
  490.     gotoxy(25, 15);
  491.     printf("2. Good");
  492.     gotoxy(25, 16);
  493.     printf("3. Average");
  494.     gotoxy(25, 17);
  495.     printf("4. Acceptable");
  496.     gotoxy(25, 18);
  497.     printf("5. Bad");
  498.     gotoxy(25, 20);
  499.     printf("Please Enter Your Choice:");
  500.     scanf("%d", &choice);
  501.     switch(choice)
  502.     {
  503.         case 1: system("cls");
  504.                 gotoxy(25, 11);
  505.                 printf("Wow thanks! See You Again!");
  506.                 break;
  507.         case 2: system("cls");
  508.                 gotoxy(25, 11);
  509.                 printf("Thank You :)!");
  510.                 break;
  511.         case 3: system("cls");
  512.                 gotoxy(25, 11);
  513.                 printf("We will try to improve it!");
  514.                 break;
  515.         case 4: system("cls");
  516.                 gotoxy(25, 11);
  517.                 printf("We have try to provide the best next time you visit!");
  518.                 break;
  519.         case 5: system("cls");
  520.                 gotoxy(25, 11);
  521.                 printf("Sorry about that, do tell us via 012-468 6684!");
  522.                 break;
  523.     }
  524.     gotoxy(25, 13);
  525.     system("pause");
  526.     return;
  527. }
  528.  
  529. void receptionist_menu()
  530. {
  531.     do
  532.     {
  533.         while(1)
  534.         {
  535.             system("cls");
  536.             system("color F8");
  537.             gotoxy(25, 8);
  538.             printf("----------RECEPTIONIST MENU----------");
  539.             gotoxy(25, 10);
  540.             printf("1. Patient\n");
  541.             gotoxy(25, 11);
  542.             printf("2. Appointment\n");
  543.             gotoxy(25, 12);
  544.             printf("3. Logout");
  545.             gotoxy(25, 13);
  546.             printf("4. Exit");
  547.             gotoxy(25, 14);
  548.             printf("Enter your choice:");
  549.             scanf("%d", &choice);
  550.             switch (choice)
  551.             {
  552.                 case 1: receptionist_pat_menu();
  553.                         break;
  554.                 case 2: receptionist_app_menu();
  555.                         break;
  556.                 case 3: return;
  557.                 case 4: exit(0);
  558.             }
  559.         }
  560.     }
  561.     while (choice <1  ||  choice > 2);
  562. }
  563. void doctor_pat_menu()
  564. {
  565.     do
  566.     {
  567.         while(1)
  568.         {
  569.             system("cls");
  570.             system("color FA");
  571.             gotoxy(25, 10);
  572.             printf("1. Search Patient\n");
  573.             gotoxy(25, 11);
  574.             printf("2. View Patient\n");
  575.             gotoxy(25, 12);
  576.             printf("3. Back\n");
  577.             gotoxy(25, 14);
  578.             printf("Enter your choice:");
  579.             scanf("%d", &choice);
  580.             switch (choice)
  581.             {
  582.                 case 1: searchPat();
  583.                         break;
  584.                 case 2: viewPat();
  585.                         break;
  586.                 case 3: return;
  587.             }
  588.         }
  589.     }
  590. while (choice <1  ||  choice > 2);
  591. }
  592.  
  593. void doctor_app_menu()
  594. {
  595.     do
  596.     {
  597.         while(1)
  598.         {
  599.             system("cls");
  600.             system("color FD");
  601.             gotoxy(25, 10);
  602.             printf("1. Search Appointment\n");
  603.             gotoxy(25, 11);
  604.             printf("2. View Appointment\n");
  605.             gotoxy(25, 12);
  606.             printf("3. Back\n");
  607.             gotoxy(25, 14);
  608.             printf("Enter your choice:");
  609.             scanf("%d", &choice);
  610.             switch (choice)
  611.             {
  612.                 case 1: searchApp();
  613.                         break;
  614.                 case 2: viewApp();
  615.                         break;
  616.                 case 3: return;
  617.             }
  618.         }
  619.     }
  620.     while (choice <1  ||  choice > 2);
  621. }
  622.  
  623. void receptionist_pat_menu()
  624. {
  625.     do
  626.     {
  627.         while(1)
  628.         {
  629.             system("cls");
  630.             system("color F0");
  631.             gotoxy(25, 8);
  632.             printf("1. Add New Patient\n");
  633.             gotoxy(25, 9);
  634.             printf("2. View Patient\n");
  635.             gotoxy(25, 10);
  636.             printf("3. Search Patient\n");
  637.             gotoxy(25, 11);
  638.             printf("4. Delete Patient\n");
  639.             gotoxy(25, 12);
  640.             printf("5. Modify Patient\n");
  641.             gotoxy(25, 13);
  642.             printf("6. Back\n");
  643.             gotoxy(25, 15);
  644.             printf("Enter your choice:");
  645.             fflush(stdin);
  646.             scanf("%d", &choice);
  647.             switch (choice)
  648.             {
  649.                 case 1: addPat();
  650.                         break;
  651.                 case 2: viewPat();
  652.                         break;
  653.                 case 3: searchPat();
  654.                         break;
  655.                 case 4: deletePat();
  656.                         break;
  657.                 case 5: editPat();
  658.                         break;
  659.                 case 6: return;
  660.             }
  661.  
  662.         }
  663.     }
  664.     while ((choice <1  ||  choice > 5));
  665. }
  666.  
  667. void receptionist_app_menu()
  668. {
  669.     do
  670.     {
  671.         while(1)
  672.         {
  673.             system("cls");
  674.             system("color FC");
  675.             gotoxy(25, 8);
  676.             printf("1. Add New Appointment\n");
  677.             gotoxy(25, 9);
  678.             printf("2. View Appointment\n");
  679.             gotoxy(25, 10);
  680.             printf("3. Search Appointment\n");
  681.             gotoxy(25, 11);
  682.             printf("4. Delete Appointment\n");
  683.             gotoxy(25, 12);
  684.             printf("5. Modify Appointment\n");
  685.             gotoxy(25 ,13);
  686.             printf("6. Back\n");
  687.             gotoxy(25, 15);
  688.             printf("Enter your choice:");
  689.             fflush(stdin);
  690.             scanf("%d", &choice);
  691.             switch (choice)
  692.             {
  693.                 case 1: addApp();
  694.                         break;
  695.                 case 2: viewApp();
  696.                         break;
  697.                 case 3: searchApp();
  698.                         break;
  699.                 case 4: deleteApp();
  700.                         break;
  701.  
  702.                 case 5: editApp();
  703.                         break;
  704.                 case 6: return;
  705.             }
  706.             system("pause");
  707.         }
  708.     }
  709.     while ((choice <1 || choice > 6));
  710. }
  711.  
  712.  
  713. int getLine()
  714. {
  715.     int i=1;
  716.     char ch;
  717.  
  718.     FILE *fpatient;
  719.     fpatient=fopen("PATIENT.txt","r");
  720.  
  721.     if(fpatient!=NULL)
  722.     {
  723.         while(!feof(fpatient))
  724.         {
  725.             ch = fgetc(fpatient);
  726.             if (ch=='\n')
  727.                 i++;
  728.         }
  729.     }
  730.  
  731.     else
  732.     {
  733.         perror("ERROR\n");
  734.         return(-1);
  735.     }
  736.     fclose(fpatient);
  737.     return i;
  738. }
  739.  
  740. int getPID()
  741. {
  742.     int i = getLine();
  743.     sprintf(patientdets.pid,"TP%004d",i);
  744.     return i;
  745. }
  746.  
  747. char *getFirstName()
  748. {
  749.     int val=0;
  750.     static char first[20];
  751.     do
  752.     {
  753.         fflush(stdin);
  754.         printf("Enter First Name:\n");
  755.         gets(first);
  756.         val=isalphaVal(first);
  757.     }while(val==0);
  758.     return first;
  759. }
  760.  
  761. char *getLastName()
  762. {
  763.     int val=0;
  764.     static char last[20];
  765.     do
  766.     {
  767.         fflush(stdin);
  768.         printf("Enter Last Name:\n");
  769.         gets(last);
  770.         val=isalphaVal(last);
  771.     }while(val==0);
  772.     return last;
  773. }
  774.  
  775.  
  776. char *getFullName()
  777. {
  778.     static char full[50];
  779.     printf("Insert Full name:\n");
  780.     gets(full);
  781.     return full;
  782. }
  783. char *getGender()
  784. {
  785.     static char gender[5];
  786.     printf("Enter Gender:\n");
  787.     gets(gender);
  788.     return gender;
  789. }
  790.  
  791. char *getAge()
  792. {
  793.     int val=0;
  794.     static char age[3];
  795.     do
  796.     {
  797.         printf("Enter Age:\n");
  798.         gets(age);
  799.         val=isdigitVal(age);
  800.     }while(val==0);
  801.     return age;
  802. }
  803.  
  804. char *getAddress()
  805. {
  806.     static char address[30];
  807.     printf("Enter Address:\n");
  808.     gets(address);
  809.     return address;
  810. }
  811.  
  812. char *getContact()
  813. {
  814.     int val=0;
  815.     static char contact[20];
  816.     do
  817.     {
  818.         printf("Enter Contact:\n");
  819.         gets(contact);
  820.         val=isdigitVal(contact);
  821.     }while(val==0);
  822.     return contact;
  823. }
  824.  
  825. char *getDate()
  826. {
  827.     int val=0;
  828.     static char day[4];
  829.     static char month[4];
  830.     static char year[8];
  831.     static char date[25];
  832.     char slash[2]="/";
  833.     char sslash[2]="/";
  834.     printf("Enter App. Day:\n");
  835.     gets(day);
  836.     printf("Enter App. Month:\n");
  837.     gets(month);
  838.     printf("Enter App. Year:\n");
  839.     gets(year);
  840.  
  841.     strcpy(date,"");
  842.     strcat(date,day);
  843.     strcat(date,sslash);
  844.     strcat(date,month);
  845.     strcat(date,slash);
  846.     strcat(date,year);
  847.     return date;
  848. }
  849.  
  850. char *getTime()
  851. {
  852.     static char time[15];
  853.     static char hour[5];
  854.     static char mins[5];
  855.     static char type[3];
  856.     char dot[2]=":";
  857.  
  858.     printf("Enter App. Hour:\n");
  859.     gets(hour);
  860.     printf("Enter App. Minutes:\n");
  861.     gets(mins);
  862.     printf("(AM/PM):\n");
  863.     gets(type);
  864.  
  865.     strcpy(time,"");
  866.     strcat(time,hour);
  867.     strcat(time,dot);
  868.     strcat(time,mins);
  869.     strcat(time,type);
  870.     return time;
  871. }
  872.  
  873. char *getLocation()
  874. {
  875.     static char location[40];
  876.     printf("Enter App. Location:\n");
  877.     gets(location);
  878.     return location;
  879. }
  880.  
  881. char *getDoctor()
  882. {
  883.     static char doctor[30];
  884.     printf("Enter App. Doctor:\n");
  885.     gets(doctor);
  886.     return doctor;
  887. }
  888.  
  889. char *getAppType()
  890. {
  891.      int app;
  892.      char sp[]="Specialist";
  893.      char no[]="Normal";
  894.      char he[]="Health Test";
  895.      printf("Choose Appointment Type:\n(1)Specialist\t(2)Normal\t(3)Health Test\n");
  896.      scanf("%d",&app);
  897.      switch(app)
  898.      {
  899.          case 1:
  900.             {
  901.                 return(sp);
  902.             }
  903.          case 2:
  904.             {
  905.                 return(no);
  906.             }
  907.          case 3:
  908.             {
  909.                 return(he);
  910.             }
  911.          default:
  912.             {
  913.                 perror("PLEASE ENTER A VALID NUMBER!\n");
  914.                 return(-1);
  915.             }
  916.      }
  917.  
  918. }
  919.  
  920. void addPat()
  921. {
  922.     system("color FC");
  923.     int i = 0;
  924.  
  925.     int selection = 'N';
  926.  
  927.     FILE *fpatient;
  928.  
  929.     do
  930.     {
  931.         fpatient = fopen("PATIENT.txt", "a");
  932.  
  933.         if (fpatient==NULL)
  934.         {
  935.             perror("FILE DOES NOT EXISTS!\n");
  936.             return ;
  937.         }
  938.         else
  939.         {
  940.             char z[20] = " ";
  941.             system("cls");
  942.             i = getPID();
  943.             printf("Patient ID:%s\n",patientdets.pid);
  944.             fflush(stdin);
  945.             strcpy(patientdets.fname,getFirstName());
  946.             strcpy(patientdets.lname,getLastName());
  947.             strcat(z,patientdets.lname);
  948.             strcat(patientdets.fname,z);
  949.             strcpy(patientdets.fullname,patientdets.fname);
  950.             strcpy(patientdets.gender,getGender());
  951.             strcpy(patientdets.age,getAge());
  952.             strcpy(patientdets.address,getAddress());
  953.             strcpy(patientdets.contact,getContact());
  954.             fprintf(fpatient,"%s-->%s-->%s-->%s-->%s-->%s\n",patientdets.pid,patientdets.fullname,patientdets.gender,patientdets.age,patientdets.address,patientdets.contact);
  955.             printf("\nDo You Want To Add Another Patient's Details? (Y/N):");
  956.             fflush(stdin);
  957.             selection = getchar();
  958.             fclose(fpatient);
  959.         }
  960.  
  961.     }while(selection == 'Y' || selection == 'y');
  962.  
  963.     return ;
  964. }
  965.  
  966. void viewPat()
  967. {
  968.     system("cls");
  969.     char buffer[1024]=" ";
  970.     char s[4] = "-->";
  971.     char *token;
  972.     int counter = 1;
  973.     FILE *fpatient;
  974.  
  975.     fpatient = fopen("PATIENT.txt","r");
  976.  
  977.     if (fpatient == NULL)
  978.     {
  979.         printf("ERROR:FILE CANNOT BE OPENED!\n");
  980.     }
  981.     else
  982.     {
  983.         if (feof(fpatient))
  984.         {
  985.             printf("ERROR:END OF FILE!\n");
  986.         }
  987.         else
  988.         {
  989.             while( fgets(buffer,1024,fpatient) != NULL )
  990.             {
  991.                 token = strtok(buffer, s);
  992.                 strcpy(patientdets.pid,token);
  993.                 token = strtok(NULL,s);
  994.                 strcpy(patientdets.fullname,token);
  995.                 token = strtok(NULL,s);
  996.                 strcpy(patientdets.gender,token);
  997.                 token = strtok(NULL,s);
  998.                 strcpy(patientdets.age,token);
  999.                 token = strtok(NULL,s);
  1000.                 strcpy(patientdets.address,token);
  1001.                 token = strtok(NULL,s);
  1002.                 strcpy(patientdets.contact,token);
  1003.                 token = strtok(NULL,s);
  1004.  
  1005.                 printf("Patient's ID:       %s\n",patientdets.pid);
  1006.                 printf("Patient's name:     %s\n",patientdets.fullname);
  1007.                 printf("Patient's gender:   %s\n",patientdets.gender);
  1008.                 printf("Patient's age:      %s\n",patientdets.age);
  1009.                 printf("Patient's address:  %s\n",patientdets.address);
  1010.                 printf("Patient's contract: %s\n",patientdets.contact);
  1011.                 printf("------------------------------------------------------\n");
  1012.             }
  1013.  
  1014.         }
  1015.     }
  1016.     fclose(fpatient);
  1017.     printf("\n");
  1018.     system("pause");
  1019. }
  1020.  
  1021. void searchPat()
  1022. {
  1023.     do
  1024.     {
  1025.         system("cls");
  1026.         printf("---------- SEARCH MAIN MENU ----------\n");
  1027.         printf("(1)SEARCH PID\n(2)SEARCH NAME\n(3)RETURN TO MAIN\n(4)LOGOUT\n");
  1028.         do
  1029.         {
  1030.             printf("CHOICE:");
  1031.             scanf("%d", &choice);
  1032.         }while(choice<1 || choice > 4);
  1033.  
  1034.         switch (choice)
  1035.         {
  1036.             case 1:
  1037.             {
  1038.                 searchPID();
  1039.                 break;
  1040.             }
  1041.             case 2:
  1042.             {
  1043.                 searchNAME();
  1044.                 break;
  1045.             }
  1046.             case 3:
  1047.             {
  1048.                 return;
  1049.             }
  1050.             case 4:
  1051.             {
  1052.                 exit(0);
  1053.             }
  1054.             default:
  1055.             {
  1056.                 printf("PLEASE ENTER A VALID NUMBER!\n");
  1057.                 break;
  1058.             }
  1059.         }
  1060.         system("pause");
  1061.  
  1062.     }while (1 == 1);
  1063. }
  1064.  
  1065. int searchMAIN()
  1066. {
  1067.     int i;
  1068.     const char s[4] = "-->";
  1069.     char *token;
  1070.     int done = 0;
  1071.  
  1072.     FILE *fpatient;
  1073.     fpatient = fopen("PATIENT.txt","r+");
  1074.     if(fpatient==NULL)
  1075.     {
  1076.         perror("FILE CANNOT OPEN\n");
  1077.         return(-1);
  1078.     }
  1079.     else
  1080.     {
  1081.         char buffer[200];
  1082.         char input[20];
  1083.         char tok[200];
  1084.         int line=0;
  1085.  
  1086.         printf("ENTER ID: ");
  1087.         fflush(stdin);
  1088.         scanf("%20s",input);
  1089.         while(fgets(buffer,200,fpatient) != NULL)
  1090.         {
  1091.  
  1092.             line++;
  1093.             token = strtok(buffer, s);
  1094.             if (strcmp(token,input)==0)
  1095.             {
  1096.                 strcpy(patientdets.pid,token);
  1097.                 token = strtok(NULL,s);
  1098.                 strcpy(patientdets.fullname,token);
  1099.                 token = strtok(NULL,s);
  1100.                 strcpy(patientdets.gender,token);
  1101.                 token = strtok(NULL,s);
  1102.                 strcpy(patientdets.age,token);
  1103.                 token = strtok(NULL,s);
  1104.                 strcpy(patientdets.address,token);
  1105.                 token = strtok(NULL,s);
  1106.                 strcpy(patientdets.contact,token);
  1107.                 token = strtok(NULL,s);
  1108.  
  1109.                 fclose(fpatient);
  1110.                 return line;
  1111.             }
  1112.         }
  1113.         if(feof(fpatient))
  1114.         {
  1115.             printf("\n**SEARCH COMPLETED!**\n");
  1116.         }
  1117.  
  1118.     }
  1119.     fclose(fpatient);
  1120.     return -1;
  1121. }
  1122.  
  1123. void searchPID()
  1124. {
  1125.     int found = 0;
  1126.     found = searchMAIN();
  1127.     if (found != -1)
  1128.     {
  1129.         printf("Patient ID:     %s\n",patientdets.pid);
  1130.         printf("Fullname  :     %s\n",patientdets.fullname);
  1131.         printf("Gender    :     %s\n",patientdets.gender);
  1132.         printf("Age       :     %s\n",patientdets.age);
  1133.         printf("Address   :     %s\n",patientdets.address);
  1134.         printf("Contact   :     %s\n",patientdets.contact);
  1135.     }
  1136.     else
  1137.     {
  1138.         printf("\n\nID NOT DETECTED\n!");
  1139.     }
  1140. }
  1141.  
  1142. void searchNAME()
  1143. {
  1144.     const char s[4] = "-->";
  1145.     char *token;
  1146.  
  1147.     FILE *fpatient;
  1148.     fpatient = fopen("PATIENT.txt","r");
  1149.     if(fpatient==NULL)
  1150.     {
  1151.         perror("FILE CANNOT OPEN\n");
  1152.         exit(1);
  1153.     }
  1154.     else
  1155.     {
  1156.         char buffer[1024];
  1157.         char input[1024];
  1158.         char tok[512];
  1159.         int done = 0;
  1160.  
  1161.         printf("ENTER NAME: ");
  1162.         fflush(stdin);
  1163.         gets(input);
  1164.         while(fgets(buffer,1024,fpatient) != NULL)
  1165.         {
  1166.             strcpy(tok,buffer);
  1167.             token = strtok(tok,s);
  1168.             token = strtok(NULL,s);
  1169.  
  1170.             if(strstr(token,input)!=NULL)
  1171.             {
  1172.                 token = strtok(buffer, s);
  1173.                 strcpy(patientdets.pid,token);
  1174.                 token = strtok(NULL,s);
  1175.                 strcpy(patientdets.fullname,token);
  1176.                 token = strtok(NULL,s);
  1177.                 strcpy(patientdets.gender,token);
  1178.                 token = strtok(NULL,s);
  1179.                 strcpy(patientdets.age,token);
  1180.                 token = strtok(NULL,s);
  1181.                 strcpy(patientdets.address,token);
  1182.                 token = strtok(NULL,s);
  1183.                 strcpy(patientdets.contact,token);
  1184.                 token = strtok(NULL,s);
  1185.  
  1186.                 printf("Patient ID:     %s\n",patientdets.pid);
  1187.                 printf("Fullname  :     %s\n",patientdets.fullname);
  1188.                 printf("Gender    :     %s\n",patientdets.gender);
  1189.                 printf("Age       :     %s\n",patientdets.age);
  1190.                 printf("Address   :     %s\n",patientdets.address);
  1191.                 printf("Contact   :     %s\n",patientdets.contact);
  1192.  
  1193.                 done =1;
  1194.             }
  1195.         }
  1196.         if(feof(fpatient))
  1197.         {
  1198.              printf("\nSearch complete\n");
  1199.         }
  1200.         else
  1201.         {
  1202.             printf("\nFile can't be searched completely ");
  1203.             exit(1);
  1204.         }
  1205.     if (done!=1)
  1206.     {
  1207.         printf("Name cannot be found");
  1208.     }
  1209.     }
  1210.     fclose(fpatient);
  1211. }
  1212.  
  1213. void editPat()
  1214. {
  1215.     FILE *fpatient;
  1216.     FILE *ftemp;
  1217.     int found=0,choice=0,line=0,i=0;
  1218.     char selection=0,ch;
  1219.     char buff[512]="",buff2[512];
  1220.     found = searchMAIN();
  1221.     if (found != -1)
  1222.     {
  1223.         fflush(stdin);
  1224.         printf("Is this the patient ID you want to modify? (Y/N)\n");
  1225.         printf("Patient ID:     %s\n",patientdets.pid);
  1226.         printf("Fullname  :     %s\n",patientdets.fullname);
  1227.         printf("Gender    :     %s\n",patientdets.gender);
  1228.         printf("Age       :     %s\n",patientdets.age);
  1229.         printf("Address   :     %s\n",patientdets.address);
  1230.         printf("Contact   :     %s\n",patientdets.contact);
  1231.         printf("CHOICE:");
  1232.         scanf("%c",&selection);
  1233.         if(toupper(selection)== 'Y')
  1234.         {
  1235.             fpatient=fopen("PATIENT.txt","a+");
  1236.             ftemp=fopen("EditPatient.txt","w");
  1237.  
  1238.             while(!feof(fpatient))
  1239.             {
  1240.                 ch = fgetc(fpatient);
  1241.                 if(ch == '\n')
  1242.                     i++;
  1243.             }
  1244.  
  1245.             printf("(1)Full Name\n");printf("(2)Gender\n");printf("(3)Age\n");printf("(4)Address\n");printf("(5)Contact\n");printf("(0)Back\n");
  1246.             printf("Select option to be modified:");
  1247.             scanf("%d",&choice);
  1248.             switch(choice)
  1249.             {
  1250.                 case 1:
  1251.                 {
  1252.                     fflush(stdin);
  1253.                     strcpy(patientdets.fullname,getFullName());
  1254.                     break;
  1255.                 }
  1256.                 case 2:
  1257.                 {
  1258.                     fflush(stdin);
  1259.                     strcpy(patientdets.gender,getGender());
  1260.                     printf("%s",patienttemp.gender);
  1261.                     break;
  1262.                 }
  1263.                 case 3:
  1264.                 {
  1265.                     fflush(stdin);
  1266.                     strcpy(patientdets.age,getAge());
  1267.                     break;
  1268.                 }
  1269.                 case 4:
  1270.                 {
  1271.                     fflush(stdin);
  1272.                     strcpy(patientdets.address,getAddress());
  1273.                     break;
  1274.                 }
  1275.                 case 5:
  1276.                 {
  1277.                     fflush(stdin);
  1278.                     strcpy(patientdets.contact,getContact());
  1279.                     break;
  1280.                 }
  1281.  
  1282.             }
  1283.             strcat(buff, patientdets.pid);strcat(buff,"-->");
  1284.             strcat(buff, patientdets.fullname);strcat(buff, "-->");
  1285.             strcat(buff, patientdets.gender);strcat(buff, "-->");
  1286.             strcat(buff, patientdets.age);strcat(buff, "-->");
  1287.             strcat(buff, patientdets.address);strcat(buff,"-->");
  1288.             strcat(buff, patientdets.contact);
  1289.  
  1290.  
  1291.             rewind(fpatient);
  1292.             while (line<i)
  1293.             {
  1294.  
  1295.                 line++;
  1296.                 if(found == line)
  1297.                 {
  1298.                     fputs(buff,ftemp);
  1299.                     fgets(buff2,512,fpatient); // store current line into buffer2
  1300.                 }
  1301.                 else
  1302.                 {
  1303.                     fgets(buff2,512,fpatient);
  1304.                     fputs(buff2,ftemp);
  1305.                 }
  1306.  
  1307.             }
  1308.             fclose(fpatient);
  1309.             fclose(ftemp);
  1310.             remove("PATIENT.txt");
  1311.             rename("EditPatient.txt","PATIENT.txt");
  1312.         }
  1313.         else
  1314.         {
  1315.             printf("SEE YOU AGAIN!");
  1316.             return;
  1317.             system("pause");
  1318.         }
  1319.     }
  1320.     else
  1321.     {
  1322.         printf("\n\nID NOT DETECTED\n!");
  1323.     }
  1324.     system("pause");
  1325. }
  1326.  
  1327.  
  1328.  
  1329. void deletePat()
  1330. {
  1331.     FILE *fpatient;
  1332.     fpatient = fopen("PATIENT.txt","r");
  1333.     if(fpatient==NULL)
  1334.     {
  1335.         printf("FILE CANNOT BE OPENED!\n");
  1336.         exit(1);
  1337.     }
  1338.     else
  1339.     {
  1340.         int line=0;
  1341.         char selection;
  1342.         line = searchMAIN();
  1343.         rewind(fpatient);
  1344.         if(line != -1)
  1345.         {
  1346.             printf("Patient ID:     %s\n",patientdets.pid);
  1347.             printf("Fullname  :     %s\n",patientdets.fullname);
  1348.             printf("Gender    :     %s\n",patientdets.gender);
  1349.             printf("Age       :     %s\n",patientdets.age);
  1350.             printf("Address   :     %s\n",patientdets.address);
  1351.             printf("Contact   :     %s\n",patientdets.contact);
  1352.             printf("Are you sure you wanted to delete this patient?\n");
  1353.             fflush(stdin);
  1354.             printf("CHOICE:");
  1355.             selection=getchar();
  1356.             if(toupper(selection)=='Y')
  1357.             {
  1358.                 char words[100];
  1359.                 char *buffer;
  1360.                 char *ptr;
  1361.                 int  read_line=0;
  1362.                 char user_input[100];
  1363.                 int  done=0;
  1364.  
  1365.                 buffer=(char *)malloc(1000*sizeof(char));
  1366.                 memset(buffer,0,1000*sizeof(char));
  1367.                 ptr=buffer;
  1368.  
  1369.                 strcat(patientdets.pid,"\n");
  1370.  
  1371.                 while(fgets(words,100,fpatient) != NULL)
  1372.                 {
  1373.                     read_line ++;
  1374.                     if(read_line != line)
  1375.                     {
  1376.                         strcpy(ptr,words);
  1377.                         ptr +=strlen(words);
  1378.                     }
  1379.                 }
  1380.                 fclose(fpatient);
  1381.  
  1382.                 fpatient=fopen("PATIENT.txt","w");
  1383.                 fprintf(fpatient,"%s",buffer);
  1384.                 fclose(fpatient);
  1385.             }
  1386.         }
  1387.     }
  1388.     system("pause");
  1389. }
  1390.  
  1391. int addApp()
  1392. {
  1393.     FILE *fapp;
  1394.     int found=-1;
  1395.     char selection;
  1396.  
  1397.     found = searchMAIN();
  1398.     if (found >= 0)
  1399.     {
  1400.         printf("Add appointment for this patient? (Y/N)\n");
  1401.         printf("Patient ID:     %s\n",patientdets.pid);
  1402.         printf("Fullname  :     %s\n",patientdets.fullname);
  1403.         printf("Gender    :     %s\n",patientdets.gender);
  1404.         printf("Age       :     %s\n",patientdets.age);
  1405.         printf("Address   :     %s\n",patientdets.address);
  1406.         printf("Contact   :     %s\n",patientdets.contact);
  1407.         fflush(stdin);
  1408.         scanf("%c",&selection);
  1409.         if(toupper(selection)=='Y')
  1410.         {
  1411.             fapp=fopen("APP.txt","a+");
  1412.  
  1413.             strcpy(appdets.pid,patientdets.pid);
  1414.             fflush(stdin);
  1415.             strcpy(appdets.date,getDate());
  1416.             fflush(stdin);
  1417.             strcpy(appdets.time,getTime());
  1418.             fflush(stdin);
  1419.             strcpy(appdets.location,getLocation());
  1420.             fflush(stdin);
  1421.             strcpy(appdets.doctor,getDoctor());
  1422.             fflush(stdin);
  1423.             strcpy(appdets.apptype,getAppType());
  1424.             fprintf(fapp,"%s-->%s-->%s-->%s-->%s-->%s\n",appdets.pid,appdets.date,appdets.time,appdets.location,appdets.doctor,appdets.apptype);
  1425.             fclose(fapp);
  1426.         }
  1427.         else
  1428.         {
  1429.             printf("<-Back");
  1430.             return 1;
  1431.         }
  1432.     }
  1433.     return 0;
  1434.     system("pause");
  1435. }
  1436.  
  1437. void viewApp()
  1438. {
  1439.     system("cls");
  1440.     char buffer[1024]=" ";
  1441.     char s[4] = "-->";
  1442.     char *token;
  1443.     int counter = 1;
  1444.  
  1445.     FILE *fapp;
  1446.     fapp = fopen("APP.txt","r");
  1447.  
  1448.     if (fapp == NULL)
  1449.     {
  1450.         printf("ERROR:FILE CANNOT BE OPENED!\n");
  1451.     }
  1452.     else
  1453.     {
  1454.         if (feof(fapp))
  1455.         {
  1456.             printf("ERROR:END OF FILE!\n");
  1457.             fclose(fapp);
  1458.         }
  1459.         else
  1460.         {
  1461.             while( fgets(buffer,1024,fapp) != NULL )
  1462.             {
  1463.                 token = strtok(buffer, s);
  1464.                 strcpy(appdets.pid,token);
  1465.                 token = strtok(NULL,s);
  1466.                 strcpy(appdets.date,token);
  1467.                 token = strtok(NULL,s);
  1468.                 strcpy(appdets.time,token);
  1469.                 token = strtok(NULL,s);
  1470.                 strcpy(appdets.location,token);
  1471.                 token = strtok(NULL,s);
  1472.                 strcpy(appdets.doctor,token);
  1473.                 token = strtok(NULL,s);
  1474.                 strcpy(appdets.apptype,token);
  1475.                 token = strtok(NULL,s);
  1476.  
  1477.                 printf("Patient's ID: %s\n",appdets.pid);
  1478.                 printf("Appointment's Date: %s\n",appdets.date);
  1479.                 printf("Appointment's Time: %s\n",appdets.time);
  1480.                 printf("Appointment's Location: %s\n",appdets.location);
  1481.                 printf("Doctor In Charge: %s\n",appdets.doctor);
  1482.                 printf("Appointment's Type: %s\n",appdets.apptype);
  1483.                 printf("------------------------------------------------------\n");
  1484.             }
  1485.             fclose(fapp);
  1486.         }
  1487.     }
  1488.  
  1489.  
  1490. }
  1491.  
  1492. int searchApp()
  1493. {
  1494.     system("cls");
  1495.     char buffer[1024]=" ";
  1496.     char s[4] = "-->";
  1497.     char *token;
  1498.     int counter = 0;
  1499.     char tempID[100] ;
  1500.  
  1501.     FILE *fapp;
  1502.     fapp = fopen("APP.txt","r");
  1503.  
  1504.     if (fapp == NULL)
  1505.     {
  1506.         printf("ERROR:FILE CANNOT BE OPENED!\n");
  1507.     }
  1508.     else
  1509.     {
  1510.         if (feof(fapp))
  1511.         {
  1512.             printf("ERROR:END OF FILE!\n");
  1513.             fclose(fapp);
  1514.         }
  1515.         else
  1516.         {
  1517.             printf("ENTER ID: ");
  1518.             fflush(stdin);
  1519.             gets(tempID);
  1520.  
  1521.             while( fgets(buffer,1024,fapp) != NULL )
  1522.             {
  1523.                 counter ++ ;
  1524.                 token = strtok(buffer, s);
  1525.                 if ( strcmp(token,tempID)== 0)
  1526.                 {
  1527.                     strcpy(appdets.pid,token);
  1528.                     token = strtok(NULL,s);
  1529.                     strcpy(appdets.date,token);
  1530.                     token = strtok(NULL,s);
  1531.                     strcpy(appdets.time,token);
  1532.                     token = strtok(NULL,s);
  1533.                     strcpy(appdets.location,token);
  1534.                     token = strtok(NULL,s);
  1535.                     strcpy(appdets.doctor,token);
  1536.                     token = strtok(NULL,s);
  1537.                     strcpy(appdets.apptype,token);
  1538.                     token = strtok(NULL,s);
  1539.  
  1540.                     printf("Patient's ID:           %s\n",appdets.pid);
  1541.                     printf("Appointment's Date:     %s\n",appdets.date);
  1542.                     printf("Appointment's Time:     %s\n",appdets.time);
  1543.                     printf("Appointment's Location: %s\n",appdets.location);
  1544.                     printf("Doctor In Charge:       %s\n",appdets.doctor);
  1545.                     printf("Appointment's Type:     %s\n",appdets.apptype);
  1546.                     printf("------------------------------------------------------\n");
  1547.  
  1548.                     fclose(fapp);
  1549.                     return counter;
  1550.                 }
  1551.             }
  1552.  
  1553.         }
  1554.     }
  1555.     printf("Can't find appointment");
  1556.     system("pause");
  1557.  
  1558.     fclose(fapp);
  1559.     return counter;
  1560.  
  1561. }
  1562.  
  1563. void deleteApp()
  1564. {
  1565.     system("cls");
  1566.     FILE *fapp;
  1567.     FILE *fpatient;
  1568.     fapp = fopen("APP.txt","r");
  1569.     char buffer[1024]=" ";
  1570.     char s[4] = "-->";
  1571.     char *token;
  1572.     int counter = 1;
  1573.     char tempID[100];
  1574.     if(fapp==NULL)
  1575.     {
  1576.         printf("FILE CANNOT BE OPENED!\n");
  1577.         exit(1);
  1578.     }
  1579.     else
  1580.     {
  1581.         char selection;
  1582.         printf("ENTER ID: ");
  1583.         fflush(stdin);
  1584.         gets(tempID);
  1585.  
  1586.         while(fgets(buffer,1024,fapp) != NULL)
  1587.         {
  1588.             token = strtok(buffer, s);
  1589.             if ( strcmp(token,tempID)== 0)
  1590.             {
  1591.                 strcpy(appdets.pid,token);
  1592.                 token = strtok(NULL,s);
  1593.                 strcpy(appdets.date,token);
  1594.                 token = strtok(NULL,s);
  1595.                 strcpy(appdets.time,token);
  1596.                 token = strtok(NULL,s);
  1597.                 strcpy(appdets.location,token);
  1598.                 token = strtok(NULL,s);
  1599.                 strcpy(appdets.doctor,token);
  1600.                 token = strtok(NULL,s);
  1601.                 strcpy(appdets.apptype,token);
  1602.                 token = strtok(NULL,s);
  1603.  
  1604.  
  1605.                 printf("Patient's ID:           %s\n",appdets.pid);
  1606.                 printf("Appointment's Date:     %s\n",appdets.date);
  1607.                 printf("Appointment's Time:     %s\n",appdets.time);
  1608.                 printf("Appointment's Location: %s\n",appdets.location);
  1609.                 printf("Doctor In Charge:       %s\n",appdets.doctor);
  1610.                 printf("Appointment's Type:     %s\n",appdets.apptype);
  1611.                 printf("------------------------------------------------------\n");
  1612.                 printf("Are you sure you wanted to delete this entry?\n");
  1613.                 fflush(stdin);
  1614.                 printf("CHOICE:");
  1615.                 selection=getchar();
  1616.                 if(toupper(selection)=='Y')
  1617.                 {
  1618.                     char words[100];
  1619.                     char *buffer;
  1620.                     char *ptr;
  1621.                     int  read_line=0;
  1622.                     char user_input[100];
  1623.                     int  done=0;
  1624.  
  1625.                     buffer=(char *)malloc(1000*sizeof(char));
  1626.                     memset(buffer,0,1000*sizeof(char));
  1627.                     ptr=buffer;
  1628.  
  1629.                     strcat(appdets.pid,"\n");
  1630.  
  1631.                     while(fgets(words,100,fapp) != NULL)
  1632.                     {
  1633.                         read_line ++;
  1634.                         if(read_line != 0)
  1635.                         {
  1636.                             strcpy(ptr,words);
  1637.                             ptr +=strlen(words);
  1638.                         }
  1639.                     }
  1640.                     fclose(fapp);
  1641.  
  1642.                     fapp=fopen("APP.txt","w");
  1643.                     fprintf(fapp,"%s",buffer);
  1644.                     fclose(fapp);
  1645.                 }
  1646.             }
  1647.         }
  1648.     }
  1649. }
  1650.  
  1651. void editApp()
  1652. {
  1653.     FILE *fapp;
  1654.     FILE *fTemp;
  1655.     int found=0,choice=0,line=0,i=0;
  1656.     char selection=0,ch;
  1657.     char buff[512]="" , buff2[512]="";
  1658.     found = searchApp();
  1659.     if (found != -1)
  1660.     {
  1661.         fflush(stdin);
  1662.         system("cls");
  1663.         printf("Patient's ID:           %s\n",appdets.pid);
  1664.                 printf("Appointment's Date:     %s\n",appdets.date);
  1665.                 printf("Appointment's Time:     %s\n",appdets.time);
  1666.                 printf("Appointment's Location: %s\n",appdets.location);
  1667.                 printf("Doctor In Charge:       %s\n",appdets.doctor);
  1668.                 printf("Appointment's Type:     %s\n",appdets.apptype);
  1669.                 printf("------------------------------------------------------\n");
  1670.                 printf("Are you sure you wanted to edit this appointment?\n");
  1671.         printf("CHOICE:");
  1672.         scanf("%c",&selection);
  1673.         if(toupper(selection)== 'Y')
  1674.         {
  1675.             fapp=fopen("APP.txt","a+");
  1676.             fTemp=fopen("EditAPP.txt","w");
  1677.             while(!feof(fapp))
  1678.             {
  1679.                 ch = fgetc(fapp);
  1680.                 if(ch == '\n')
  1681.                     i++;
  1682.             }
  1683.  
  1684.  
  1685.             printf("(1)Date\n");printf("(2)Time\n");printf("(3)Location\n");printf("(4)Doctor\n");printf("(5)Apptype\n");printf("(0)Back\n");
  1686.             printf("Select option to be modified:");
  1687.             scanf("%d",&choice);
  1688.             switch(choice)
  1689.             {
  1690.                 case 1:
  1691.                 {
  1692.                     fflush(stdin);
  1693.                     strcpy(appdets.date,getDate());
  1694.                     break;
  1695.                 }
  1696.                 case 2:
  1697.                 {
  1698.                     fflush(stdin);
  1699.                     strcpy(appdets.time,getTime());
  1700.                     break;
  1701.                 }
  1702.                 case 3:
  1703.                 {
  1704.                     fflush(stdin);
  1705.                     strcpy(appdets.location,getLocation());
  1706.                     break;
  1707.                 }
  1708.                 case 4:
  1709.                 {
  1710.                     fflush(stdin);
  1711.                     strcpy(appdets.doctor,getDoctor());
  1712.                     break;
  1713.                 }
  1714.                 case 5:
  1715.                 {
  1716.                     fflush(stdin);
  1717.                     strcpy(appdets.apptype,getAppType());
  1718.                     break;
  1719.                 }
  1720.  
  1721.             }
  1722.             strcat(buff, appdets.pid);strcat(buff,"-->");
  1723.             strcat(buff, appdets.date);strcat(buff, "-->");
  1724.             strcat(buff, appdets.time);strcat(buff, "-->");
  1725.             strcat(buff, appdets.location);strcat(buff, "-->");
  1726.             strcat(buff, appdets.doctor);strcat(buff,"-->");
  1727.             strcat(buff, appdets.apptype);
  1728.  
  1729.             rewind(fapp);
  1730.             while (line<i)
  1731.             {
  1732.                 line++;
  1733.                 if(found == line)
  1734.                 {
  1735.                     fputs(buff,fTemp);
  1736.                     fgets(buff2,512,fapp); // store current line into buffer2
  1737.                 }
  1738.                 else
  1739.                 {
  1740.                     fgets(buff2,512,fapp);
  1741.                     fputs(buff2,fTemp);
  1742.                 }
  1743.             }
  1744.             fclose(fapp);
  1745.             fclose(fTemp);
  1746.             remove("APP.txt");
  1747.             rename("EditApp.txt","APP.txt");
  1748.         }
  1749.         else
  1750.         {
  1751.             printf("SEE YOU AGAIN!");
  1752.             return;
  1753.             system("pause");
  1754.         }
  1755.     }
  1756.     else
  1757.     {
  1758.         printf("\n\nID NOT DETECTED\n!");
  1759.     }
  1760.  
  1761. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement