Advertisement
Guest User

Untitled

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