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 <string.h>
- /* Function declarations */
- void emptyBuffer(void);
- int nurseLogin();
- void createPatientFile
- (char *patientLastName, char *patientDay,
- char *patientMonth, char *patientYear,
- char *patientsFileName);
- void displayTitle(char *title);
- int mainMenu();
- void waitForKey(void);
- void decryptString(char dest[], char source[]);
- void encryptString(char dest[], char source[]);
- void addOtherPatient();
- void changeStringToChar(char *encrypted, char *source, int change);
- void alterPassword(int);
- 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("Dont forget 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];
- displayTitle(" // NURSE LOGIN //");
- printf("Please enter your Username: \n");
- scanf("%9[^\n]", nurseUsername);
- emptyBuffer();
- printf("Please enter your Password: \n");
- scanf("%9[^\n]", nursePassword);
- emptyBuffer();
- //encryptString(nurseUsername, nurseUsername);
- //encryptString(nursePassword, nursePassword);
- filesPassword = fopen("passwords.txt", "r");
- while(0<fscanf(filesPassword, " %[^\t] %[^\n]", filesUsername,
- filePassword))
- {
- if((strcmp(filesUsername, nurseUsername) == 0)
- && (strcmp(filePassword, nursePassword) == 0))
- {
- fclose(filesPassword);
- return i;
- }
- ++i;
- }
- fclose(filesPassword);
- puts("\n You entered the wrong username or password, please try "
- "again!\n");
- waitForKey();
- return -1;
- }
- /* Function that displays the main menu options to the user */
- int mainMenu()
- {
- int menuChoice;
- displayTitle(" // NURSE MENU //");
- printf("1. Add a patient file\n");
- printf("2. Change password\n");
- printf("3. Exit\n");
- printf("Enter Appropriate Number: ");
- while(((scanf(" %d", &menuChoice)) == 0) ||
- (menuChoice < 1) || (menuChoice > 3))
- {
- emptyBuffer();
- printf("Please enter a valid 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 patientsLastName[20];
- char patientsDateOfBirth[3][20];
- char patientsHeight[10];
- char patientsWaist[10];
- char patientsWeight[10];
- char patientsComment[500];
- displayTitle(" // ADD A PATIENT //");
- printf("Patients First Name: ");
- scanf("%[^\n]", &patientsFirstName);
- emptyBuffer();
- printf("Patients Last Name: ");
- scanf("%[^\n]", &patientsLastName);
- 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 in cm: ");
- scanf("%[^\n]", &patientsHeight);
- emptyBuffer();
- printf("Patients Waist measurement in inches: ");
- scanf("%[^\n]", &patientsWaist);
- emptyBuffer();
- printf("Patients Weight in kg: ");
- scanf("%[^\n]", &patientsWeight);
- emptyBuffer();
- printf("Extra Comments: ");
- scanf("%[^\n]", &patientsComment);
- emptyBuffer();
- /* Function to create the file name */
- createPatientFile(patientsLastName, patientsDateOfBirth[0], patientsDateOfBirth[1],
- patientsDateOfBirth[2], patientFileName);
- /*Encrypt information of the patients*/
- 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);
- patientDetails = fopen(patientFileName, "w");
- 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);
- fclose(patientDetails);
- printf("-------------------------------------------------------\n");
- 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 *encrypted, char *source, int change)
- {
- int strLen = strlen(source);
- int i;
- int ch;
- for(i = 0; i < strLen; ++i)
- {
- ch = source[i];
- 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;
- }
- }
- else if(ch >= '0' && ch <= '9')
- {
- ch += change;
- if(ch > '9')
- {
- ch = '0' + (ch - '9') - 1;
- }
- else if(ch < '0')
- {
- ch = '9' + (ch - '0') + 1;
- }
- }
- encrypted[i] = ch;
- }
- encrypted[strLen] = '\0';
- }
- /*A function for encryption which uses transform function*/
- void encryptString(char dest[], char source[])
- {
- changeStringToChar(dest, source, +7);
- }
- /*A function for decryption which uses transform function*/
- void decryptString(char dest[], char source[])
- {
- changeStringToChar(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];
- displayTitle(" // CHANGE PASSWORD //");
- printf("Please enter your new password:\n");
- scanf("%[^\n]", newPassword);
- emptyBuffer();
- if(strlen(newPassword) != 8)
- {
- printf("Your new password MUST be 8 characters long ! \n");
- waitForKey();
- 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 allPasswords[5][10];
- char allUsernames[5][10];
- passwordFileRead = fopen("passwords.txt", "r");
- int i = 0;
- while(2==fscanf(passwordFileRead, " %[^\t] %[^\n]",
- allUsernames[i], allPasswords[i]))
- {
- i++;
- }
- fclose(passwordFileRead);
- passwordFileWrite = fopen("passwords.txt", "w");
- strcpy(allPasswords[nurse], encryptedPassword);
- for (i = 0; i < 5; ++i)
- {
- fprintf(passwordFileWrite, "%s\t%s\n", allUsernames[i],
- allPasswords[i]);
- }
- fclose(passwordFileWrite);
- printf("Password has been changed!\n");
- printf("---------------------------------------------------\n");
- waitForKey();
- return;
- }
- else
- {
- puts("Your password did not match, please try again !");
- waitForKey();
- return;
- }
- }
- /*Function that display the program header*/
- void displayTitle(char *header)
- {
- printf
- ("=========================================================\n");
- printf(" ACTION ON WEIGHT\n");
- printf("========================================================\n");
- printf(header);
- printf("\n");
- }
- /*Function that asks user to press any key to continue */
- void waitForKey(void)
- {
- printf
- ("*********************************************************"
- "*\n");
- printf("Press any key in order to continue...\n");
- printf
- ("*********************************************************"
- "*\n");
- getchar();
- }
- /*Function to empty the buffer*/
- void emptyBuffer(void)
- {
- while(getchar() != '\n')
- {
- ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement