Advertisement
Guest User

C For Macs

a guest
Jun 14th, 2016
139
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 fname[50];
  35.     char lname[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 FB");
  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.     static 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.         printf("Enter First Name:\n");
  754.         gets(first);
  755.         val=isalphaVal(first);
  756.     }while(val==0);
  757.     return first;
  758. }
  759.  
  760. char *getLastName()
  761. {
  762.     int val=0;
  763.     static char last[20];
  764.     do
  765.     {
  766.         printf("Enter Last Name:\n");
  767.         gets(last);
  768.         val=isalphaVal(last);
  769.     }while(val==0);
  770.     return last;
  771. }
  772.  
  773.  
  774. char *getFullName()
  775. {
  776.     static char full[50];
  777.     printf("Insert Full name:\n");
  778.     gets(full);
  779.     return full;
  780. }
  781. char *getGender()
  782. {
  783.     static char gender[5];
  784.     printf("Enter Gender:\n");
  785.     gets(gender);
  786.     return gender;
  787. }
  788.  
  789. char *getAge()
  790. {
  791.     int val=0;
  792.     static char age[3];
  793.     do
  794.     {
  795.         printf("Enter Age:\n");
  796.         gets(age);
  797.         val=isdigitVal(age);
  798.     }while(val==0);
  799.     return age;
  800. }
  801.  
  802. char *getAddress()
  803. {
  804.     static char address[30];
  805.     printf("Enter Address:\n");
  806.     gets(address);
  807.     return address;
  808. }
  809.  
  810. char *getContact()
  811. {
  812.     int val=0;
  813.     static char contact[20];
  814.     do
  815.     {
  816.         printf("Enter Contact:\n");
  817.         gets(contact);
  818.         val=isdigitVal(contact);
  819.     }while(val==0);
  820.     return contact;
  821. }
  822.  
  823. char *getDate()
  824. {
  825.     int val=0;
  826.     static char day[4];
  827.     static char month[4];
  828.     static char year[8];
  829.     static char date[25];
  830.     char slash[2]="/";
  831.     char sslash[2]="/";
  832.     printf("Enter App. Day:\n");
  833.     gets(day);
  834.     printf("Enter App. Month:\n");
  835.     gets(month);
  836.     printf("Enter App. Year:\n");
  837.     gets(year);
  838.  
  839.     strcpy(date,"");
  840.     strcat(date,day);
  841.     strcat(date,sslash);
  842.     strcat(date,month);
  843.     strcat(date,slash);
  844.     strcat(date,year);
  845.     return date;
  846. }
  847.  
  848. char *getTime()
  849. {
  850.     static char time[15];
  851.     static char hour[5];
  852.     static char mins[5];
  853.     static char type[3];
  854.     char dot[2]=":";
  855.  
  856.     printf("Enter App. Hour:\n");
  857.     gets(hour);
  858.     printf("Enter App. Minutes:\n");
  859.     gets(mins);
  860.     printf("(AM/PM):\n");
  861.     gets(type);
  862.  
  863.     strcpy(time,"");
  864.     strcat(time,hour);
  865.     strcat(time,dot);
  866.     strcat(time,mins);
  867.     strcat(time,type);
  868.     return time;
  869. }
  870.  
  871. char *getLocation()
  872. {
  873.     static char location[40];
  874.     printf("Enter App. Location:\n");
  875.     gets(location);
  876.     return location;
  877. }
  878.  
  879. char *getDoctor()
  880. {
  881.     static char doctor[30];
  882.     printf("Enter App. Doctor:\n");
  883.     gets(doctor);
  884.     return doctor;
  885. }
  886.  
  887. char *getAppType()
  888. {
  889.      int app;
  890.      char sp[]="Specialist";
  891.      char no[]="Normal";
  892.      char he[]="Health Test";
  893.      printf("Choose Appointment Type:\n(1)Specialist\t(2)Normal\t(3)Health Test\n");
  894.      scanf("%d",&app);
  895.      switch(app)
  896.      {
  897.          case 1:
  898.             {
  899.                 return(sp);
  900.             }
  901.          case 2:
  902.             {
  903.                 return(no);
  904.             }
  905.          case 3:
  906.             {
  907.                 return(he);
  908.             }
  909.          default:
  910.             {
  911.                 perror("PLEASE ENTER A VALID NUMBER!\n");
  912.                 return(-1);
  913.             }
  914.      }
  915.  
  916. }
  917.  
  918. void addPat()
  919. {
  920.     system("color FC");
  921.     int i = 0;
  922.     char z[30] = " ";
  923.     int selection = 'N';
  924.  
  925.     FILE *fpatient;
  926.  
  927.     do
  928.     {
  929.         fpatient = fopen("PATIENT.txt", "a");
  930.  
  931.         if (fpatient==NULL)
  932.         {
  933.             perror("FILE DOES NOT EXISTS!\n");
  934.             return ;
  935.         }
  936.         else
  937.         {
  938.             system("cls");
  939.             i = getPID();
  940.             printf("Patient ID:%s\n",patientdets.pid);
  941.             fflush(stdin);
  942.             strcpy(patientdets.fname,getFirstName());
  943.             strcpy(patientdets.lname,getLastName());
  944.             strcat(z,patientdets.lname);
  945.             strcat(patientdets.fname,z);
  946.             strcpy(patientdets.fullname,patientdets.fname);
  947.             strcpy(patientdets.gender,getGender());
  948.             strcpy(patientdets.age,getAge());
  949.             strcpy(patientdets.address,getAddress());
  950.             strcpy(patientdets.contact,getContact());
  951.             fprintf(fpatient,"%s-->%s-->%s-->%s-->%s-->%s\n",patientdets.pid,patientdets.fullname,patientdets.gender,patientdets.age,patientdets.address,patientdets.contact);
  952.             printf("\nDo You Want To Add Another Patient's Details? (Y/N):");
  953.             fflush(stdin);
  954.             selection = getchar();
  955.             fclose(fpatient);
  956.         }
  957.  
  958.     }while(selection == 'Y' || selection == 'y');
  959.  
  960.     return ;
  961. }
  962.  
  963. void viewPat()
  964. {
  965.     system("cls");
  966.     char buffer[1024]=" ";
  967.     char s[4] = "-->";
  968.     char *token;
  969.     int counter = 1;
  970.     FILE *fpatient;
  971.  
  972.     fpatient = fopen("PATIENT.txt","r");
  973.  
  974.     if (fpatient == NULL)
  975.     {
  976.         printf("ERROR:FILE CANNOT BE OPENED!\n");
  977.     }
  978.     else
  979.     {
  980.         if (feof(fpatient))
  981.         {
  982.             printf("ERROR:END OF FILE!\n");
  983.         }
  984.         else
  985.         {
  986.             while( fgets(buffer,1024,fpatient) != NULL )
  987.             {
  988.                 token = strtok(buffer, s);
  989.                 strcpy(patientdets.pid,token);
  990.                 token = strtok(NULL,s);
  991.                 strcpy(patientdets.fullname,token);
  992.                 token = strtok(NULL,s);
  993.                 strcpy(patientdets.gender,token);
  994.                 token = strtok(NULL,s);
  995.                 strcpy(patientdets.age,token);
  996.                 token = strtok(NULL,s);
  997.                 strcpy(patientdets.address,token);
  998.                 token = strtok(NULL,s);
  999.                 strcpy(patientdets.contact,token);
  1000.                 token = strtok(NULL,s);
  1001.  
  1002.                 printf("Patient's ID:       %s\n",patientdets.pid);
  1003.                 printf("Patient's name:     %s\n",patientdets.fullname);
  1004.                 printf("Patient's gender:   %s\n",patientdets.gender);
  1005.                 printf("Patient's age:      %s\n",patientdets.age);
  1006.                 printf("Patient's address:  %s\n",patientdets.address);
  1007.                 printf("Patient's contract: %s\n",patientdets.contact);
  1008.                 printf("------------------------------------------------------\n");
  1009.             }
  1010.  
  1011.         }
  1012.     }
  1013.     fclose(fpatient);
  1014.     printf("\n");
  1015.     system("pause");
  1016. }
  1017.  
  1018. void searchPat()
  1019. {
  1020.     do
  1021.     {
  1022.         system("cls");
  1023.         printf("---------- SEARCH MAIN MENU ----------\n");
  1024.         printf("(1)SEARCH PID\n(2)SEARCH NAME\n(3)RETURN TO MAIN\n(4)LOGOUT\n");
  1025.         do
  1026.         {
  1027.             printf("CHOICE:");
  1028.             scanf("%d", &choice);
  1029.         }while(choice<1 || choice > 4);
  1030.  
  1031.         switch (choice)
  1032.         {
  1033.             case 1:
  1034.             {
  1035.                 searchPID();
  1036.                 break;
  1037.             }
  1038.             case 2:
  1039.             {
  1040.                 searchNAME();
  1041.                 break;
  1042.             }
  1043.             case 3:
  1044.             {
  1045.                 return;
  1046.             }
  1047.             case 4:
  1048.             {
  1049.                 exit(0);
  1050.             }
  1051.             default:
  1052.             {
  1053.                 printf("PLEASE ENTER A VALID NUMBER!\n");
  1054.                 break;
  1055.             }
  1056.         }
  1057.         system("pause");
  1058.  
  1059.     }while (1 == 1);
  1060. }
  1061.  
  1062. int searchMAIN()
  1063. {
  1064.     int i;
  1065.     const char s[4] = "-->";
  1066.     char *token;
  1067.     int done = 0;
  1068.  
  1069.     FILE *fpatient;
  1070.     fpatient = fopen("PATIENT.txt","r+");
  1071.     if(fpatient==NULL)
  1072.     {
  1073.         perror("FILE CANNOT OPEN\n");
  1074.         return(-1);
  1075.     }
  1076.     else
  1077.     {
  1078.         char buffer[200];
  1079.         char input[20];
  1080.         char tok[200];
  1081.         int line=0;
  1082.  
  1083.         printf("ENTER ID: ");
  1084.         fflush(stdin);
  1085.         scanf("%20s",input);
  1086.         while(fgets(buffer,200,fpatient) != NULL)
  1087.         {
  1088.  
  1089.             line++;
  1090.             token = strtok(buffer, s);
  1091.             if (strcmp(token,input)==0)
  1092.             {
  1093.                 strcpy(patientdets.pid,token);
  1094.                 token = strtok(NULL,s);
  1095.                 strcpy(patientdets.fullname,token);
  1096.                 token = strtok(NULL,s);
  1097.                 strcpy(patientdets.gender,token);
  1098.                 token = strtok(NULL,s);
  1099.                 strcpy(patientdets.age,token);
  1100.                 token = strtok(NULL,s);
  1101.                 strcpy(patientdets.address,token);
  1102.                 token = strtok(NULL,s);
  1103.                 strcpy(patientdets.contact,token);
  1104.                 token = strtok(NULL,s);
  1105.  
  1106.                 fclose(fpatient);
  1107.                 return line;
  1108.             }
  1109.         }
  1110.         if(feof(fpatient))
  1111.         {
  1112.             printf("\n**SEARCH COMPLETED!**\n");
  1113.         }
  1114.  
  1115.     }
  1116.     fclose(fpatient);
  1117.     return -1;
  1118. }
  1119.  
  1120. void searchPID()
  1121. {
  1122.     int found = 0;
  1123.     found = searchMAIN();
  1124.     if (found != -1)
  1125.     {
  1126.         printf("Patient ID:     %s\n",patientdets.pid);
  1127.         printf("Fullname  :     %s\n",patientdets.fullname);
  1128.         printf("Gender    :     %s\n",patientdets.gender);
  1129.         printf("Age       :     %s\n",patientdets.age);
  1130.         printf("Address   :     %s\n",patientdets.address);
  1131.         printf("Contact   :     %s\n",patientdets.contact);
  1132.     }
  1133.     else
  1134.     {
  1135.         printf("\n\nID NOT DETECTED\n!");
  1136.     }
  1137. }
  1138.  
  1139. void searchNAME()
  1140. {
  1141.     const char s[4] = "-->";
  1142.     char *token;
  1143.  
  1144.     FILE *fpatient;
  1145.     fpatient = fopen("PATIENT.txt","r+");
  1146.     if(fpatient==NULL)
  1147.     {
  1148.         perror("FILE CANNOT OPEN\n");
  1149.         exit(1);
  1150.     }
  1151.     else
  1152.     {
  1153.         char buffer[1024];
  1154.         char input[1024];
  1155.         char tok[512];
  1156.         int done = 0;
  1157.  
  1158.         printf("ENTER NAME: ");
  1159.         fflush(stdin);
  1160.         gets(input);
  1161.         while(fgets(buffer,1024,fpatient) != NULL)
  1162.         {
  1163.             strcpy(tok,buffer);
  1164.             token = strtok(tok,s);
  1165.             token = strtok(NULL,s);
  1166.  
  1167.             if(strstr(token,input)!=NULL)
  1168.             {
  1169.                 token = strtok(buffer, s);
  1170.                 strcpy(patientdets.pid,token);
  1171.                 token = strtok(NULL,s);
  1172.                 strcpy(patientdets.fullname,token);
  1173.                 token = strtok(NULL,s);
  1174.                 strcpy(patientdets.gender,token);
  1175.                 token = strtok(NULL,s);
  1176.                 strcpy(patientdets.age,token);
  1177.                 token = strtok(NULL,s);
  1178.                 strcpy(patientdets.address,token);
  1179.                 token = strtok(NULL,s);
  1180.                 strcpy(patientdets.contact,token);
  1181.                 token = strtok(NULL,s);
  1182.  
  1183.                 printf("Patient ID:     %s\n",patientdets.pid);
  1184.                 printf("Fullname  :     %s\n",patientdets.fullname);
  1185.                 printf("Gender    :     %s\n",patientdets.gender);
  1186.                 printf("Age       :     %s\n",patientdets.age);
  1187.                 printf("Address   :     %s\n",patientdets.address);
  1188.                 printf("Contact   :     %s\n",patientdets.contact);
  1189.  
  1190.                 done =1;
  1191.             }
  1192.         }
  1193.         if(feof(fpatient))
  1194.         {
  1195.              printf("\nSearch complete\n");
  1196.         }
  1197.         else
  1198.         {
  1199.             printf("\nFile can't be searched completely ");
  1200.             exit(1);
  1201.         }
  1202.     if (done!=1)
  1203.     {
  1204.         printf("Name cannot be found");
  1205.     }
  1206.     }
  1207. }
  1208.  
  1209. void editPat()
  1210. {
  1211.     FILE *fpatient;
  1212.     FILE *ftemp;
  1213.     int found=0,choice=0,line=0,i=0;
  1214.     char selection=0,ch;
  1215.     char buff[512]="",buff2[512];
  1216.     found = searchMAIN();
  1217.     if (found != -1)
  1218.     {
  1219.         fflush(stdin);
  1220.         printf("Is this the patient ID you want to modify? (Y/N)\n");
  1221.         printf("Patient ID:     %s\n",patientdets.pid);
  1222.         printf("Fullname  :     %s\n",patientdets.fullname);
  1223.         printf("Gender    :     %s\n",patientdets.gender);
  1224.         printf("Age       :     %s\n",patientdets.age);
  1225.         printf("Address   :     %s\n",patientdets.address);
  1226.         printf("Contact   :     %s\n",patientdets.contact);
  1227.         printf("CHOICE:");
  1228.         scanf("%c",&selection);
  1229.         if(toupper(selection)== 'Y')
  1230.         {
  1231.             fpatient=fopen("PATIENT.txt","a+");
  1232.             ftemp=fopen("EditPatient.txt","w");
  1233.  
  1234.             while(!feof(fpatient))
  1235.             {
  1236.                 ch = fgetc(fpatient);
  1237.                 if(ch == '\n')
  1238.                     i++;
  1239.             }
  1240.  
  1241.             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");
  1242.             printf("Select option to be modified:");
  1243.             scanf("%d",&choice);
  1244.             switch(choice)
  1245.             {
  1246.                 case 1:
  1247.                 {
  1248.                     fflush(stdin);
  1249.                     strcpy(patientdets.fullname,getFullName());
  1250.                     break;
  1251.                 }
  1252.                 case 2:
  1253.                 {
  1254.                     fflush(stdin);
  1255.                     strcpy(patientdets.gender,getGender());
  1256.                     printf("%s",patienttemp.gender);
  1257.                     break;
  1258.                 }
  1259.                 case 3:
  1260.                 {
  1261.                     fflush(stdin);
  1262.                     strcpy(patientdets.age,getAge());
  1263.                     break;
  1264.                 }
  1265.                 case 4:
  1266.                 {
  1267.                     fflush(stdin);
  1268.                     strcpy(patientdets.address,getAddress());
  1269.                     break;
  1270.                 }
  1271.                 case 5:
  1272.                 {
  1273.                     fflush(stdin);
  1274.                     strcpy(patientdets.contact,getContact());
  1275.                     break;
  1276.                 }
  1277.  
  1278.             }
  1279.             strcat(buff, patientdets.pid);strcat(buff,"-->");
  1280.             strcat(buff, patientdets.fullname);strcat(buff, "-->");
  1281.             strcat(buff, patientdets.gender);strcat(buff, "-->");
  1282.             strcat(buff, patientdets.age);strcat(buff, "-->");
  1283.             strcat(buff, patientdets.address);strcat(buff,"-->");
  1284.             strcat(buff, patientdets.contact);
  1285.  
  1286.  
  1287.             rewind(fpatient);
  1288.             while (line<i)
  1289.             {
  1290.  
  1291.                 line++;
  1292.                 if(found == line)
  1293.                 {
  1294.                     fputs(buff,ftemp);
  1295.                     fgets(buff2,512,fpatient); // store current line into buffer2
  1296.                 }
  1297.                 else
  1298.                 {
  1299.                     fgets(buff2,512,fpatient);
  1300.                     fputs(buff2,ftemp);
  1301.                 }
  1302.  
  1303.             }
  1304.             fclose(fpatient);
  1305.             fclose(ftemp);
  1306.             remove("PATIENT.txt");
  1307.  
  1308.             rename("EditPatient.txt","PATIENT.txt");
  1309.         }
  1310.         else
  1311.         {
  1312.             printf("SEE YOU AGAIN!");
  1313.             return;
  1314.             system("pause");
  1315.         }
  1316.     }
  1317.     else
  1318.     {
  1319.         printf("\n\nID NOT DETECTED\n!");
  1320.     }
  1321.     system("pause");
  1322. }
  1323.  
  1324.  
  1325.  
  1326. void deletePat()
  1327. {
  1328.     FILE *fpatient;
  1329.     fpatient = fopen("PATIENT.txt","r");
  1330.     if(fpatient==NULL)
  1331.     {
  1332.         printf("FILE CANNOT BE OPENED!\n");
  1333.         exit(1);
  1334.     }
  1335.     else
  1336.     {
  1337.         int line=0;
  1338.         char selection;
  1339.         line = searchMAIN();
  1340.         rewind(fpatient);
  1341.         if(line != -1)
  1342.         {
  1343.             printf("Patient ID:     %s\n",patientdets.pid);
  1344.             printf("Fullname  :     %s\n",patientdets.fullname);
  1345.             printf("Gender    :     %s\n",patientdets.gender);
  1346.             printf("Age       :     %s\n",patientdets.age);
  1347.             printf("Address   :     %s\n",patientdets.address);
  1348.             printf("Contact   :     %s\n",patientdets.contact);
  1349.             printf("Are you sure you wanted to delete this patient?\n");
  1350.             fflush(stdin);
  1351.             printf("CHOICE:");
  1352.             selection=getchar();
  1353.             if(toupper(selection)=='Y')
  1354.             {
  1355.                 char words[100];
  1356.                 char *buffer;
  1357.                 char *ptr;
  1358.                 int  read_line=0;
  1359.                 char user_input[100];
  1360.                 int  done=0;
  1361.  
  1362.                 buffer=(char *)malloc(1000*sizeof(char));
  1363.                 memset(buffer,0,1000*sizeof(char));
  1364.                 ptr=buffer;
  1365.  
  1366.                 strcat(patientdets.pid,"\n");
  1367.  
  1368.                 while(fgets(words,100,fpatient) != NULL)
  1369.                 {
  1370.                     read_line ++;
  1371.                     if(read_line != line)
  1372.                     {
  1373.                         strcpy(ptr,words);
  1374.                         ptr +=strlen(words);
  1375.                     }
  1376.                 }
  1377.                 fclose(fpatient);
  1378.  
  1379.                 fpatient=fopen("PATIENT.txt","w");
  1380.                 fprintf(fpatient,"%s",buffer);
  1381.                 fclose(fpatient);
  1382.             }
  1383.         }
  1384.     }
  1385.     system("pause");
  1386. }
  1387.  
  1388. int addApp()
  1389. {
  1390.     FILE *fapp;
  1391.     int found=-1;
  1392.     char selection;
  1393.  
  1394.     found = searchMAIN();
  1395.     if (found >= 0)
  1396.     {
  1397.         printf("Add appointment for this patient? (Y/N)\n");
  1398.         printf("Patient ID:     %s\n",patientdets.pid);
  1399.         printf("Fullname  :     %s\n",patientdets.fullname);
  1400.         printf("Gender    :     %s\n",patientdets.gender);
  1401.         printf("Age       :     %s\n",patientdets.age);
  1402.         printf("Address   :     %s\n",patientdets.address);
  1403.         printf("Contact   :     %s\n",patientdets.contact);
  1404.         fflush(stdin);
  1405.         scanf("%c",&selection);
  1406.         if(toupper(selection)=='Y')
  1407.         {
  1408.             fapp=fopen("APP.txt","a");
  1409.  
  1410.             strcpy(appdets.pid,patientdets.pid);
  1411.             fflush(stdin);
  1412.             strcpy(appdets.date,getDate());
  1413.             fflush(stdin);
  1414.             strcpy(appdets.time,getTime());
  1415.             fflush(stdin);
  1416.             strcpy(appdets.location,getLocation());
  1417.             fflush(stdin);
  1418.             strcpy(appdets.doctor,getDoctor());
  1419.             fflush(stdin);
  1420.             strcpy(appdets.apptype,getAppType());
  1421.             fprintf(fapp,"%s-->%s-->%s-->%s-->%s-->%s\n",appdets.pid,appdets.date,appdets.time,appdets.location,appdets.doctor,appdets.apptype);
  1422.             fclose(fapp);
  1423.         }
  1424.         else
  1425.         {
  1426.             printf("<-Back");
  1427.             return 1;
  1428.         }
  1429.     }
  1430.     return 0;
  1431.     system("pause");
  1432. }
  1433.  
  1434. void viewApp()
  1435. {
  1436.     system("cls");
  1437.     char buffer[1024]=" ";
  1438.     char s[4] = "-->";
  1439.     char *token;
  1440.     int counter = 1;
  1441.  
  1442.     FILE *fapp;
  1443.     fapp = fopen("APP.txt","r");
  1444.  
  1445.     if (fapp == NULL)
  1446.     {
  1447.         printf("ERROR:FILE CANNOT BE OPENED!\n");
  1448.     }
  1449.     else
  1450.     {
  1451.         if (feof(fapp))
  1452.         {
  1453.             printf("ERROR:END OF FILE!\n");
  1454.             fclose(fapp);
  1455.         }
  1456.         else
  1457.         {
  1458.             while( fgets(buffer,1024,fapp) != NULL )
  1459.             {
  1460.                 token = strtok(buffer, s);
  1461.                 strcpy(appdets.pid,token);
  1462.                 token = strtok(NULL,s);
  1463.                 strcpy(appdets.date,token);
  1464.                 token = strtok(NULL,s);
  1465.                 strcpy(appdets.time,token);
  1466.                 token = strtok(NULL,s);
  1467.                 strcpy(appdets.location,token);
  1468.                 token = strtok(NULL,s);
  1469.                 strcpy(appdets.doctor,token);
  1470.                 token = strtok(NULL,s);
  1471.                 strcpy(appdets.apptype,token);
  1472.                 token = strtok(NULL,s);
  1473.  
  1474.                 printf("Patient's ID: %s\n",appdets.pid);
  1475.                 printf("Appointment's Date: %s\n",appdets.date);
  1476.                 printf("Appointment's Time: %s\n",appdets.time);
  1477.                 printf("Appointment's Location: %s\n",appdets.location);
  1478.                 printf("Doctor In Charge: %s\n",appdets.doctor);
  1479.                 printf("Appointment's Type: %s\n",appdets.apptype);
  1480.                 printf("------------------------------------------------------\n");
  1481.             }
  1482.             fclose(fapp);
  1483.         }
  1484.     }
  1485.  
  1486.  
  1487. }
  1488.  
  1489. int searchApp()
  1490. {
  1491.     system("cls");
  1492.     char buffer[1024]=" ";
  1493.     char s[4] = "-->";
  1494.     char *token;
  1495.     int counter = 0;
  1496.     char tempID[100] ;
  1497.  
  1498.     FILE *fapp;
  1499.     fapp = fopen("APP.txt","r");
  1500.  
  1501.     if (fapp == NULL)
  1502.     {
  1503.         printf("ERROR:FILE CANNOT BE OPENED!\n");
  1504.     }
  1505.     else
  1506.     {
  1507.         if (feof(fapp))
  1508.         {
  1509.             printf("ERROR:END OF FILE!\n");
  1510.             fclose(fapp);
  1511.         }
  1512.         else
  1513.         {
  1514.             printf("ENTER ID: ");
  1515.             fflush(stdin);
  1516.             gets(tempID);
  1517.  
  1518.             while( fgets(buffer,1024,fapp) != NULL )
  1519.             {
  1520.                 counter ++ ;
  1521.                 token = strtok(buffer, s);
  1522.                 if ( strcmp(token,tempID)== 0)
  1523.                 {
  1524.                     strcpy(appdets.pid,token);
  1525.                     token = strtok(NULL,s);
  1526.                     strcpy(appdets.date,token);
  1527.                     token = strtok(NULL,s);
  1528.                     strcpy(appdets.time,token);
  1529.                     token = strtok(NULL,s);
  1530.                     strcpy(appdets.location,token);
  1531.                     token = strtok(NULL,s);
  1532.                     strcpy(appdets.doctor,token);
  1533.                     token = strtok(NULL,s);
  1534.                     strcpy(appdets.apptype,token);
  1535.                     token = strtok(NULL,s);
  1536.  
  1537.                     printf("Patient's ID:           %s\n",appdets.pid);
  1538.                     printf("Appointment's Date:     %s\n",appdets.date);
  1539.                     printf("Appointment's Time:     %s\n",appdets.time);
  1540.                     printf("Appointment's Location: %s\n",appdets.location);
  1541.                     printf("Doctor In Charge:       %s\n",appdets.doctor);
  1542.                     printf("Appointment's Type:     %s\n",appdets.apptype);
  1543.                     printf("------------------------------------------------------\n");
  1544.  
  1545.                     fclose(fapp);
  1546.                     return counter;
  1547.                 }
  1548.             }
  1549.  
  1550.         }
  1551.     }
  1552.     printf("Can't find appointment");
  1553.     system("pause");
  1554.  
  1555.     fclose(fapp);
  1556.     return counter;
  1557.  
  1558. }
  1559.  
  1560. void deleteApp()
  1561. {
  1562.     system("cls");
  1563.     FILE *fapp;
  1564.     FILE *fpatient;
  1565.     fapp = fopen("APP.txt","r");
  1566.     char buffer[1024]=" ";
  1567.     char s[4] = "-->";
  1568.     char *token;
  1569.     int counter = 1;
  1570.     char tempID[100];
  1571.     if(fapp==NULL)
  1572.     {
  1573.         printf("FILE CANNOT BE OPENED!\n");
  1574.         exit(1);
  1575.     }
  1576.     else
  1577.     {
  1578.         char selection;
  1579.         printf("ENTER ID: ");
  1580.         fflush(stdin);
  1581.         gets(tempID);
  1582.  
  1583.         while(fgets(buffer,1024,fapp) != NULL)
  1584.         {
  1585.             token = strtok(buffer, s);
  1586.             if ( strcmp(token,tempID)== 0)
  1587.             {
  1588.                 strcpy(appdets.pid,token);
  1589.                 token = strtok(NULL,s);
  1590.                 strcpy(appdets.date,token);
  1591.                 token = strtok(NULL,s);
  1592.                 strcpy(appdets.time,token);
  1593.                 token = strtok(NULL,s);
  1594.                 strcpy(appdets.location,token);
  1595.                 token = strtok(NULL,s);
  1596.                 strcpy(appdets.doctor,token);
  1597.                 token = strtok(NULL,s);
  1598.                 strcpy(appdets.apptype,token);
  1599.                 token = strtok(NULL,s);
  1600.  
  1601.  
  1602.                 printf("Patient's ID:           %s\n",appdets.pid);
  1603.                 printf("Appointment's Date:     %s\n",appdets.date);
  1604.                 printf("Appointment's Time:     %s\n",appdets.time);
  1605.                 printf("Appointment's Location: %s\n",appdets.location);
  1606.                 printf("Doctor In Charge:       %s\n",appdets.doctor);
  1607.                 printf("Appointment's Type:     %s\n",appdets.apptype);
  1608.                 printf("------------------------------------------------------\n");
  1609.                 printf("Are you sure you wanted to delete this entry?\n");
  1610.                 fflush(stdin);
  1611.                 printf("CHOICE:");
  1612.                 selection=getchar();
  1613.                 if(toupper(selection)=='Y')
  1614.                 {
  1615.                     char words[100];
  1616.                     char *buffer;
  1617.                     char *ptr;
  1618.                     int  read_line=0;
  1619.                     char user_input[100];
  1620.                     int  done=0;
  1621.  
  1622.                     buffer=(char *)malloc(1000*sizeof(char));
  1623.                     memset(buffer,0,1000*sizeof(char));
  1624.                     ptr=buffer;
  1625.  
  1626.                     strcat(appdets.pid,"\n");
  1627.  
  1628.                     while(fgets(words,100,fapp) != NULL)
  1629.                     {
  1630.                         read_line ++;
  1631.                         if(read_line != 0)
  1632.                         {
  1633.                             strcpy(ptr,words);
  1634.                             ptr +=strlen(words);
  1635.                         }
  1636.                     }
  1637.                     fclose(fapp);
  1638.  
  1639.                     fpatient=fopen("APP.txt","w");
  1640.                     fprintf(fapp,"%s",buffer);
  1641.                     fclose(fapp);
  1642.                 }
  1643.             }
  1644.         }
  1645.     }
  1646. }
  1647.  
  1648. void editApp()
  1649. {
  1650.     FILE *fapp;
  1651.     FILE *fTemp;
  1652.     int found=0,choice=0,line=0,i=0;
  1653.     char selection=0,ch;
  1654.     char buff[512]="" , buff2[512]="";
  1655.     found = searchApp();
  1656.     if (found != -1)
  1657.     {
  1658.         fflush(stdin);
  1659.         system("cls");
  1660.         printf("Patient's ID:           %s\n",appdets.pid);
  1661.                 printf("Appointment's Date:     %s\n",appdets.date);
  1662.                 printf("Appointment's Time:     %s\n",appdets.time);
  1663.                 printf("Appointment's Location: %s\n",appdets.location);
  1664.                 printf("Doctor In Charge:       %s\n",appdets.doctor);
  1665.                 printf("Appointment's Type:     %s\n",appdets.apptype);
  1666.                 printf("------------------------------------------------------\n");
  1667.                 printf("Are you sure you wanted to edit this appointment?\n");
  1668.         printf("CHOICE:");
  1669.         scanf("%c",&selection);
  1670.         if(toupper(selection)== 'Y')
  1671.         {
  1672.             fapp=fopen("APP.txt","a+");
  1673.             fTemp=fopen("EditAPP.txt","w");
  1674.             while(!feof(fapp))
  1675.             {
  1676.                 ch = fgetc(fapp);
  1677.                 if(ch == '\n')
  1678.                     i++;
  1679.             }
  1680.  
  1681.  
  1682.             printf("(1)Date\n");printf("(2)Time\n");printf("(3)Location\n");printf("(4)Doctor\n");printf("(5)Apptype\n");printf("(0)Back\n");
  1683.             printf("Select option to be modified:");
  1684.             scanf("%d",&choice);
  1685.             switch(choice)
  1686.             {
  1687.                 case 1:
  1688.                 {
  1689.                     fflush(stdin);
  1690.                     strcpy(appdets.date,getDate());
  1691.                     break;
  1692.                 }
  1693.                 case 2:
  1694.                 {
  1695.                     fflush(stdin);
  1696.                     strcpy(appdets.time,getTime());
  1697.                     break;
  1698.                 }
  1699.                 case 3:
  1700.                 {
  1701.                     fflush(stdin);
  1702.                     strcpy(appdets.location,getLocation());
  1703.                     break;
  1704.                 }
  1705.                 case 4:
  1706.                 {
  1707.                     fflush(stdin);
  1708.                     strcpy(appdets.doctor,getDoctor());
  1709.                     break;
  1710.                 }
  1711.                 case 5:
  1712.                 {
  1713.                     fflush(stdin);
  1714.                     strcpy(appdets.apptype,getAppType());
  1715.                     break;
  1716.                 }
  1717.  
  1718.             }
  1719.             strcat(buff, appdets.pid);strcat(buff,"-->");
  1720.             strcat(buff, appdets.date);strcat(buff, "-->");
  1721.             strcat(buff, appdets.time);strcat(buff, "-->");
  1722.             strcat(buff, appdets.location);strcat(buff, "-->");
  1723.             strcat(buff, appdets.doctor);strcat(buff,"-->");
  1724.             strcat(buff, appdets.apptype);
  1725.  
  1726.             rewind(fapp);
  1727.             while (line<i)
  1728.             {
  1729.                 line++;
  1730.                 if(found == line)
  1731.                 {
  1732.                     fputs(buff,fTemp);
  1733.                     fgets(buff2,512,fapp); // store current line into buffer2
  1734.                 }
  1735.                 else
  1736.                 {
  1737.                     fgets(buff2,512,fapp);
  1738.                     fputs(buff2,fTemp);
  1739.                 }
  1740.             }
  1741.             fclose(fapp);
  1742.             fclose(fTemp);
  1743.             remove("APP.txt");
  1744.             rename("EditApp.txt","APP.txt");
  1745.         }
  1746.         else
  1747.         {
  1748.             printf("SEE YOU AGAIN!");
  1749.             return;
  1750.             system("pause");
  1751.         }
  1752.     }
  1753.     else
  1754.     {
  1755.         printf("\n\nID NOT DETECTED\n!");
  1756.     }
  1757.  
  1758. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement