Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************
- * nurseprogram.c *
- * A program with intergrated login system that let's a nurse write *
- * patient details to a file and has an option to change password. *
- * Written details are then encrypted to a file. *
- * Candidate NO: 181452 *
- * December 2017 *
- **********************************************************************/
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <unistd.h>
- /*Function prototypes*/
- void emptyBuffer(void);
- int nurseLogin();
- void createPatientFile
- (char *patientLastName, char *patientDay,
- char *patientMonth, char *patientYear,
- char *patientsFileName);
- void displayTitle(char *title);
- void equalsLine(void);
- int mainMenu();
- void waitForKey(void);
- void decryptString(char dest[], char source[]);
- void encryptString(char dest[], char source[]);
- void addOtherPatient();
- void changeString(char encrypted[], char source[], int change);
- char toLower(char any);
- int main()
- {
- int choice;
- int nurse = -1;
- while(nurse == -1)
- {
- nurse = nurseLogin();
- }
- while(choice != 3)
- {
- choice = mainMenu();
- switch(choice)
- {
- case 1:
- /*Call addOtherPatient function*/
- addOtherPatient();
- break;
- case 2:
- /*Call alterPassword function*/
- alterPassword(nurse);
- break;
- case 3:
- /*Clear the contents and display a closing message*/
- printf("See you soon!");
- break;
- }
- }
- return 0;
- }
- /* function that requires the user to enter valid username and
- password */
- int nurseLogin()
- {
- FILE *filesPassword;
- int i;
- int nurse;
- char nurseUsername[9];
- char nursePassword[9];
- char filesUsername[9];
- char filePassword[9];
- /*Displaying the header*/
- displayTitle("\t\t || NURSE LOGIN ||");
- printf("Enter your username and password:\n\n");
- /*Receiving user string inputs*/
- printf("Username: ");
- scanf("%9[^\n]", nurseUsername);
- emptyBuffer();
- printf("Password: ");
- scanf("%9[^\n]", nursePassword);
- emptyBuffer();
- /*Declare variables for encrypted username and password*/
- char encryptedNurseUsername[9];
- char encryptedNursePassword[9];
- /*Use encryptString to encrypt username and password*/
- encryptString(nurseUsername, encryptedNurseUsername);
- encryptString(nursePassword, encryptedNursePassword);
- /*Opening file for reading purposes*/
- filesPassword = fopen("passwords.txt", "r");
- /*Scanning for username and password within a file*/
- while(0<fscanf(filesPassword, " %[^\t] %[^\n]", filesUsername,
- filePassword))
- {
- /*Comparing user input strings with file strings*/
- if((strcmp(filesUsername, encryptedNurseUsername) == 0)
- && (strcmp(filePassword, encryptedNursePassword) == 0))
- {
- /*Closing the file*/
- fclose(filesPassword);
- /*Returning a nurse address*/
- return i;
- }
- /*Adding one to i*/
- ++i;
- }
- /*Closing the file*/
- fclose(filesPassword);
- puts("\n!! Wrong username or password, please try again !!\n");
- waitForKey();
- return -1;
- }
- /*Main menu function which displays main menu contents including
- a header and returns user's input*/
- int mainMenu()
- {
- int choice;
- /*Displaying menu list choices*/
- displayTitle("\t\t || NURSE MENU ||");
- puts("1) Add a patient");
- puts("2) Change password");
- puts("3) Exit");
- printf(">>> ");
- /*If the input is less than 1 or higher than 3 then
- display an error message*/
- while(((scanf(" %d", &choice)) == 0) ||
- (choice < 1) || (choice > 3))
- {
- emptyBuffer();
- puts("!! Please make a valid choice !!");
- printf(">>> ");
- }
- emptyBuffer();
- return choice;
- }
- void addOtherPatient(void)
- {
- /*Declaring variables for iteriation and patient details, using
- 2d array to store date of birth*/
- FILE *patientDetails;
- char patientFileName[265];
- char patientsFirstName[256];
- char patientsLastName[256];
- char patientsDateOfBirth[3][256];
- char patientsHeight[256];
- char patientsWaist[256];
- char patientsWeight[256];
- char patientsComment[512];
- /*Displaying the header*/
- displayTitle("\t\t || ADD A PATIENT ||");
- /*Storing non-encryped patient details into [0]*/
- printf("First Name: ");
- scanf("%[^\n]", &patientsFirstName);
- emptyBuffer();
- printf("Last Name: ");
- scanf("%[^\n]", &patientsLastName);
- emptyBuffer();
- printf("DOB Day [DD]: ");
- scanf("%[^\n]", &patientsDateOfBirth[0]);
- emptyBuffer();
- printf("DOB Month [MM]: ");
- scanf("%[^\n]", &patientsDateOfBirth[1]);
- emptyBuffer();
- printf("DOB Year [YYYY]: ");
- scanf("%[^\n]", &patientsDateOfBirth[2]);
- emptyBuffer();
- printf("Height [cm]: ");
- scanf("%[^\n]", &patientsHeight);
- emptyBuffer();
- printf("Waist measurement [inches]: ");
- scanf("%[^\n]", &patientsWaist);
- emptyBuffer();
- printf("Weight [kg]: ");
- scanf("%[^\n]", &patientsWeight);
- emptyBuffer();
- printf("Comment: ");
- scanf("%[^\n]", &patientsComment);
- emptyBuffer();
- /*Creating file name using the createPatientFile function*/
- createPatientFile(patientsLastName, patientsDateOfBirth[0], patientsDateOfBirth[1],
- patientsDateOfBirth[2], patientFileName);
- /*Encrypting patient's details*/
- encryptString(patientsFirstName, patientsFirstName);
- encryptString(patientsLastName, patientsLastName);
- encryptString(patientsDateOfBirth[0], patientsDateOfBirth[0]);
- encryptString(patientsDateOfBirth[1], patientsDateOfBirth[1]);
- encryptString(patientsDateOfBirth[2], patientsDateOfBirth[2]);
- encryptString(patientsHeight, patientsHeight);
- encryptString(patientsWaist, patientsWaist);
- encryptString(patientsWeight, patientsWeight);
- encryptString(patientsComment, patientsComment);
- /*Opening the file for writing purposes*/
- patientDetails = fopen(patientFileName, "w");
- /*Writing user to a file*/
- fprintf(patientDetails, "%s\n", patientsFirstName);
- fprintf(patientDetails, "%s\n", patientsLastName);
- fprintf(patientDetails, "%s/%s/%s\n", patientsDateOfBirth[0],
- patientsDateOfBirth[1], patientsDateOfBirth[2]);
- fprintf(patientDetails, "%s\n", patientsHeight);
- fprintf(patientDetails, "%s\n", patientsWaist);
- fprintf(patientDetails, "%s\n", patientsWeight);
- fprintf(patientDetails, "%s\n", patientsComment);
- /*Closing the file*/
- fclose(patientDetails);
- equalsLine();
- puts("Patient has been added!");
- /*Display continue message using waitForKey function*/
- waitForKey();
- }
- /*Function that takes patient's last name and DOB input and returns
- a file name for the patient*/
- void createPatientFile(char *patientLastName, char *patientDay, char *patientMonth,
- char *patientYear, char *patientsFileName)
- {
- int i;
- char copyYearLastTwoDigits[3];
- strcpy(patientsFileName, "Patients/");
- /*Adding last name, day and month to a file name*/
- strcat(patientsFileName, patientLastName);
- strcat(patientsFileName, patientDay);
- strcat(patientsFileName, patientMonth);
- /*Copy two last characters from the patient year of birth and store
- it to the variable*/
- memcpy(copyYearLastTwoDigits, &patientYear[2], 2);
- /*Adding \0 to the variable to make it a string*/
- copyYearLastTwoDigits[2] = '\0';
- /*Adding last two DOB digits to the file name*/
- strcat(patientsFileName, copyYearLastTwoDigits);
- /*Add the .aow extension to the file name*/
- strcat(patientsFileName, ".aow");
- /*Iterating trough a file name*/
- for(i = 0; patientsFileName[i]; ++i)
- {
- /*Changing file first character to lowercase*/
- patientsFileName[i] = toLower(patientsFileName[i]);
- }
- }
- /*A function which takes a string and transforms it's characters*/
- void changeString(char encrypted[], char source[], int change)
- {
- /*storing length of the string to encrypt*/
- int strLen = strlen(source);
- int i;
- int ch;
- /*Iterate trough the string*/
- for(i = 0; i < strLen; ++i)
- {
- /*Assign one character of the string to
- the character variable*/
- ch = source[i];
- /*If the character is between a and z*/
- if(ch >= 'a' && ch <= 'z')
- {
- /*Add change(7) to the character*/
- ch += change;
- /*If character is out of the range of z*/
- if(ch > 'z')
- {
- /*Get the difference between the character a and z,
- subtract 1 and add it to the a value*/
- ch = 'a' + (ch - 'z') - 1;
- }
- /*If the character is out of the range of a*/
- else if(ch < 'a')
- {
- /*Get the difference between the character z and a,
- add 1 and add it to the z value*/
- ch = 'z' + (ch - 'a') + 1;
- }
- }
- /*If the character is between A and Z*/
- else if(ch >= 'A' && ch <= 'Z')
- {
- /*Add change(7) to the character*/
- ch += change;
- /*If character is out of the range of Z*/
- if(ch > 'Z')
- {
- /*Get the difference between the character A and Z,
- subtract 1 and add it to the A value*/
- ch = 'A' + (ch - 'Z') - 1;
- }
- /*If the character is out of the range of A*/
- else if(ch < 'A')
- {
- /*Get the difference between the character Z and A,
- add 1 and add it to the Z value*/
- ch = 'Z' + (ch - 'A') + 1;
- }
- }
- /*If the character is between 0 and 9*/
- else if(ch >= '0' && ch <= '9')
- {
- //Add change(7) to the character
- ch += change;
- /*If character is out of the range of 9*/
- if(ch > '9')
- {
- /*Get the difference between the character 0 and 9,
- subtract 1 and add it to the 9 value*/
- ch = '0' + (ch - '9') - 1;
- }
- /*If character is out of the range of 0*/
- else if(ch < '0')
- {
- /*Get the difference between the character 9 and 0,
- add 1 and add it to the 0 value*/
- ch = '9' + (ch - '0') + 1;
- }
- }
- /*Store the encrypted character in the encrypted array*/
- encrypted[i] = ch;
- }
- /*Add \0 to the encrypted array to make it a string*/
- encrypted[strLen] = '\0';
- }
- /*A function for encryption which uses transform function*/
- void encryptString(char dest[], char source[])
- {
- /*Transforming character and moving it forwards by seven places*/
- changeString(dest, source, +7);
- }
- /*A function for decryption which uses transform function*/
- void decryptString(char dest[], char source[])
- {
- /*Transforming character and moving it backwards by seven places*/
- changeString(dest, source, -7);
- }
- /*A function which allows a nurse to change a password within
- a specified array location depending which nurse is logged in*/
- void alterPassword(int nurse)
- {
- FILE *passwordFileRead;
- FILE *passwordFileWrite;
- char newPassword[10];
- char reinforcePassword[10];
- /*Displaying the header*/
- displayTitle("\t\t || CHANGE PASSWORD ||");
- puts("Enter your new password:");
- printf(">>> ");
- /*Receiving user first password input*/
- scanf("%[^\n]", newPassword);
- emptyBuffer();
- /*If password string is not equal to 8 characters then display
- an an appropiate error*/
- if(strlen(newPassword) != 8)
- {
- puts("!! Password MUST be 8 characters long !!");
- waitForKey();
- return;
- }
- puts("Confirm your new password:");
- printf(">>> ");
- /*Receiving user second password input*/
- scanf("%[^\n]", reinforcePassword);
- emptyBuffer();
- /*Compaing the two password strings to see if they match*/
- if(strcmp(newPassword, reinforcePassword) == 0)
- {
- char encryptedPassword[10];
- /*Encrypting the new password*/
- encryptString(reinforcePassword, encryptedPassword);
- char allPasswords[5][10];
- char allUsernames[5][10];
- /*Opening file for reading purposes*/
- passwordFileRead = fopen("passwords.txt", "r");
- int i = 0;
- /*Read all usernames and passwords*/
- while(2==fscanf(passwordFileRead, " %[^\t] %[^\n]",
- allUsernames[i], allPasswords[i]))
- {
- i++;
- }
- /*Closing the file*/
- fclose(passwordFileRead);
- /*Opening the file for writing purposes*/
- passwordFileWrite = fopen("passwords.txt", "w");
- /*Overwriting an old password with a new password string*/
- strcpy(allPasswords[nurse], encryptedPassword);
- for (i = 0; i < 5; ++i)
- {
- /*Writing new strings to a file*/
- fprintf(passwordFileWrite, "%s\t%s\n", allUsernames[i],
- allPasswords[i]);
- }
- /*Closing the file*/
- fclose(passwordFileWrite);
- equalsLine();
- puts("Password has been changed!");
- waitForKey();
- return;
- }
- else
- {
- puts("!! Your password did not match, please try again !!");
- waitForKey();
- return;
- }
- }
- /*Function that display the header*/
- void displayTitle(char *header)
- {
- puts("*********************************************************");
- printf("\t\t ACTION ON WEIGHT\n");
- puts("*********************************************************");
- equalsLine();
- printf(header);
- printf("\n");
- equalsLine();
- printf("\n");
- }
- /*Function that displays some ending text and waits for user
- to press any key*/
- void waitForKey(void)
- {
- puts("=========================================================");
- printf("Press the Enter Key to Continue...\n");
- puts("=========================================================");
- getchar();
- }
- /*Function which displays a line*/
- void equalsLine(void)
- {
- puts("=========================================================");
- }
- /*Function that emptys the buffer*/
- void emptyBuffer(void)
- {
- while(getchar() != '\n')
- {
- ;
- }
- }
- char toLower(char any)
- {
- return tolower(any);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement