Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************
- * prog1nurse.c
- * A program that allows only 5 nurses to sign in. Once logged on the
- program allows patient files to be created and nurses passwords to
- be changed.
- * December 2017
- **********************************************************************/
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- /* Function declarations */
- void emptyBuffer(void);
- void printLine(void);
- int nurseLogin();
- void createPatientFile
- (char *patientLastName, char *patientDay,
- char *patientMonth, char *patientYear,
- char *patientsFileName);
- int mainMenu();
- void decryptString(char decrypt[], char source[]);
- void encryptString(char encrypt[], char source[]);
- void addOtherPatient();
- void changeStringToChar(char encrypt[], char source[], int change);
- void alterPassword(int login);
- int main()
- {
- int choice;
- int nurse = -1;
- while(nurse == -1)
- {
- nurse = nurseLogin();
- }
- while(choice != 3)
- {
- choice = mainMenu();
- switch(choice)
- {
- case 1:
- addOtherPatient();
- break;
- case 2:
- alterPassword(nurse);
- break;
- case 3:
- printf("Make sure to visit soon!");
- break;
- }
- }
- return 0;
- }
- /* function that requires the user to enter valid username and
- password, which is then checked with files*/
- int nurseLogin()
- {
- FILE *filesPassword;
- int i;
- char nurseUsername[9];
- char nursePassword[9];
- char filesUsername[9];
- char filePassword[9];
- char encryptUser[9];
- char encryptPass[9];
- printf("=======================================================\n");
- printf(" ACTION ON WEIGHT\n");
- printf("=======================================================\n");
- printf("\n *// Nurse Login //*\n\n");
- printf("Please enter your Username: \n");
- scanf("%9[^\n]", nurseUsername);
- emptyBuffer();
- printf("Please enter your Password: \n");
- scanf("%9[^\n]", nursePassword);
- emptyBuffer();
- encryptString(nurseUsername, encryptUser);
- encryptString(nursePassword, encryptPass);
- filesPassword = fopen("passwords.txt", "r");
- while(0<fscanf(filesPassword, " %[^\t] %[^\n]", filesUsername,
- filePassword))
- {
- printf("%s %s : %s %s", filesUsername, filePassword,
- encryptUser, encryptPass);
- if((strcmp(filesUsername, encryptUser) == 0)
- && (strcmp(filePassword, encryptPass) == 0))
- {
- fclose(filesPassword);
- return i;
- }
- ++i;
- }
- fclose(filesPassword);
- puts("\n|| The password or username you entered seems to be\n"
- "incorrect, please try again ||\n");
- return -1;
- }
- /* Function that displays the main menu options to the user */
- int mainMenu()
- {
- int menuChoice;
- printLine();
- printf("\n *// Nurse Main Menu //*\n\n");
- printLine();
- printf("\n1. Add a patient file\n");
- printf("2. Change password\n");
- printf("3. Exit\n");
- printf("\nPlease selection an option: ");
- while(((scanf(" %d", &menuChoice)) == 0) ||
- (menuChoice < 1) || (menuChoice > 3))
- {
- emptyBuffer();
- printf("Please select an appropiate number(1-3)! \n");
- printf("Re-enter number: ");
- }
- emptyBuffer();
- return menuChoice;
- }
- /* function that allows the user to enter a new patients details */
- void addOtherPatient(void)
- {
- FILE *patientDetails;
- char patientFileName[20];
- char patientsFirstName[20];
- char patientsSecondName[20];
- char patientsDateOfBirth[3][20];
- char patientsHeight[10];
- char patientsWaist[10];
- char patientsWeight[10];
- char patientsComment[500];
- printLine();
- printf(" Add a patient to a file!\n");
- printLine();
- printf("Patients First Name: ");
- scanf("%[^\n]", &patientsFirstName);
- emptyBuffer();
- printf("Patients Second Name: ");
- scanf("%[^\n]", &patientsSecondName);
- emptyBuffer();
- printf("Patients DOB Day: ");
- scanf("%[^\n]", &patientsDateOfBirth[0]);
- emptyBuffer();
- printf("Patients DOB Month: ");
- scanf("%[^\n]", &patientsDateOfBirth[1]);
- emptyBuffer();
- printf("Patients DOB Year: ");
- scanf("%[^\n]", &patientsDateOfBirth[2]);
- emptyBuffer();
- printf("Patients Height: ");
- scanf("%[^\n]", &patientsHeight);
- emptyBuffer();
- printf("Patients Waist Measurement: ");
- scanf("%[^\n]", &patientsWaist);
- emptyBuffer();
- printf("Patients Weight: ");
- scanf("%[^\n]", &patientsWeight);
- emptyBuffer();
- printf("Extra Comments: ");
- scanf("%[^\n]", &patientsComment);
- emptyBuffer();
- /* Function to create the file name */
- createPatientFile(patientsSecondName, patientsDateOfBirth[0], patientsDateOfBirth[1],
- patientsDateOfBirth[2], patientFileName);
- /*Encrypt information of the patients*/
- encryptString(patientsFirstName, patientsFirstName);
- encryptString(patientsSecondName, patientsSecondName);
- 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);
- patientDetails = fopen(patientFileName, "w");
- fprintf(patientDetails, "%s\n", patientsFirstName);
- fprintf(patientDetails, "%s\n", patientsSecondName);
- 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);
- fclose(patientDetails);
- printLine();
- puts("Patient successfully added!");
- }
- /* Function which creates a file name for patients based on their
- last name and their date of birth */
- void createPatientFile(char *patientLastName, char *patientDay, char *patientMonth,
- char *patientYear, char *patientsFileName)
- {
- int i;
- char copyYearLastTwoDigits[3];
- strcpy(patientsFileName, "Patients/");
- strcat(patientsFileName, patientLastName);
- strcat(patientsFileName, patientDay);
- strcat(patientsFileName, patientMonth);
- memcpy(copyYearLastTwoDigits, &patientYear[2], 2);
- copyYearLastTwoDigits[2] = '\0';
- strcat(patientsFileName, copyYearLastTwoDigits);
- strcat(patientsFileName, ".aow");
- }
- /*A function which takes a string and transforms it's characters*/
- void changeStringToChar(char encrypt[], char source[], int change)
- {
- int length = strlen(encrypt);
- char toEncrypt[length+1];
- int i;
- int ch;
- for(i = 0; i < length; ++i)
- {
- ch = encrypt[i];
- if(ch >= '0' && ch <= '9')
- {
- ch += change;
- if(ch > '9')
- ch = '0' + (ch - '9') - 1;
- else if(ch < '0')
- ch = '9' + (ch - '0') + 1;
- }
- else if(ch >= 'A' && ch <= 'Z')
- {
- ch += change;
- if(ch > 'Z')
- ch = 'A' + (ch - 'Z') - 1;
- else if(ch < 'A')
- ch = 'Z' + (ch - 'A') + 1;
- }
- else if(ch >= 'a' && ch <= 'z')
- {
- ch += change;
- if(ch > 'z')
- ch = 'a' + (ch - 'z') - 1;
- else if(ch < 'a')
- ch = 'z' + (ch - 'a') + 1;
- }
- toEncrypt[i] = ch;
- }
- toEncrypt[length] = '\0';
- strcpy(source, toEncrypt);
- }
- //A function for encryption which uses transform function
- void encryptString(char encrypt[], char source[])
- {
- changeStringToChar(encrypt, source, +7);
- }
- //A function for decryption which uses transform function
- void decryptString(char decrypt[], char source[])
- {
- changeStringToChar(decrypt, 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 login)
- {
- FILE *passwordFileRead;
- FILE *passwordFileWrite;
- char newPassword[10];
- char reinforcePassword[10];
- printLine();
- printf(" Change your password!\n");
- printLine();
- printf("Please enter your new password:\n");
- scanf("%[^\n]", newPassword);
- emptyBuffer();
- if(strlen(newPassword) != 8)
- {
- printf("Password must be 8 characters long! \n");
- return;
- }
- printf("Please confirm your new password: \n");
- scanf("%[^\n]", reinforcePassword);
- emptyBuffer();
- /*function to compare two password strings to see if they
- match*/
- if(strcmp(newPassword, reinforcePassword) == 0)
- {
- char encryptedPassword[10];
- encryptString(reinforcePassword, encryptedPassword);
- char fileUsernames[5][10];
- char filePasswords[5][10];
- passwordFileRead = fopen("passwords.txt", "r");
- int i = 0;
- while(2==fscanf(passwordFileRead, " %[^\t] %[^\n]",
- fileUsernames[i], filePasswords[i]))
- {
- i++;
- }
- fclose(passwordFileRead);
- passwordFileWrite = fopen("passwords.txt", "w");
- strcpy(filePasswords[login], encryptedPassword);
- for (i = 0; i < 5; ++i)
- {
- fprintf(passwordFileWrite, "%s\t%s\n", fileUsernames[i],
- filePasswords[i]);
- }
- fclose(passwordFileWrite);
- printf("\nPassword successfully changed!\n");
- return;
- }
- else
- {
- puts("Your password did not match, please try again !");
- return;
- }
- }
- /*Function that asks user to press any key to continue */
- void printLine(void)
- {
- printf("-------------------------------------------------------\n");
- }
- /*Function to empty the buffer*/
- void emptyBuffer(void)
- {
- while(getchar() != '\n')
- {
- ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement