Code:
#include <stdio.h>#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define CHILDRENFILE "children"
#define SESSIONFILE "sessions"
// child record
struct CHILDDETAILS
{
char childForename[31];
char childSurname[31];
char guardianName[31];
char guardianNum[12];
char homeNumber[12];
int birthDay, birthMonth, birthYear, childID;
int sessionID;
};
//children record
struct SESSIONTYPE
{
int sessionID;
char sessionName[21];
char sessionTime[30];
char sessionDay[15];
char headOfSession[21];
};
// Prototypes
int SessionToArray(struct SESSIONTYPE *);
void ArrayToSession(int, struct SESSIONTYPE *);
int ChildrenToArray(struct CHILDDETAILS *);
void ArrayToChildren(int, struct CHILDDETAILS *);
int AddSession(int numSessions, struct SESSIONTYPE *);//Add records to borrower file
void DisplaySessions(int numSessions, struct SESSIONTYPE *);//Display all session records
void DisplayOneSession(struct SESSIONTYPE session); //Display individual session record
void SearchSession(int numSessions, struct SESSIONTYPE *);
void EditSession(int numSessions, struct SESSIONTYPE *);
void DeleteSession(int numSessions, struct SESSIONTYPE *);
int AddChildren(int numSessions, int numChildren, struct SESSIONTYPE *, struct CHILDDETAILS *);//Add records to children file
void DisplayChildren(int numSessions, int numChildren, struct SESSIONTYPE *, struct CHILDDETAILS *);//Display all children records
void DisplayOneChild(struct CHILDDETAILS, int numSessions, struct SESSIONTYPE *sessions);//Display individual children record
void SearchChildren(int numSessions, int numChildren, struct SESSIONTYPE *, struct CHILDDETAILS *);
void EditChildren(int numSessions, int numChildren, struct SESSIONTYPE *, struct CHILDDETAILS *);
void DeleteChildren(int numSessions, int numChildren, struct SESSIONTYPE *, struct CHILDDETAILS *);
int Menu();//Main menu
int SessionMenu(int, struct SESSIONTYPE *);//session sub-menu
int ChildMenu(int numSessions, struct SESSIONTYPE *, int numChildren, struct CHILDDETAILS *);//children sub-menu
void EnterSession(int numSessions, struct SESSIONTYPE *sessions); //Enter fields of session record
void EnterChildInfo(int numSessions, int numChildren, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children); //Enter fields of children record
void DisplayThisSession(int sessionID, int numSessions, struct SESSIONTYPE *);//Display single borrower record identified by ID - parameter
void ListSessions(int numSessions, struct SESSIONTYPE *sessions); //Show list of sessions
char ValidQual(); //Enter valid children qualification
void DisplayQual(char qual); //Display children qualification
char ValidYN(); //Function to enter and validate a Y/N reply
int ValidInt(int lower, int upper); //Function to enter and validate an integer in a range
char *ValidateString(int maxLength, char *string);
// Main function
void main()
{
int choice;
int numSessions, numChildren;
//Arrays to store files
struct SESSIONTYPE sessions[100];
struct CHILDDETAILS children[100];
numSessions = SessionToArray(sessions);
numChildren = ChildrenToArray(children);
do
{
// Call main menu - sessions or children
choice = Menu();
switch (choice)
{
// Process session file
case 1: numSessions = SessionMenu(numSessions, sessions);
break;
// Process children file
case 2: numChildren = ChildMenu(numSessions, numChildren, sessions, children);
break;
// Quit system
case 3: printf("Bye bye\n\n");
break;
}
} while (choice != 3);
ArrayToSession(numSessions, sessions);
ArrayToChildren(numChildren, children);
system("pause");
exit(0);
}
// Copy session File to session Array
int SessionToArray(struct SESSIONTYPE *sessions)
{
FILE *fp;
struct SESSIONTYPE session;
char reply;
int numSessions = 0;
fopen_s(&fp, SESSIONFILE, "rb");
if (fp == NULL)
{
printf("Existing session file not found. Do you want to create a new file? Y/N: ");
reply = ValidYN();
if (reply == 'N')
{
exit(0);
}
}
else
{
// Read first session record
fread(&session, sizeof(struct SESSIONTYPE), 1, fp);
//Keep going up to end of file marker
while (!(feof(fp)))
{
// Copy session read from file to array
sessions[numSessions] = session;
//Move to next session in array
numSessions++;
// Read subsequent session records
fread(&session, sizeof(struct SESSIONTYPE), 1, fp);
}
fclose(fp);
}
return numSessions;
}
// Copy session Array to session File
void ArrayToSession(int numSessions, struct SESSIONTYPE *sessions)
{
FILE *fp;
struct SESSIONTYPE session;
int i;
fopen_s(&fp, SESSIONFILE, "wb");
if (fp == NULL)
{
printf("Cannot open session file\n");
exit(0);;
}
for (i = 0; i < numSessions; i++)
{
// Write session record in array to file
fwrite(&sessions[i], sizeof(struct SESSIONTYPE), 1, fp);
}
fclose(fp);
}
// Copy children File to children Array
int ChildrenToArray(struct CHILDDETAILS *children)
{
FILE *fp;
struct CHILDDETAILS child;
char reply;
int numChildren = 0;
fopen_s(&fp, CHILDRENFILE, "rb");
if (fp == NULL)
{
printf("Existing children file not found. Do you want to create a new file? Y/N: ");
reply = ValidYN();
if (reply == 'N')
{
exit(0);
}
}
else
{
// Read first session record
fread(&child, sizeof(struct CHILDDETAILS), 1, fp);
// Keep going up to end of file marker
while (!(feof(fp)))
{
// Copy children read from file to array
children[numChildren] = child;
//Move to next children in array
numChildren++;
// Read subsequent session records
fread(&child, sizeof(struct CHILDDETAILS), 1, fp);
}
fclose(fp);
}
return numChildren;
}
// Copy children Array to children File
void ArrayToChildren(int numChildren, struct CHILDDETAILS *children)
{
FILE *fp;
struct CHILDDETAILS child;
int i;
fopen_s(&fp, CHILDRENFILE, "wb");
if (fp == NULL)
{
printf("Cannot open children file\n");
exit(0);;
}
for (i = 0; i < numChildren; i++)
{
// Write session record in array to file
fwrite(&children[i], sizeof(struct CHILDDETAILS), 1, fp);
}
fclose(fp);
}
// Main menu function
int Menu()
{
int select;
printf("\n\nWelcome to the Brimsbury Parish Church Little Foxes Toddler Group session management system\n");
printf("Please select one of the options below: \n\n");
printf("1: Enter Session information Menu\n2: Enter Children information Menu\n3: Quit system\n\n");
printf("Enter a number that corresponds to your choice: ");
select = ValidInt(1, 3);
printf("\n");
return select;
}
// Session Menu function
int SessionMenu(int numSessions, struct SESSIONTYPE *sessions)
{
int choice;
do
{
// Make choice between session file processes
printf("\n\nYou have entered the Session menu\n");
printf("This is where you can locate and add information on the Sessions \n\n");
printf("1: Add a new Session\n2: Display all stored Sessions\n3: Search and display one Session\n4: Edit a Session\n5: Delete a Session\n6: Go Back to the main menu\n\n");
printf("Enter a number that corresponds to your choice: ");
choice = ValidInt(1, 6);
// Do session file processes
switch (choice)
{
case 1: numSessions = AddSession(numSessions, sessions);
break;
case 2: DisplaySessions(numSessions, sessions);
break;
case 3: SearchSession(numSessions, sessions);
break;
case 4: EditSession(numSessions, sessions);
break;
case 5: DeleteSession(numSessions, sessions);
break;
case 6: ArrayToSession(numSessions, sessions);
printf("Session file saved\n\n");
break;
}
} while (choice != 6);
return numSessions;
}
// children Menu function
int ChildMenu(int numSessions, int numChildren, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children)
{
int choice;
do
{
printf("\n\nThis is the children sub_system\n");
printf("You can choose different file handling routines\n\n");
printf("1: Add a children to file\n2: Display all children on file\n3: Search for a child on file\n4: Edit a child on file\n5: Delete a child on file\n6: Back to main menu\n\n");
printf("Enter a number that corresponds to your choice: ");
choice = ValidInt(1, 6);
// Do children file processes
switch (choice)
{
case 1: numChildren = AddChildren(numSessions, numChildren, sessions, children);
break;
case 2: DisplayChildren(numSessions, numChildren, sessions, children);
break;
case 3: SearchChildren(numSessions, numChildren, sessions, children);
break;
case 4: EditChildren(numSessions, numChildren, sessions, children);
break;
case 5: DeleteChildren(numSessions, numChildren, sessions, children);
break;
case 6: ArrayToChildren(numChildren, children);
printf("Children file saved\n\n");
break;
}
} while (choice != 6);
return numChildren;
}
//Add records to session file
int AddSession(int numSessions, struct SESSIONTYPE *sessions)
{
//Enter fields of next session record in array
EnterSession(numSessions, sessions);
//Increment number of sessions
numSessions++;
return numSessions;
}
//Enter fields of session record
void EnterSession(int numSessions, struct SESSIONTYPE *sessions)
{
printf("Please enter the new Session ID (Format must be between 101 and 999): ");
sessions[numSessions].sessionID = ValidInt(101, 999);
printf("Please enter a name for this Session (Up to 50 characters): ");
strcpy_s(sessions[numSessions].sessionName, 50, ValidateString(50, sessions[numSessions].sessionName));
printf("Type the day of the week attending (T - Tuesday, W - Wednesday or F - Friday): ");
strcpy_s(sessions[numSessions].sessionDay, 2, ValidateString(2, sessions[numSessions].sessionDay));
printf("Please select the time of day this session runs: \n\nM - :Morning (09:30 - 11:00)\nA - :Afternoon (13:30 - 15:00)\n\n");
strcpy_s(sessions[numSessions].sessionTime, 2, ValidateString(2, sessions[numSessions].sessionTime));
printf("Enter the initials of the session leader (3 characters): ");
strcpy_s(sessions[numSessions].headOfSession, 4, ValidateString(4, sessions[numSessions].headOfSession));
}
// Display the whole session file
void DisplaySessions(int numSessions, struct SESSIONTYPE *sessions)
{
int i;
printf("Session ID Name Time of Day Head of Session \n");
// for every record in array
for (i = 0; i < numSessions; i++)
{
DisplayOneSession(sessions[i]);
}
}
void SearchSession(int numSessions, struct SESSIONTYPE *sessions)
{
int searchID; //SessionID Search
int found = FALSE;
int i = 0;
printf("Enter the ID of the session you want to display (101 - 999): ");
searchID = ValidInt(101, 999);
//Loop while there are records in the file and student not found
while ((i < numSessions) && (found == FALSE))
{
if (searchID == sessions[i].sessionID)
{
//Display student record
DisplayOneSession(sessions[i]);
found = TRUE;
}
i++;
}
if (found == FALSE)
{
printf("Session %d is not on file\n", searchID);
}
printf("\n\n");
}
void EditSession(int numSessions, struct SESSIONTYPE *sessions)
{
int editID; //ID to edit
int found = FALSE;
int i = 0;
printf("Enter the ID of Session you want to edit (101 - 999): ");
editID = ValidInt(101, 999);
//Loop while there are records in the file and student not found
while ((i < numSessions) && (found == FALSE))
{
if (editID == sessions[i].sessionID)
{
//Display record found
DisplayOneSession(sessions[i]);
found = TRUE;
printf("Enter changed session name (Up to 50 characters): ");
strcpy_s(sessions[numSessions].sessionName, 50, ValidateString(50, sessions[numSessions].sessionName));
printf("Enter changed session day (T - Tuesday, W - Wednesday or F - Friday): ");
strcpy_s(sessions[numSessions].sessionDay, 3, ValidateString(3, sessions[numSessions].sessionDay));
printf("Enter changed session time M - :Morning (09:30 - 11:00) A - :Afternoon (13:30 - 15:00): ");
strcpy_s(sessions[numSessions].sessionTime, 2, ValidateString(2, sessions[numSessions].sessionTime));
printf("Enter changed session Leader (3 characters): ");
strcpy_s(sessions[numSessions].headOfSession, 3, ValidateString(3, sessions[numSessions].headOfSession));
//Display updated record
DisplayOneSession(sessions[i]);
}
i++;
}
if (found == FALSE)
{
printf("Session %d is not on file\n", editID);
}
printf("\n\n");
}
void DeleteSession(int numSessions, struct SESSIONTYPE *sessions)
{
int searchID; //SessionID Search
int found = FALSE;
int i = 0;
printf("Enter the ID of the Session you want to display (101 - 999): ");
searchID = ValidInt(101, 999);
//Loop while there are records in the file and student not found
while ((i < numSessions) && (found == FALSE))
{
if (searchID == sessions[i].sessionID)
{
sessions[i].sessionID == 'NULL';
found = TRUE;
}
i++;
}
if (found == FALSE)
{
printf("Session %d is not on file\n", searchID);
}
printf("\n\n");
}
// Display individual session record
void DisplayOneSession(struct SESSIONTYPE session)
{
printf("\n%-11d%-20s%-20s%-20s\n", session.sessionID, session.sessionName, session.sessionTime, session.headOfSession);
}
// Display one particular session record
// This function finds and displays session of the children you're looking at
// Get ID of matching record thro' parameter passed from children file
void DisplayThisSession(int sessionID, int numSessions, struct SESSIONTYPE *sessions)
{
int found;
int i = 0;
// Matching record not yet found
found = FALSE;
//Keep looking while not yet end of file and matching record not yet found
while ((i < numSessions) && (found == FALSE))
{
// Check if this is matching record
if (sessions[i].sessionID == sessionID)
{
// Set flag to show matching record has been found
found = TRUE;
// Display details of matching record
DisplayOneSession(sessions[i]);
}
//Move to next session
i++;
}
// If you reach end of array without finding matching record, it's not there
if (found == FALSE)
{
printf("Session not on file\n");
}
}
//Show list of sessions
void ListSessions(int numSessions, struct SESSIONTYPE *sessions)
{
int i;
printf("Session ID Name\n");
for (i = 0; i < numSessions; i++)
{
printf("%-10d %s\n", sessions[i].sessionID, sessions[i].sessionName);
}
}
// Add records to children file
int AddChildren(int numSessions, int numChildren, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children)
{
// Type fields into next children record in array
EnterChildInfo(numSessions, numChildren, sessions, children);
//Increment number of children
numChildren++;
return numChildren;
}
//Enter child information to store
void EnterChildInfo(int numChildren, int numSessions, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children)
{
system("cls");
printf("Type child ID: (a number between 1 and 90): ");
children[numChildren].childID = ValidInt(1, 90);
printf("Type child forename (max length 30 characters): ");
strcpy_s(children[numChildren].childForename, 30, ValidateString(30, children[numChildren].childForename));
printf("Type child surname (max length 30 characters): ");
strcpy_s(children[numChildren].childSurname, 30, ValidateString(30, children[numChildren].childSurname));
printf("Type child day of birth (in the format DD): ");
children[numChildren].birthDay = ValidInt(1, 31);
printf("Type child month of birth (in the format MM): ");
children[numChildren].birthMonth = ValidInt(1, 12);
printf("Type child year of birth (in the format YYYY): ");
children[numChildren].birthYear = ValidInt(1990, 2099);
printf("Type guardian name (max length 30 characters): ");
strcpy_s(children[numChildren].guardianName, 30, ValidateString(30, children[numChildren].guardianName));
printf("Type guardian mobile telephone number (max length 12 numbers): ");
strcpy_s(children[numChildren].guardianNum, 12, ValidateString(12, children[numChildren].guardianNum));
printf("Type guardian home telephone number (max length 12 numbers): ");
strcpy_s(children[numChildren].homeNumber, 12, ValidateString(12, children[numChildren].homeNumber));
//Children qualification
printf("Type session ID: \nPossible sessions are:\n");
ListSessions(numSessions, sessions);
children[numChildren].sessionID = ValidInt(101, 999);
}
void SearchChildren(int numChildren, int numSessions, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children)
{
int searchID; //SessionID Search
int found = FALSE;
int i = 0;
printf("Enter the ID of the child you want to display (1 - 90): ");
searchID = ValidInt(1, 90);
//Loop while there are records in the file and student not found
while ((i < numChildren) && (found == FALSE))
{
if (searchID == children[i].childID)
{
//Display student record
DisplayOneChild(children[i], numSessions, sessions);
found = TRUE;
}
i++;
}
if (found == FALSE)
{
printf("Session %d is not on file\n", searchID);
}
printf("\n\n");
}
void EditChildren(int numChildren, int numSessions, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children)
{
int editID; //ID to edit
int found = FALSE;
int i = 0;
system("cls"); //Clear screen
printf("Please enter the ID of the child you want to edit: ");
editID = ValidInt(1, 90);
while ((i < numChildren) && (found == FALSE)) //Loop while there are records in the file and student not found
{
if (editID == children[i].childID)
{
DisplayOneChild(children[i], numSessions, sessions); //Displays the record that links to the student ID
found = TRUE;
printf("Enter revised day of birth (in the format DD): "); //Enter revised day of birth
children[i].birthDay = ValidInt(1, 31); //Validates the number
printf("Enter revised month of birth (in the format MM): "); //Enter revised month of birth
children[i].birthMonth = ValidInt(1, 12); //Validates the number
printf("Enter revised month of birth (in the format YYYY): "); //Enter revised month of birth
children[i].birthMonth = ValidInt(1990, 2099); //Validates the number
printf("Enter a revised home number: "); //Enter revised home number
strcpy_s(children[numChildren].homeNumber, 12, ValidateString(12, children[numChildren].homeNumber));
printf("Enter a revised guardian number: "); //Enter a revised guardian number
strcpy_s(children[numChildren].guardianNum, 12, ValidateString(12, children[numChildren].guardianNum));
printf("Enter revised session ID: \nPossible sessions are:\n");
ListSessions(numSessions, sessions);
children[numChildren].sessionID = ValidInt(101, 999);
printf("The revised child record is below.\n\n");
DisplayOneChild(children[i], numSessions, sessions); //Display edited record
}
i++;
}
if (found == FALSE) //Child ID not found display error
{
printf("Child %d is not on file.\n", editID);
}
}
void DeleteChildren(int numChildren, int numSessions, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children)
{
int searchID; //SessionID Search
int found = FALSE;
int i = 0;
printf("Enter the ID of the Session you want to display (1 - 90): ");
searchID = ValidInt(1, 90);
//Loop while there are records in the file and student not found
while ((i < numChildren) && (found == FALSE))
{
if (searchID == children[i].childID)
{
children[i].childID == 'NULL';
found = TRUE;
}
i++;
}
if (found == FALSE)
{
printf("Session %d is not on file\n", searchID);
}
printf("\n\n");
}
// Display the children file
void DisplayChildren(int numSessions, int numChildren, struct SESSIONTYPE *sessions, struct CHILDDETAILS *children)
{
int i;
// Keep going for all children
for (i = 0; i < numChildren; i++)
{
if (i == 0)
{
printf("-------------------------------------");
}
//Display current children details
DisplayOneChild(children[i], numSessions, sessions);
printf("-------------------------------------");
}
}
//Displays one child record
void DisplayOneChild(struct CHILDDETAILS child, int numSessions, struct SESSIONTYPE *sessions)
{
printf("\nChild ID: %2d\n", child.childID);
printf("Child forename: %-25s\n", child.childForename);
printf("Child surname; %-25s\n", child.childSurname);
printf("Child date of birth: %2d%2d%2d\n", child.birthDay, child.birthMonth, child.birthYear);
printf("Guardian name: %-25s\n", child.guardianName);
printf("Guardian mobile number: %-25s\n", child.guardianNum);
printf("Guardian home number: %-25s\n", child.homeNumber);
DisplayThisSession(child.sessionID, numSessions, sessions);
}
//Enter and validate Y/N reply
char ValidYN()
{
char reply;
do
{
scanf_s("%c", &reply, 1);
fflush(stdin);
reply = toupper(reply);
} while ((reply != 'Y') && (reply != 'N'));
return reply;
}
//Function to enter and validate an integer in a set range
int ValidInt(int lowest, int highest)
{
int entry;
do
{
scanf_s("%d", &entry);
fflush(stdin);
if ((entry < lowest) || (entry > highest))
{
printf("Error! Please re-enter\n");
}
} while ((entry < lowest) || (entry > highest));
return entry;
}
char *ValidateString(int MaxLength, char *String1)
{
String1 = (char *)malloc(80);
do
{
gets_s(String1, 80);
if (strlen(String1) > MaxLength)
{
printf("Error. String too long. Re-enter\n");
}
} while (strlen(String1) > MaxLength);
return String1;
}