Advertisement
Guest User

Untitled

a guest
Dec 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. /**********************************************************************
  2. * prog1nurse.c
  3. * A program that allows only 5 nurses to sign in. Once logged on the
  4. program allows patient files to be created and nurses passwords to
  5. be changed.
  6. * December 2017
  7. **********************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12.  
  13. /* Function declarations */
  14. void emptyBuffer(void);
  15. void printLine(void);
  16. int nurseLogin();
  17. void createPatientFile
  18. (char *patientLastName, char *patientDay,
  19. char *patientMonth, char *patientYear,
  20. char *patientsFileName);
  21. int mainMenu();
  22. void decryptString(char dest[], char source[]);
  23. void encryptString(char dest[], char source[]);
  24. void addOtherPatient();
  25. void changeStringToChar(char *encrypted, char *source, int change);
  26. void alterPassword(int login);
  27.  
  28. int main()
  29. {
  30. int choice;
  31. int nurse = -1;
  32. while(nurse == -1)
  33. {
  34. nurse = nurseLogin();
  35. }
  36.  
  37.  
  38. while(choice != 3)
  39. {
  40. choice = mainMenu();
  41. switch(choice)
  42. {
  43. case 1:
  44. addOtherPatient();
  45. break;
  46. case 2:
  47. alterPassword(nurse);
  48. break;
  49. case 3:
  50. printf("Make sure to visit soon!");
  51. break;
  52. }
  53. }
  54.  
  55. return 0;
  56. }
  57.  
  58. /* function that requires the user to enter valid username and
  59. password, which is then checked with files*/
  60.  
  61. int nurseLogin()
  62. {
  63. FILE *filesPassword;
  64. int i;
  65. char nurseUsername[9];
  66. char nursePassword[9];
  67. char filesUsername[9];
  68. char filePassword[9];
  69.  
  70. printf("=======================================================\n");
  71. printf(" ACTION ON WEIGHT\n");
  72. printf("=======================================================\n");
  73.  
  74. printf("\n *// Nurse Login //*\n\n");
  75. // displayTitle("\n *// NURSE LOGIN //*\n");
  76. printf("Please enter your Username: \n");
  77. scanf("%9[^\n]", nurseUsername);
  78. emptyBuffer();
  79. printf("Please enter your Password: \n");
  80. scanf("%9[^\n]", nursePassword);
  81. emptyBuffer();
  82.  
  83. //encryptString(nurseUsername, nurseUsername);
  84. //encryptString(nursePassword, nursePassword);
  85.  
  86. filesPassword = fopen("passwords.txt", "r");
  87.  
  88. while(0<fscanf(filesPassword, " %[^\t] %[^\n]", filesUsername,
  89. filePassword))
  90. {
  91.  
  92.  
  93. if((strcmp(filesUsername, nurseUsername) == 0)
  94. && (strcmp(filePassword, nursePassword) == 0))
  95. {
  96. fclose(filesPassword);
  97. return i;
  98. }
  99. ++i;
  100. }
  101.  
  102. fclose(filesPassword);
  103. puts("\n|| The password or username you entered seems to be\n"
  104. "incorrect, please try again ||\n");
  105.  
  106. return -1;
  107. }
  108.  
  109. /* Function that displays the main menu options to the user */
  110. int mainMenu()
  111. {
  112. int menuChoice;
  113.  
  114. printLine();
  115. printf("\n *// Nurse Main Menu //*\n\n");
  116. printLine();
  117. printf("\n1. Add a patient file\n");
  118. printf("2. Change password\n");
  119. printf("3. Exit\n");
  120.  
  121. printf("\nPlease selection an option: ");
  122.  
  123. while(((scanf(" %d", &menuChoice)) == 0) ||
  124. (menuChoice < 1) || (menuChoice > 3))
  125. {
  126. emptyBuffer();
  127. printf("Please select an appropiate number(1-3)! \n");
  128. printf("Re-enter number: ");
  129. }
  130. emptyBuffer();
  131.  
  132. return menuChoice;
  133. }
  134.  
  135. /* function that allows the user to enter a new patients details */
  136.  
  137. void addOtherPatient(void)
  138. {
  139. FILE *patientDetails;
  140. char patientFileName[20];
  141. char patientsFirstName[20];
  142. char patientsSecondName[20];
  143. char patientsDateOfBirth[3][20];
  144. char patientsHeight[10];
  145. char patientsWaist[10];
  146. char patientsWeight[10];
  147. char patientsComment[500];
  148.  
  149. printLine();
  150. printf(" Add a patient to a file!\n");
  151. printLine();
  152.  
  153. printf("Patients First Name: ");
  154. scanf("%[^\n]", &patientsFirstName);
  155. emptyBuffer();
  156.  
  157. printf("Patients Second Name: ");
  158. scanf("%[^\n]", &patientsSecondName);
  159. emptyBuffer();
  160.  
  161. printf("Patients DOB Day: ");
  162. scanf("%[^\n]", &patientsDateOfBirth[0]);
  163. emptyBuffer();
  164.  
  165. printf("Patients DOB Month: ");
  166. scanf("%[^\n]", &patientsDateOfBirth[1]);
  167. emptyBuffer();
  168.  
  169. printf("Patients DOB Year: ");
  170. scanf("%[^\n]", &patientsDateOfBirth[2]);
  171. emptyBuffer();
  172.  
  173. printf("Patients Height: ");
  174. scanf("%[^\n]", &patientsHeight);
  175. emptyBuffer();
  176.  
  177. printf("Patients Waist Measurement: ");
  178. scanf("%[^\n]", &patientsWaist);
  179. emptyBuffer();
  180.  
  181. printf("Patients Weight: ");
  182. scanf("%[^\n]", &patientsWeight);
  183. emptyBuffer();
  184.  
  185. printf("Extra Comments: ");
  186. scanf("%[^\n]", &patientsComment);
  187. emptyBuffer();
  188.  
  189. /* Function to create the file name */
  190. createPatientFile(patientsSecondName, patientsDateOfBirth[0], patientsDateOfBirth[1],
  191. patientsDateOfBirth[2], patientFileName);
  192.  
  193. /*Encrypt information of the patients*/
  194. encryptString(patientsFirstName, patientsFirstName);
  195. encryptString(patientsSecondName, patientsSecondName);
  196. encryptString(patientsDateOfBirth[0], patientsDateOfBirth[0]);
  197. encryptString(patientsDateOfBirth[1], patientsDateOfBirth[1]);
  198. encryptString(patientsDateOfBirth[2], patientsDateOfBirth[2]);
  199. encryptString(patientsHeight, patientsHeight);
  200. encryptString(patientsWaist, patientsWaist);
  201. encryptString(patientsWeight, patientsWeight);
  202. encryptString(patientsComment, patientsComment);
  203.  
  204. patientDetails = fopen(patientFileName, "w");
  205.  
  206. fprintf(patientDetails, "%s\n", patientsFirstName);
  207. fprintf(patientDetails, "%s\n", patientsSecondName);
  208.  
  209. fprintf(patientDetails, "%s/%s/%s\n", patientsDateOfBirth[0],
  210. patientsDateOfBirth[1], patientsDateOfBirth[2]);
  211.  
  212. fprintf(patientDetails, "%s\n", patientsHeight);
  213. fprintf(patientDetails, "%s\n", patientsWaist);
  214. fprintf(patientDetails, "%s\n", patientsWeight);
  215. fprintf(patientDetails, "%s\n", patientsComment);
  216.  
  217. fclose(patientDetails);
  218.  
  219. printLine();
  220. puts("Patient successfully added!");
  221. }
  222.  
  223. /* Function which creates a file name for patients based on their
  224. last name and their date of birth */
  225.  
  226. void createPatientFile(char *patientLastName, char *patientDay, char *patientMonth,
  227. char *patientYear, char *patientsFileName)
  228. {
  229. int i;
  230. char copyYearLastTwoDigits[3];
  231.  
  232. strcpy(patientsFileName, "Patients/");
  233.  
  234. strcat(patientsFileName, patientLastName);
  235. strcat(patientsFileName, patientDay);
  236. strcat(patientsFileName, patientMonth);
  237.  
  238. memcpy(copyYearLastTwoDigits, &patientYear[2], 2);
  239.  
  240. copyYearLastTwoDigits[2] = '\0';
  241.  
  242. strcat(patientsFileName, copyYearLastTwoDigits);
  243.  
  244. strcat(patientsFileName, ".aow");
  245. }
  246.  
  247. /*A function which takes a string and transforms it's characters*/
  248. void changeStringToChar(char *encrypted, char *source, int change)
  249. {
  250. int strLen = strlen(source);
  251.  
  252. int i;
  253. int ch;
  254.  
  255. for(i = 0; i < strLen; ++i)
  256. {
  257. ch = source[i];
  258.  
  259. if(ch >= 'a' && ch <= 'z')
  260. {
  261. ch += change;
  262.  
  263. if(ch > 'z')
  264. {
  265. ch = 'a' + (ch - 'z') - 1;
  266. }
  267.  
  268. else if(ch < 'a')
  269. {
  270. ch = 'z' + (ch - 'a') + 1;
  271. }
  272. }
  273.  
  274. else if(ch >= 'A' && ch <= 'Z')
  275. {
  276. ch += change;
  277.  
  278. if(ch > 'Z')
  279. {
  280. ch = 'A' + (ch - 'Z') - 1;
  281. }
  282. else if(ch < 'A')
  283. {
  284. ch = 'Z' + (ch - 'A') + 1;
  285. }
  286. }
  287.  
  288. else if(ch >= '0' && ch <= '9')
  289. {
  290. ch += change;
  291.  
  292. if(ch > '9')
  293. {
  294. ch = '0' + (ch - '9') - 1;
  295. }
  296.  
  297. else if(ch < '0')
  298. {
  299. ch = '9' + (ch - '0') + 1;
  300. }
  301. }
  302. encrypted[i] = ch;
  303. }
  304. encrypted[strLen] = '\0';
  305.  
  306. }
  307. /*A function for encryption which uses transform function*/
  308. void encryptString(char dest[], char source[])
  309. {
  310. changeStringToChar(dest, source, +7);
  311. }
  312.  
  313. /*A function for decryption which uses transform function*/
  314. void decryptString(char dest[], char source[])
  315. {
  316. changeStringToChar(dest, source, -7);
  317. }
  318.  
  319. /*A function which allows a nurse to change a password within
  320. a specified array location depending which nurse is logged in*/
  321. void alterPassword(int login)
  322. {
  323. FILE *passwordFileRead;
  324. FILE *passwordFileWrite;
  325.  
  326. char newPassword[10];
  327. char reinforcePassword[10];
  328.  
  329. printLine();
  330. printf(" Change your password!\n");
  331. printLine();
  332.  
  333. printf("Please enter your new password:\n");
  334. scanf("%[^\n]", newPassword);
  335. emptyBuffer();
  336.  
  337. if(strlen(newPassword) != 8)
  338. {
  339. printf("Password must be 8 characters long! \n");
  340. return;
  341. }
  342. printf("Please confirm your new password: \n");
  343. scanf("%[^\n]", reinforcePassword);
  344. emptyBuffer();
  345.  
  346. /*function to compare two password strings to see if they
  347. match*/
  348. if(strcmp(newPassword, reinforcePassword) == 0)
  349. {
  350. char encryptedPassword[10];
  351.  
  352. encryptString(reinforcePassword, encryptedPassword);
  353.  
  354. char fileUsernames[5][10];
  355. char filePasswords[5][10];
  356.  
  357. passwordFileRead = fopen("passwords.txt", "r");
  358. int i = 0;
  359.  
  360. while(2==fscanf(passwordFileRead, " %[^\t] %[^\n]",
  361. fileUsernames[i], filePasswords[i]))
  362. {
  363. i++;
  364. }
  365.  
  366. fclose(passwordFileRead);
  367.  
  368. passwordFileWrite = fopen("passwords.txt", "w");
  369.  
  370. strcpy(filePasswords[login], encryptedPassword);
  371.  
  372. for (i = 0; i < 5; ++i)
  373. {
  374. fprintf(passwordFileWrite, "%s\t%s\n", fileUsernames[i],
  375. filePasswords[i]);
  376. }
  377.  
  378. fclose(passwordFileWrite);
  379.  
  380. printf("\nPassword successfully changed!\n");
  381. return;
  382. }
  383. else
  384. {
  385. puts("Your password did not match, please try again !");
  386. return;
  387. }
  388. }
  389. /*Function that asks user to press any key to continue */
  390. void printLine(void)
  391. {
  392. printf("-------------------------------------------------------\n");
  393.  
  394. }
  395.  
  396. /*Function to empty the buffer*/
  397. void emptyBuffer(void)
  398. {
  399. while(getchar() != '\n')
  400. {
  401. ;
  402. }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement