Advertisement
Guest User

Untitled

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