Advertisement
Guest User

prog1

a guest
Dec 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.36 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. #include <unistd.h>
  14.  
  15.  
  16.  
  17. /*Function prototypes*/
  18. void emptyBuffer(void);
  19. int nurseLogin();
  20. void createPatientFile
  21. (char *patientLastName, char *patientDay,
  22. char *patientMonth, char *patientYear,
  23. char *patientsFileName);
  24. void displayTitle(char *title);
  25. int mainMenu();
  26. void waitForKey(void);
  27. void decryptString(char dest[], char source[]);
  28. void encryptString(char dest[], char source[]);
  29. void addOtherPatient();
  30. void changeString(char encrypted[], char source[], int change);
  31. char toLower(char any);
  32. void alterPassword(int);
  33.  
  34. int main()
  35. {
  36. int choice;
  37. int nurse = -1;
  38. while(nurse == -1)
  39. {
  40. nurse = nurseLogin();
  41. }
  42.  
  43.  
  44. while(choice != 3)
  45. {
  46. choice = mainMenu();
  47. switch(choice)
  48. {
  49. case 1:
  50. /*Call addOtherPatient function*/
  51. addOtherPatient();
  52. break;
  53. case 2:
  54. /*Call alterPassword function*/
  55. alterPassword(nurse);
  56. break;
  57. case 3:
  58. /*Clear the contents and display a closing message*/
  59. printf("See you soon!");
  60. break;
  61. }
  62. }
  63.  
  64. return 0;
  65. }
  66. /* function that requires the user to enter valid username and
  67. password */
  68.  
  69. int nurseLogin()
  70. {
  71. FILE *filesPassword;
  72. int i;
  73. int nurse;
  74. char nurseUsername[9];
  75. char nursePassword[9];
  76. char filesUsername[9];
  77. char filePassword[9];
  78.  
  79.  
  80. displayTitle(" // NURSE LOGIN //");
  81. printf("Please enter your Username: ");
  82. scanf("%9[^\n]", nurseUsername);
  83. emptyBuffer();
  84. printf("Please enter your Password: ");
  85. scanf("%9[^\n]", nursePassword);
  86. emptyBuffer();
  87.  
  88. //encryptString(nurseUsername, nurseUsername);
  89. //encryptString(nursePassword, nursePassword);
  90.  
  91. filesPassword = fopen("passwords.txt", "r");
  92.  
  93. while(0<fscanf(filesPassword, " %[^\t] %[^\n]", filesUsername,
  94. filePassword))
  95. {
  96.  
  97.  
  98. if((strcmp(filesUsername, nurseUsername) == 0)
  99. && (strcmp(filePassword, nursePassword) == 0))
  100. {
  101. fclose(filesPassword);
  102. return i;
  103. }
  104. ++i;
  105. }
  106.  
  107. fclose(filesPassword);
  108. puts("\n You entered the wrong username or password, please try "
  109. "again!\n");
  110. waitForKey();
  111.  
  112. return -1;
  113. }
  114. /*Main menu function which displays main menu contents including
  115. a header and returns user's input*/
  116. int mainMenu()
  117. {
  118. int choice;
  119. /*Displaying menu list choices*/
  120. displayTitle("\t\t || NURSE MENU ||");
  121. puts("1) Add a patient");
  122. puts("2) Change password");
  123. puts("3) Exit");
  124.  
  125. printf(">>> ");
  126.  
  127. /*If the input is less than 1 or higher than 3 then
  128. display an error message*/
  129. while(((scanf(" %d", &choice)) == 0) ||
  130. (choice < 1) || (choice > 3))
  131. {
  132. emptyBuffer();
  133. puts("!! Please make a valid choice !!");
  134. printf(">>> ");
  135. }
  136. emptyBuffer();
  137.  
  138. return choice;
  139. }
  140.  
  141. void addOtherPatient(void)
  142. {
  143. /*Declaring variables for iteriation and patient details, using
  144. 2d array to store date of birth*/
  145. FILE *patientDetails;
  146. char patientFileName[265];
  147. char patientsFirstName[256];
  148. char patientsLastName[256];
  149. char patientsDateOfBirth[3][256];
  150. char patientsHeight[256];
  151. char patientsWaist[256];
  152. char patientsWeight[256];
  153. char patientsComment[512];
  154.  
  155.  
  156. /*Displaying the header*/
  157. displayTitle("\t\t || ADD A PATIENT ||");
  158.  
  159. /*Storing non-encryped patient details into [0]*/
  160. printf("First Name: ");
  161. scanf("%[^\n]", &patientsFirstName);
  162. emptyBuffer();
  163.  
  164. printf("Last Name: ");
  165. scanf("%[^\n]", &patientsLastName);
  166. emptyBuffer();
  167.  
  168. printf("DOB Day [DD]: ");
  169. scanf("%[^\n]", &patientsDateOfBirth[0]);
  170. emptyBuffer();
  171.  
  172. printf("DOB Month [MM]: ");
  173. scanf("%[^\n]", &patientsDateOfBirth[1]);
  174. emptyBuffer();
  175.  
  176. printf("DOB Year [YYYY]: ");
  177. scanf("%[^\n]", &patientsDateOfBirth[2]);
  178. emptyBuffer();
  179.  
  180. printf("Height [cm]: ");
  181. scanf("%[^\n]", &patientsHeight);
  182. emptyBuffer();
  183.  
  184. printf("Waist measurement [inches]: ");
  185. scanf("%[^\n]", &patientsWaist);
  186. emptyBuffer();
  187.  
  188. printf("Weight [kg]: ");
  189. scanf("%[^\n]", &patientsWeight);
  190. emptyBuffer();
  191.  
  192. printf("Comment: ");
  193. scanf("%[^\n]", &patientsComment);
  194. emptyBuffer();
  195.  
  196. /*Creating file name using the createPatientFile function*/
  197. createPatientFile(patientsLastName, patientsDateOfBirth[0], patientsDateOfBirth[1],
  198. patientsDateOfBirth[2], patientFileName);
  199.  
  200. /*Encrypting patient's details*/
  201. encryptString(patientsFirstName, patientsFirstName);
  202. encryptString(patientsLastName, patientsLastName);
  203. encryptString(patientsDateOfBirth[0], patientsDateOfBirth[0]);
  204. encryptString(patientsDateOfBirth[1], patientsDateOfBirth[1]);
  205. encryptString(patientsDateOfBirth[2], patientsDateOfBirth[2]);
  206. encryptString(patientsHeight, patientsHeight);
  207. encryptString(patientsWaist, patientsWaist);
  208. encryptString(patientsWeight, patientsWeight);
  209. encryptString(patientsComment, patientsComment);
  210.  
  211. /*Opening the file for writing purposes*/
  212. patientDetails = fopen(patientFileName, "w");
  213.  
  214. /*Writing user to a file*/
  215. fprintf(patientDetails, "%s\n", patientsFirstName);
  216. fprintf(patientDetails, "%s\n", patientsLastName);
  217.  
  218. fprintf(patientDetails, "%s/%s/%s\n", patientsDateOfBirth[0],
  219. patientsDateOfBirth[1], patientsDateOfBirth[2]);
  220.  
  221. fprintf(patientDetails, "%s\n", patientsHeight);
  222. fprintf(patientDetails, "%s\n", patientsWaist);
  223. fprintf(patientDetails, "%s\n", patientsWeight);
  224. fprintf(patientDetails, "%s\n", patientsComment);
  225.  
  226. /*Closing the file*/
  227. fclose(patientDetails);
  228.  
  229. printf("-------------------------------------------------------\n");
  230. puts("Patient has been added!");
  231. /*Display continue message using waitForKey function*/
  232. waitForKey();
  233. }
  234.  
  235. /*Function that takes patient's last name and DOB input and returns
  236. a file name for the patient*/
  237. void createPatientFile(char *patientLastName, char *patientDay, char *patientMonth,
  238. char *patientYear, char *patientsFileName)
  239. {
  240. int i;
  241. char copyYearLastTwoDigits[3];
  242.  
  243. strcpy(patientsFileName, "Patients/");
  244.  
  245. /*Adding last name, day and month to a file name*/
  246. strcat(patientsFileName, patientLastName);
  247. strcat(patientsFileName, patientDay);
  248. strcat(patientsFileName, patientMonth);
  249.  
  250. /*Copy two last characters from the patient year of birth and store
  251. it to the variable*/
  252. memcpy(copyYearLastTwoDigits, &patientYear[2], 2);
  253.  
  254. /*Adding \0 to the variable to make it a string*/
  255. copyYearLastTwoDigits[2] = '\0';
  256.  
  257. /*Adding last two DOB digits to the file name*/
  258. strcat(patientsFileName, copyYearLastTwoDigits);
  259. /*Add the .aow extension to the file name*/
  260. strcat(patientsFileName, ".aow");
  261.  
  262. /*Iterating trough a file name*/
  263. for(i = 0; patientsFileName[i]; ++i)
  264. {
  265. /*Changing file first character to lowercase*/
  266. patientsFileName[i] = toLower(patientsFileName[i]);
  267. }
  268.  
  269. }
  270. /*A function which takes a string and transforms it's characters*/
  271. void changeString(char encrypted[], char source[], int change)
  272. {
  273. /*storing length of the string to encrypt*/
  274. int strLen = strlen(source);
  275.  
  276. int i;
  277. int ch;
  278.  
  279. /*Iterate trough the string*/
  280. for(i = 0; i < strLen; ++i)
  281. {
  282. /*Assign one character of the string to
  283. the character variable*/
  284. ch = source[i];
  285.  
  286. /*If the character is between a and z*/
  287. if(ch >= 'a' && ch <= 'z')
  288. {
  289. /*Add change(7) to the character*/
  290. ch += change;
  291.  
  292. /*If character is out of the range of z*/
  293. if(ch > 'z')
  294. {
  295. /*Get the difference between the character a and z,
  296. subtract 1 and add it to the a value*/
  297. ch = 'a' + (ch - 'z') - 1;
  298. }
  299. /*If the character is out of the range of a*/
  300. else if(ch < 'a')
  301. {
  302. /*Get the difference between the character z and a,
  303. add 1 and add it to the z value*/
  304. ch = 'z' + (ch - 'a') + 1;
  305. }
  306. }
  307. /*If the character is between A and Z*/
  308. else if(ch >= 'A' && ch <= 'Z')
  309. {
  310. /*Add change(7) to the character*/
  311. ch += change;
  312.  
  313. /*If character is out of the range of Z*/
  314. if(ch > 'Z')
  315. {
  316. /*Get the difference between the character A and Z,
  317. subtract 1 and add it to the A value*/
  318. ch = 'A' + (ch - 'Z') - 1;
  319. }
  320. /*If the character is out of the range of A*/
  321. else if(ch < 'A')
  322. {
  323. /*Get the difference between the character Z and A,
  324. add 1 and add it to the Z value*/
  325. ch = 'Z' + (ch - 'A') + 1;
  326. }
  327. }
  328. /*If the character is between 0 and 9*/
  329. else if(ch >= '0' && ch <= '9')
  330. {
  331. //Add change(7) to the character
  332. ch += change;
  333.  
  334. /*If character is out of the range of 9*/
  335. if(ch > '9')
  336. {
  337. /*Get the difference between the character 0 and 9,
  338. subtract 1 and add it to the 9 value*/
  339. ch = '0' + (ch - '9') - 1;
  340. }
  341. /*If character is out of the range of 0*/
  342. else if(ch < '0')
  343. {
  344. /*Get the difference between the character 9 and 0,
  345. add 1 and add it to the 0 value*/
  346. ch = '9' + (ch - '0') + 1;
  347. }
  348. }
  349. /*Store the encrypted character in the encrypted array*/
  350. encrypted[i] = ch;
  351. }
  352. /*Add \0 to the encrypted array to make it a string*/
  353. encrypted[strLen] = '\0';
  354.  
  355. }
  356. /*A function for encryption which uses transform function*/
  357. void encryptString(char dest[], char source[])
  358. {
  359. /*Transforming character and moving it forwards by seven places*/
  360. changeString(dest, source, +7);
  361. }
  362. /*A function for decryption which uses transform function*/
  363. void decryptString(char dest[], char source[])
  364. {
  365. /*Transforming character and moving it backwards by seven places*/
  366. changeString(dest, source, -7);
  367. }
  368. /*A function which allows a nurse to change a password within
  369. a specified array location depending which nurse is logged in*/
  370. void alterPassword(int nurse)
  371. {
  372. FILE *passwordFileRead;
  373. FILE *passwordFileWrite;
  374.  
  375. char newPassword[10];
  376. char reinforcePassword[10];
  377.  
  378. /*Displaying the header*/
  379. displayTitle("\t\t || CHANGE PASSWORD ||");
  380.  
  381. puts("Enter your new password:");
  382. printf(">>> ");
  383. /*Receiving user first password input*/
  384. scanf("%[^\n]", newPassword);
  385. emptyBuffer();
  386. /*If password string is not equal to 8 characters then display
  387. an an appropiate error*/
  388. if(strlen(newPassword) != 8)
  389. {
  390. puts("!! Password MUST be 8 characters long !!");
  391. waitForKey();
  392. return;
  393. }
  394. puts("Confirm your new password:");
  395. printf(">>> ");
  396.  
  397. /*Receiving user second password input*/
  398. scanf("%[^\n]", reinforcePassword);
  399. emptyBuffer();
  400.  
  401. /*Compaing the two password strings to see if they match*/
  402. if(strcmp(newPassword, reinforcePassword) == 0)
  403. {
  404. char encryptedPassword[10];
  405.  
  406. /*Encrypting the new password*/
  407. encryptString(reinforcePassword, encryptedPassword);
  408.  
  409. char allPasswords[5][10];
  410. char allUsernames[5][10];
  411.  
  412. /*Opening file for reading purposes*/
  413. passwordFileRead = fopen("passwords.txt", "r");
  414. int i = 0;
  415.  
  416. /*Read all usernames and passwords*/
  417. while(2==fscanf(passwordFileRead, " %[^\t] %[^\n]",
  418. allUsernames[i], allPasswords[i]))
  419. {
  420. i++;
  421. }
  422.  
  423. /*Closing the file*/
  424. fclose(passwordFileRead);
  425. /*Opening the file for writing purposes*/
  426. passwordFileWrite = fopen("passwords.txt", "w");
  427. /*Overwriting an old password with a new password string*/
  428. strcpy(allPasswords[nurse], encryptedPassword);
  429.  
  430. for (i = 0; i < 5; ++i)
  431. {
  432. /*Writing new strings to a file*/
  433. fprintf(passwordFileWrite, "%s\t%s\n", allUsernames[i],
  434. allPasswords[i]);
  435. }
  436. /*Closing the file*/
  437. fclose(passwordFileWrite);
  438.  
  439. printf("---------------------------------------------------\n");
  440. puts("Password has been changed!");
  441. waitForKey();
  442. return;
  443. }
  444. else
  445. {
  446. puts("!! Your password did not match, please try again !!");
  447. waitForKey();
  448. return;
  449. }
  450. }
  451. /*Function that display the header*/
  452. void displayTitle(char *header)
  453. {
  454. puts("=========================================================");
  455. printf(" ACTION ON WEIGHT\n");
  456. puts("========================================================");
  457. printf(header);
  458. printf("\n");
  459. }
  460.  
  461. /*Function that displays some ending text and waits for user
  462. to press any key*/
  463. void waitForKey(void)
  464. {
  465. puts("=========================================================");
  466. printf("Press the Enter Key to Continue...\n");
  467. puts("=========================================================");
  468. getchar();
  469.  
  470. }
  471.  
  472. /*Function that emptys the buffer*/
  473. void emptyBuffer(void)
  474. {
  475. while(getchar() != '\n')
  476. {
  477. ;
  478. }
  479. }
  480. char toLower(char any)
  481. {
  482. return tolower(any);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement