I noticed that I have a lot of duplication in my codes, but I really don't know how to solve duplication that's why I decided to seek for advice and help from Stack Overflow. I also notice that I have been using more than 1 loop in more cases so I wonder can you all check on my following code snippet and tell me how do I reduce the amount of loop usage and remove the duplication.
My code is a bit long so I decide to put it in an online compiler so that you guys can see it clearly, I'm also a bit sorry for asking this ridiculous question, but I really wanted to know how can I reduce the number of lines and memory usage in my program, I'm pretty bad at this..
The following code snippet is basically a program that captures the record about sanitizing a venue; the data will be written in a binary file.
Infection Level Check
This function is mainly for prompting the user and collecting the response from user. It is for tracking their level of infection based on their response. I only allow the user to answer yes or no which is a method of validation I believe.
Table Header / Footer
This is just for printing the table header and footer which will be used in most of the functions. I decided to make it a function to reduce duplication.
Sanitize Menu
This is basically the main menu of this program.
It allows user to navigate to any other feature that he or she wishes to use.
I use a nested do
…while
for doing validation. I feel like this is not the best way of doing it, but I really don't know how can I improve it.
Add Record
It is mainly for adding a new sanitization record. It prompts and asks the user a few things and all the data will be written in a binary file call Sanitization Record.dat
. I did some validation for date and time, but I feel like it is a little bit too long. I wonder if I can reduce the amount of lines for the validation?
Search Record
This is just a menu for searching a record based on their ID, the company name and etc. Yes, I did use a nested while
loop in here for doing validation, which I feel is definitely not the right way of doing it. Maybe you guys can give me any suggestions on it?
The Rest of the Search Function
All of these are the same, just that the searching method is different. If it's a string, I will be using strcmp
, else I will just compare it with a normal if
condition. Once the record is found in the binary file, it will print out the record for the user to see.
Modify Function
It allows the user to modify everything inside the binary file except the venue ID. It will prompt and ask the user to enter the venue id he wish to modify, and it will print out the relevant detail to that venue ID and ask for confirmation. If the user answers "Y", it will allow the user to modify those records. Once modified, all the records will be rewritten into the binary file.
Delete Function
It allows the user to delete any record inside the binary file. It will first read the binary file and capture all the data into an array structure, then it will ask the user to enter the venue ID for deleting the record. If a record is found, it will prompt and ask whether the user wants to delete; if the user answers "Y" the record will be deleted through array shifting method. Then everything will be rewritten into the binary file.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#pragma warning (disable:4996)
#define MAX 20
typedef struct {
char companyName[41], personInCharge[41], gender, contactNum[13];
float temperature;
int infectionLevel;
}Company;
typedef struct {
int hours, minute;
}Time;
typedef struct {
int day, month, year;
}Date;
struct SanitizeData {
char venueID[10];
Company Details;
Time Start;
Time End;
Date sanitizeDate;
};
// Function Prototype Declaration
int infectionLevelCheck();
void tableHeader();
void tableFooter(int, int);
void sanitizeMenu();
void addRecord();
void searchRecord();
void searchVenueID();
void searchDate();
void searchCompany();
void searchName();
void searchContactNumber();
void searchGender();
void searchTemperature();
void modifyRecord();
void displayRecord();
void deleteRecord();
int infectionLevelCheck() {
char userResponse;
int infectionLevel;
int invalidCounter;
do {
invalidCounter = 0;
infectionLevel = 0;
printf("\n\nRisk Of Infection Check: \n");
printf("-----------------------\n");
printf("Are you having the 2 or more of the following symptoms listed?\n");
printf("- Fever\n");
printf("- Sore Throat\n");
printf("- Runny Nose\n");
printf("- Diarrhea\n");
printf("- Headache\n");
printf("- Body Ache\n");
printf("(Y = Yes, N = No): ");
scanf("%c", &userResponse);
rewind(stdin);
if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
invalidCounter++;
}
else if (toupper(userResponse) == 'Y') {
infectionLevel++;
}
printf("\n\nBesides the Symptoms listed above, are you having the following symptoms: \n");
printf("- Cough\n");
printf("- Difficulty breathing\n");
printf("- Loss of smell\n");
printf("- Loss of taste\n");
printf("(Y = Yes, N = No): ");
scanf("%c", &userResponse);
rewind(stdin);
if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
invalidCounter++;
}
else if (toupper(userResponse) == 'Y') {
infectionLevel += 4;
}
printf("\n\nHave you travelled to any country outside Malaysia\nwithin the past 14 days?\n");
printf("(Y = Yes, N = No): ");
scanf("%c", &userResponse);
rewind(stdin);
if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
invalidCounter++;
}
else if (toupper(userResponse) == 'Y') {
infectionLevel += 2;
}
printf("\n\nHave you had closed contact with anyone who \nconfirmed or suspected case of COVID 19 within the 14 days?");
printf("(Y = Yes, N = No): ");
scanf("%c", &userResponse);
rewind(stdin);
if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
invalidCounter++;
}
else if (toupper(userResponse) == 'Y') {
infectionLevel += 4;
}
if (invalidCounter > 0) {
printf("\n- Error Found: Invalid Response Entered - \n\n");
}
} while (invalidCounter != 0);
return infectionLevel;
}
void tableHeader() {
printf("------------------------------------------------------------------------------------------------------------------------\n");
printf("| Venue | Sanitization | Ending | Company | Person | Gender | Contact | Temperature | Infection |\n");
printf("| ID | Time | Time | In-Charge | In-Charge | | | | Risk |\n");
printf("|-------|------------------|----------|---------------|-------------|--------|--------------|-------------|------------|\n");
}
void tableFooter(int i, int emptyRecord) {
printf("------------------------------------------------------------------------------------------------------------------------\n");
if (emptyRecord == MAX) {
printf("No Such Record Found\n");
}
else {
printf("%d Record Found\n", i);
}
}
void addRecord() {
struct SanitizeData newData;
FILE* addPtr;
char userResponse;
int validate, invalidCounter, counter = 0, infectionlevel = 0;
addPtr = fopen("Sanitization Record.dat", "ab");
if (addPtr == NULL) {
printf("Unable to open Sanitization Record.dat\n");
exit(-1);
}
else {
do {
system("cls");
do {
invalidCounter = 0;
printf("Add Sanitization Record\n");
printf("-----------------------\n");
printf("Enter Venue ID: ");
scanf("%s", &newData.venueID);
rewind(stdin);
printf("Enter Sanitized Date in [DD/MM/YYYY Format Eg: 19/02/2021] : ");
validate = scanf("%d/%d/%d", &newData.sanitizeDate.day, &newData.sanitizeDate.month, &newData.sanitizeDate.year);
rewind(stdin);
printf("Enter Sanitized Time in [HH:MM Format Eg: 12:51] - \n");
printf("Starting: ");
validate = scanf("%d:%d", &newData.Start.hours, &newData.Start.minute);
rewind(stdin);
printf("Ending: ");
validate = scanf("%d:%d", &newData.End.hours, &newData.End.minute);
rewind(stdin);
printf("Enter Santizie Handling Company Details: \n");
printf("Company Name: ");
scanf("%[^\n]", &newData.Details.companyName);
rewind(stdin);
printf("Person In-Charge Name: ");
scanf("%[^\n]", &newData.Details.personInCharge);
rewind(stdin);
printf("Person In-Charge Gender: ");
scanf("%c", &newData.Details.gender);
rewind(stdin);
printf("Person In-Charge Contact Numer: ");
scanf("%s", &newData.Details.contactNum);
rewind(stdin);
printf("Person In-Charge Temperature: ");
validate = scanf("%f", &newData.Details.temperature);
rewind(stdin);
newData.Details.infectionLevel = infectionLevelCheck();
if (newData.Details.temperature > 37.8) newData.Details.infectionLevel++;
if (validate == 0
|| newData.sanitizeDate.day < 1 || newData.sanitizeDate.day > 31
|| newData.sanitizeDate.month > 12 || newData.sanitizeDate.month < 1
|| newData.Start.hours > 24 || newData.Start.hours < 1
|| newData.Start.minute > 60 || newData.Start.minute < 1
|| newData.End.hours > 24 || newData.End.hours < 1
|| newData.End.minute > 60 || newData.End.minute < 1
|| toupper(newData.Details.gender) != 'M' && toupper(newData.Details.gender) != 'F')
{
if (newData.sanitizeDate.day < 1 || newData.sanitizeDate.day > 31 || newData.sanitizeDate.month > 12 || newData.sanitizeDate.month < 1) {
printf("- Error Found: Invalid Month or Days Entered. -\n");
}
if (newData.Start.hours > 24 || newData.Start.hours < 1 || newData.Start.minute > 60 || newData.Start.minute < 1) {
printf("- Error Found: Invalid Starting Hours or Minutes Entered. - \n");
}
if (newData.End.hours > 24 || newData.End.hours < 1 || newData.End.minute > 60 || newData.End.minute < 1) {
printf("- Error Found: Invalid Ending Hours or Minutes Entered. - \n");
}
if (toupper(newData.Details.gender) != 'M' || toupper(newData.Details.gender) != 'F') {
printf("- Error Found: Invalid Gender Entered. -\n");
}
if (validate == 0) {
printf("- Error Found: Kindly Enter Valid Input Only. - \n");
}
invalidCounter++;
system("pause");
system("cls");
}
else {
fwrite(&newData, sizeof(newData), 1, addPtr);
counter++;
}
} while (invalidCounter != 0);
printf("Add Another Record? (N = No): ");
scanf("%c", &userResponse);
rewind(stdin);
} while (toupper(userResponse) != 'N');
printf("%d Record Added.....\n", counter);
fclose(addPtr);
}
}
void searchVenueID() {
struct SanitizeData data[20];
FILE* searchPtr;
int i = 0, emptyRecord = 0;
int counter = 0;
char venueID[10];
searchPtr = fopen("Sanitization Record.dat", "rb");
if (searchPtr == NULL) {
printf("Unable to open Santization Record.dat\n");
exit(-1);
}
while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
i++;
}
printf("Enter Venue ID to Search: ");
scanf("%s", &venueID);
tableHeader();
for (int i = 0; i < 20; i++) {
if (strcmp(venueID, data[i].venueID) == 0) {
counter++;
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
data[i].Details.personInCharge);
switch (toupper(data[i].Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data[i].Details.contactNum, data[i].Details.temperature);
if (data[i].Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data[i].Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data[i].Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
}
else {
emptyRecord++;
}
}
tableFooter(counter, emptyRecord);
fclose(searchPtr);
}
void searchDate() {
struct SanitizeData data[20];
FILE* searchPtr;
int i = 0, emptyRecord = 0;
int counter = 0;
int day, month, year;
searchPtr = fopen("Sanitization Record.dat", "rb");
if (searchPtr == NULL) {
printf("Unable to open Santization Record.dat\n");
exit(-1);
}
while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
i++;
}
printf("Enter Date to Search [Format: 19/02/2021] : ");
scanf("%d/%d/%d", &day, &month, &year);
tableHeader();
for (i = 0; i < 20; i++) {
if (day == data[i].sanitizeDate.day && month == data[i].sanitizeDate.month && year == data[i].sanitizeDate.year) {
counter++;
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
data[i].Details.personInCharge);
switch (toupper(data[i].Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data[i].Details.contactNum, data[i].Details.temperature);
if (data[i].Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data[i].Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data[i].Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
}
else {
emptyRecord++;
}
}
tableFooter(counter, emptyRecord);
fclose(searchPtr);
}
void searchCompany() {
struct SanitizeData data[20];
FILE* searchPtr;
int i = 0, emptyRecord = 0;
char companyName[41];
int counter = 0;
searchPtr = fopen("Sanitization Record.dat", "rb");
if (searchPtr == NULL) {
printf("Unable to open Santization Record.dat\n");
exit(-1);
}
while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
i++;
}
printf("Enter Company Name to Search: ");
scanf("%[^\n]", &companyName);
tableHeader();
for (i = 0; i < 20; i++) {
if (strcmp(companyName, data[i].Details.companyName) == 0) {
counter++;
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
data[i].Details.personInCharge);
switch (toupper(data[i].Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data[i].Details.contactNum, data[i].Details.temperature);
if (data[i].Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data[i].Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data[i].Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
}
else {
emptyRecord++;
}
}
tableFooter(counter, emptyRecord);
fclose(searchPtr);
}
void searchName() {
struct SanitizeData data[20];
FILE* searchPtr;
int i = 0, emptyRecord = 0;
char personInCharge[41];
int counter = 0;
searchPtr = fopen("Sanitization Record.dat", "rb");
if (searchPtr == NULL) {
printf("Unable to open Santization Record.dat\n");
exit(-1);
}
while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
i++;
}
printf("Enter Person In-Charge Name to Search: ");
scanf("%[^\n]", &personInCharge);
tableHeader();
for (i = 0; i < 20; i++) {
if (strcmp(personInCharge, data[i].Details.personInCharge) == 0) {
counter++;
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
data[i].Details.personInCharge);
switch (toupper(data[i].Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data[i].Details.contactNum, data[i].Details.temperature);
if (data[i].Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data[i].Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data[i].Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
}
else {
emptyRecord++;
}
}
tableFooter(counter, emptyRecord);
fclose(searchPtr);
}
void searchContactNumber() {
struct SanitizeData data[20];
FILE* searchPtr;
int i = 0, emptyRecord = 0;
char contactNum[13];
int counter = 0;
searchPtr = fopen("Sanitization Record.dat", "rb");
if (searchPtr == NULL) {
printf("Unable to open Santization Record.dat\n");
exit(-1);
}
while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
i++;
}
printf("Enter Contact Number to Search [Format: 010-2012687]: ");
scanf("%s", &contactNum);
tableHeader();
for (i = 0; i < 20; i++) {
if (strcmp(contactNum, data[i].Details.contactNum) == 0) {
counter++;
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
data[i].Details.personInCharge);
switch (toupper(data[i].Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data[i].Details.contactNum, data[i].Details.temperature);
if (data[i].Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data[i].Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data[i].Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
}
else {
emptyRecord++;
}
}
tableFooter(counter, emptyRecord);
fclose(searchPtr);
}
void searchGender() {
struct SanitizeData data[20];
FILE* searchPtr;
int i = 0, emptyRecord = 0;
char gender;
int counter = 0;
searchPtr = fopen("Sanitization Record.dat", "rb");
if (searchPtr == NULL) {
printf("Unable to open Santization Record.dat\n");
exit(-1);
}
while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
i++;
}
printf("Enter Person In-Charge Gender to Search [F = Female, M = Male]: ");
scanf("%c", &gender);
tableHeader();
for (i = 0; i < 20; i++) {
if (gender == data[i].Details.gender) {
counter++;
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
data[i].Details.personInCharge);
switch (toupper(data[i].Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data[i].Details.contactNum, data[i].Details.temperature);
if (data[i].Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data[i].Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data[i].Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
}
else {
emptyRecord++;
}
}
tableFooter(counter, emptyRecord);
fclose(searchPtr);
}
void searchTemperature() {
struct SanitizeData data[20];
FILE* searchPtr;
int i = 0, emptyRecord = 0;
float temperature;
int counter = 0;
searchPtr = fopen("Sanitization Record.dat", "rb");
if (searchPtr == NULL) {
printf("Unable to open Santization Record.dat\n");
exit(-1);
}
while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
i++;
}
printf("Enter Temperature To Search [Eg: 36.6]: ");
scanf("%f", &temperature);
tableHeader();
for (i = 0; i < 20; i++) {
if (temperature == data[i].Details.temperature) {
counter++;
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
data[i].Details.personInCharge);
switch (toupper(data[i].Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data[i].Details.contactNum, data[i].Details.temperature);
if (data[i].Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data[i].Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data[i].Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
}
else {
emptyRecord++;
}
}
tableFooter(counter, emptyRecord);
fclose(searchPtr);
}
void searchRecord() {
int selection, invalidCounter;
char userResponse;
do {
system("cls");
do {
invalidCounter = 0;
printf("Search Record Based On Following Criteria: \n");
printf("1. Venue ID\n");
printf("2. Sanitization Date\n");
printf("3. Company Name\n");
printf("4. Person In-Charge\n");
printf("5. Contact Number\n");
printf("6. Gender\n");
printf("7. Temperature\n");
printf("8. Back to Sanitize Menu\n");
printf("Enter Your Choice > ");
scanf("%d", &selection);
if (selection < 1 || selection > 8) {
invalidCounter++;
printf("- Error Found: Enter 1 - 8 ONLY - \n");
system("pause");
system("cls");
}
} while (invalidCounter != 0);
switch (selection) {
case 1:
searchVenueID();
break;
case 2:
searchDate();
break;
case 3:
searchCompany();
break;
case 4:
searchName();
break;
case 5:
searchContactNumber();
break;
case 6:
searchGender();
break;
case 7:
searchTemperature();
case 8:
sanitizeMenu();
break;
}
printf("Anymore to Search? (N = No): ");
scanf("%c", &userResponse);
rewind(stdin);
} while (toupper(userResponse) != 'N');
}
void modifyRecord() {
struct SanitizeData data;
struct SanitizeData modify[20];
FILE* modifyPtr;
int i = 0, counter = 0, validate, invalidCounter, emptyRecord = 0, modifyCounter = 0;
char venueID[10], userResponse;
modifyPtr = fopen("Sanitization Record.dat", "rb");
if (modifyPtr == NULL) {
printf("Unable to open Sanitization Record.dat\n");
exit(-1);
}
while (fread(&data, sizeof(data), 1, modifyPtr) != 0) {
modify[modifyCounter] = data;
modifyCounter++;
}
modifyPtr = fopen("Sanitization Record.dat", "wb");
if (modifyPtr == NULL) {
printf("Unable to open Sanitization Record.dat\n");
exit(-1);
}
do {
system("cls");
printf("Modify Sanitize Record\n");
printf("----------------------\n\n");
printf("Enter Venue ID To Modify: ");
scanf("%s", &venueID);
rewind(stdin);
for (i = 0; i < modifyCounter; i++) {
if (strcmp(venueID, modify[i].venueID) == 0) {
printf("Venue ID : %s\n", modify[i].venueID);
printf("Sanitization Date : %02d-%02d-%4d\n", modify[i].sanitizeDate.day, modify[i].sanitizeDate.month, modify[i].sanitizeDate.year);
printf("Starting Time : %02d:%02d\n", modify[i].Start.hours, modify[i].Start.minute);
printf("Ending Time : %02d:%02d\n", modify[i].End.hours, modify[i].End.minute);
printf("Sanitized Company Name : %s\n", modify[i].Details.companyName);
printf("Person In-Charge Name : %s\n", modify[i].Details.personInCharge);
printf("Person In-Charge Gender : %c\n", modify[i].Details.gender);
printf("Person In-Charge Contact Number: %s\n", modify[i].Details.contactNum);
printf("Person In-Charge Temperature : %.2f\n\n", modify[i].Details.temperature);
printf("Sure to Modify? (Y = Yes): ");
scanf("%c", &userResponse);
if (toupper(userResponse) == 'Y') {
counter++;
do {
invalidCounter = 0;
printf("Enter Sanitized Date in [DD/MM/YYYY Format Eg: 19/02/2021] : ");
validate = scanf("%d/%d/%d", &modify[i].sanitizeDate.day, &modify[i].sanitizeDate.month, &modify[i].sanitizeDate.year);
rewind(stdin);
printf("Enter Sanitized Time in [HH:MM Format Eg: 12:51] - \n");
printf("Starting: ");
validate = scanf("%d:%d", &modify[i].Start.hours, &modify[i].Start.minute);
rewind(stdin);
printf("Ending: ");
validate = scanf("%d:%d", &modify[i].End.hours, &modify[i].End.minute);
rewind(stdin);
printf("Enter Santizie Handling Company Details: \n");
printf("Company Name: ");
scanf("%[^\n]", &modify[i].Details.companyName);
rewind(stdin);
printf("Person In-Charge Name: ");
scanf("%[^\n]", &modify[i].Details.personInCharge);
rewind(stdin);
printf("Person In-Charge Gender: ");
scanf("%c", &modify[i].Details.gender);
rewind(stdin);
printf("Person In-Charge Contact Numer: ");
scanf("%s", &modify[i].Details.contactNum);
rewind(stdin);
printf("Person In-Charge Temperature: ");
validate = scanf("%f", &modify[i].Details.temperature);
rewind(stdin);
modify[i].Details.infectionLevel = infectionLevelCheck();
if (modify[i].Details.temperature > 37.8) modify[i].Details.infectionLevel++;
if (validate == 0
|| modify[i].sanitizeDate.day < 1 || modify[i].sanitizeDate.day > 31
|| modify[i].sanitizeDate.month > 12 || modify[i].sanitizeDate.month < 1
|| modify[i].Start.hours > 24 || modify[i].Start.hours < 1
|| modify[i].Start.minute > 60 || modify[i].Start.minute < 1
|| modify[i].End.hours > 24 || modify[i].End.hours < 1
|| modify[i].End.minute > 60 || modify[i].End.minute < 1
|| toupper(modify[i].Details.gender) != 'M' && toupper(modify[i].Details.gender) != 'F')
{
if (modify[i].sanitizeDate.day < 1 || modify[i].sanitizeDate.day > 31 || modify[i].sanitizeDate.month > 12 || modify[i].sanitizeDate.month < 1) {
printf("- Error Found: Invalid Month or Days Entered. -\n");
}
if (modify[i].Start.hours > 24 || modify[i].Start.hours < 1 || modify[i].Start.minute > 60 || modify[i].Start.minute < 1) {
printf("- Error Found: Invalid Starting Hours or Minutes Entered. - \n");
}
if (modify[i].End.hours > 24 || modify[i].End.hours < 1 || modify[i].End.minute > 60 || modify[i].End.minute < 1) {
printf("- Error Found: Invalid Ending Hours or Minutes Entered. - \n");
}
if (toupper(modify[i].Details.gender) != 'M' || toupper(modify[i].Details.gender) != 'F') {
printf("- Error Found: Invalid Gender Entered. -\n");
}
if (validate == 0) {
printf("- Error Found: Kindly Enter Valid Input Only. - \n");
}
invalidCounter++;
system("pause");
system("cls");
}
} while (invalidCounter != 0);
}
else {
break;
}
fwrite(&modify[i], sizeof(data), 1, modifyPtr);
}
else {
emptyRecord++;
}
}
if (emptyRecord == 20) {
printf("No Record Found\n");
}
printf("Continue Modify Record? (N = No): ");
scanf("%c", &userResponse);
} while (userResponse != 'N');
printf("%d Record Modified\n", counter);
fclose(modifyPtr);
}
void displayRecord() {
struct SanitizeData data;
FILE* displayPtr;
int i = 0;
displayPtr = fopen("Sanitization Record.dat", "rb");
if (displayPtr == NULL) {
printf("Unable to open Sanitization Record.dat\n");
exit(-1);
}
system("cls");
printf("Display All Sanitization Record\n");
printf("-------------------------------\n");
tableHeader();
while (fread(&data, sizeof(data), 1, displayPtr) != 0) {
printf("| %-5s | %02d-%02d-%4d %02d:%02d | %02d:%02d | %-13s | %-11s | ", data.venueID, data.sanitizeDate.day, data.sanitizeDate.month, data.sanitizeDate.year,
data.Start.hours, data.Start.minute, data.End.hours, data.End.minute, data.Details.companyName,
data.Details.personInCharge);
switch (toupper(data.Details.gender)) {
case 'F':
printf("Female ");
break;
case 'M':
printf(" Male ");
break;
}
printf("| %-12s | %.2f | ", data.Details.contactNum, data.Details.temperature);
if (data.Details.infectionLevel <= 1) {
printf(" Low Risk |\n");
}
else if (data.Details.infectionLevel <= 3) {
printf(" Mid Risk |\n");
}
else if (data.Details.infectionLevel >= 4) {
printf(" High Risk |\n");
}
i++;
}
printf("------------------------------------------------------------------------------------------------------------------------\n");
printf("%d Record Found\n", i);
fclose(displayPtr);
}
void deleteRecord() {
struct SanitizeData data;
struct SanitizeData delData[20];
FILE* deletePtr;
int i = 0, delCount = 0, position, emptyRecord = 0;
char userResponse;
char venueID[10];
deletePtr = fopen("Sanitization Record.dat", "rb");
if (deletePtr == NULL) {
printf("Unable to open Sanitization.dat\n");
exit(-1);
}
else {
while (fread(&data, sizeof(data), 1, deletePtr) != 0) {
delData[i] = data;
i++;
delCount++;
}
}
fclose(deletePtr);
deletePtr = fopen("Sanitization Record.dat", "wb");
if(deletePtr == NULL) {
printf("Unable to open Sanitization.dat\n");
exit(-1);
}
else {
do {
printf("Delete Record Based On: ");
printf("1. Venue ID\n");
printf("Enter Venue ID: ");
scanf("%s", venueID);
rewind(stdin);
for (i = 0; i < delCount; i++) {
if (strcmp(venueID, delData[i].venueID) == 0) {
printf("Venue ID : %s\n", delData[i].venueID);
printf("Sanitization Date : %02d-%02d-%4d\n", delData[i].sanitizeDate.day, delData[i].sanitizeDate.month, delData[i].sanitizeDate.year);
printf("Starting Time : %02d:%02d\n", delData[i].Start.hours, delData[i].Start.minute);
printf("Ending Time : %02d:%02d\n", delData[i].End.hours, delData[i].End.minute);
printf("Sanitized Company Name : %s\n", delData[i].Details.companyName);
printf("Person In-Charge Name : %s\n", delData[i].Details.personInCharge);
printf("Person In-Charge Gender : %c\n", delData[i].Details.gender);
printf("Person In-Charge Contact Number: %s\n", delData[i].Details.contactNum);
printf("Person In-Charge Temperature : %.2f\n\n", delData[i].Details.temperature);
position = i;
printf("Confirm to Delete? (Y = Yes): ");
scanf("%c", &userResponse);
rewind(stdin);
if (toupper(userResponse) == 'Y') {
for (i = position; i < delCount; i++) {
delData[i] = delData[i + 1];
}
}
}
else {
emptyRecord++;
}
}
if (emptyRecord == 20) {
printf("-Error Found: No Record - \n");
}
printf("Anymore to Delete? (Y = Yes): ");
scanf("%c", &userResponse);
rewind(stdin);
if (toupper(userResponse) == 'N') {
for (int i = 0; i < delCount--; i++) {
fwrite(&delData[i], sizeof(delData[i]), 1, deletePtr);
}
}
} while (toupper(userResponse) != 'N');
}
fclose(deletePtr);
}
void sanitizeMenu() {
int validate, invalidCounter, selection;
char userResponse;
do {
system("cls");
do {
invalidCounter = 0;
printf(" Sanitization Record Module\n");
printf("-------------------------------------\n");
printf(" MENU\n");
printf("[1] - Add Sanitization Record\n");
printf("[2] - Search Sanitization Record\n");
printf("[3] - Modify Sanitization Record\n");
printf("[4] - Display Sanitization Record\n");
printf("[5] - Delete Sanitization Record\n");
printf("[6] - Record \n");
printf("[7] - Quit This Menu\n");
printf("[8] - Exit The Program\n\n");
printf("Enter Your Selection: ");
validate = scanf("%d", &selection);
rewind(stdin);
if (selection < 1 || selection > 8 || validate == 0) {
invalidCounter++;
printf("- Error Found: Please Enter 1 - 7 Only -\n");
system("pause");
system("cls");
}
} while (invalidCounter != 0);
switch (selection) {
case 1:
addRecord();
break;
case 2:
searchRecord();
break;
case 3:
modifyRecord();
break;
case 4:
displayRecord();
break;
case 5:
deleteRecord();
break;
case 6:
menu();
break;
case 7:
footer();
exit(0);
}
printf("Back to Sanitize Menu? (N = No): ");
rewind(stdin);
scanf("%c", &userResponse);
rewind(stdin);
} while (toupper(userResponse) != 'N');
}