// Kamberly Training Courses V 0.1
#include <stdio.h>
#include <string.h>
typedef struct
{
int number;
char cName[20];
char courseCode[6];
} Course;
/* After Execution - Course details are fetched from file and the last index
that was used it loaded (if the file exsist). If the file
does not exist return -1 */
int populateCourse(char filename[], Course courses[]);
/* After Execution - Menu option is obtained from the user */
int mainMenu();
/* Before Execution - lastIndex is the total number of courses */
void printCourses(Course courses[], int lastIndex);
/* */
int findCourse(Course courses[], int lastIndex, int cName);
/* */
void getNumber(int *number);
/* */
void getString(char s[], int size);
/* */
void removeNewline(char s[]);
/* */
void saveAll(char fileName[], Course courses[], int lastIndex);
int main()
{
int numOfCourses = 5;
Course courses[numOfCourses]; //A structure to hold 5 courses' details
int lastIndex = -1; // Used to store the last index of the courses structure
// that was used
char fileName[] = "course.dat"; // Filename to be used to save and load data
lastIndex = populateCourses(fileName, courses);
int option; /* used to store the menu option
and returns until the user enters the
exit option */
do
{
option = mainMenu();
/* Used if the user enters the option for adding a new course */
if (option == 1)
{
system("cls");
printf("Add new course\n");
/* Check that the structure is full */
if (lastIndex == numOfCourses -1)
{
printf("Unable to add any more students\n");
system("pause");
}
/* If array is not full */
else
{
lastIndex++; /* Adds 1 to the contents of lastIndex */
getNumber(&courses[lastIndex].number);
printf("Enter course name: ");
getString(courses[lastIndex].cName,
sizeof(courses[lastIndex].cName));
printf("Enter course code: ");
getString(courses[lastIndex].courseCode,
sizeof(courses[lastIndex].courseCode));
}
}
/* Used if the user enters the option for displaying courses' details */
else if (option == 3)
{
/*prompts the user to enter a course code */
printCourses(courses, lastIndex);
printf("Enter course code to change the cost: ");
int cCode; // Used to store the course code
scanf("%i", &cCode);
_flushall();
/* Checks Course Code exsists */
int found = findCourse(courses, lastIndex, cCode);
/* Performed if the course code doesn't exsist */
if (found == -1)
{
printf("Course was not found\n");
system("pause");
}
/* Performed if the course was found */
else
{
printf("Course found\n");
/* Display option choices for update */
printf("1 - Course Name\n 2- Course Code\n");
int updateOption; /* Used to store the option
the user selected */
scanf("%i", &updateOption);
_flushall();
/* Performed if the user wants to update the name of the
course */
if (updateOption == 1)
{
/* Get new Course name */
printf("Enter new course name: ");
getString(courses[found].cName,
sizeof(courses[found].cName));
}
/* Performed if the user wants to modify the course code
for the course */
else if (updateOption == 2)
{
/* Get new course code */
printf("Enter new course code: ");
getString(courses[found].courseCode,
sizeof (courses[found].courseCode));
}
}
}
/* Perfomed if the user enters the option for exiting the program */
else if (option == 4)
{
saveAll(fileName, courses, lastIndex);
}
} while (option != 4);
system("pause");
return 0;
}
int mainMenu()
{
int option;
system("cls");
printf("Kambury IT Training\n\n");
printf("1: Add new course\n");
printf("2: Display courses' details\n");
printf("3: Edit course code\n");
printf("4: Exit\n");
printf("Enter options; ");
scanf("%i", &option);
_flushall();
return option;
}
int populateCourses(char fileName[], Course courses[])
{
FILE *courseFile;
// try to open the file in read binary mode
courseFile = fopen(fileName, "rb"); // open the file in read binary file
int lastIndex = -1;
// checks if the file exists or not.
// if the file exists, retrieve the information from the file
if (courseFile != NULL)
{
int i = -1;
// obtain the information relating to the number of students' details
// that have been saved
fread(&lastIndex, sizeof(int), 1, courseFile);
// retrieve each student's details
while (i < lastIndex)
{
i = i + 1;
fread(&courses[i], sizeof(Course), 1, courseFile);
}
fclose(courseFile);
}
return lastIndex;
}
// displays all the courses' details on the screen
void printCourses(Course courses[], int lastIndex)
{
int i;
system("cls");
printf("Courses' details\n\n");
// performed if the course structure contains at least one record
if (lastIndex >= 0)
{
printf("ID\tName\t\tCourse Code\n");
// repeats until all of the courses' details have been displayed
for (i = 0; i <= lastIndex; i++)
{
printf("%i\t%s\t\t%s\n", courses[i].number, courses[i].cName,
courses[i].courseCode);
}
}
}
// searches through the structure until a match for the course code is found, or
// until the end of the structure has been reached
// returns the index where the course code was found, otherwise returns -1
int findCourse(Course courses[], int lastIndex, int cCode)
{ int i;
// repeats while the contents of i is less than or equal to the contents of
// lastIndex
for (i = 0; i <= lastIndex; i++)
{
// checks if the contents of cCode is the same as the student's id. A match
// has been made
if (cCode == courses[i].number)
{
return i; // returns the position where the student id as found
}
}
system("pause");
return -1; // returns a non-index value, the id was not found in the structure
}
void saveAll(char fileName[], Course courses[], int lastIndex)
{
// checks that the structure has at least one record to save
if (lastIndex >= 0)
{
FILE *courseFile = fopen(fileName, "wb"); // open the file in write
// binary mode
int i;
// save the contents of lastIndex so the program will know how many
// records need to be read back from the file
fwrite(&lastIndex, sizeof(int), 1, courseFile);
// repeat until all the students' details have been saved to file
for (i = 0; i <= lastIndex; i++)
{
fwrite(&courses[i], sizeof(Course), 1, courseFile);
}
fclose(courseFile); // close the file
}
}
void getNumber(int *number)
{
printf("Enter student number ");
scanf("%i", number);
_flushall();
}
void getString(char s[], int size)
{
fgets(s, size, stdin);
removeNewline(s);
}
// function to remove \n for the end of the string, if \n exists
void removeNewline(char s[])
{
// find the length of the string (this will include \n if
// it exists) and then subtract 1 from the length
int i = strlen(s) - 1;
// check if the character at position 1 is \n
if (s[i] == '\n')
{
// replace \n with \0
s[i] = '\0';
}
}