Finding and Replacing Data in a File
Hi,
I'm having a real headache with this program and I'm about on the verge of tossing the keyboard through the monitor :(
I have to build a program that allows us to store no more than 5 courses.
The only operations that need to be carried out by the program are Add a course, View courses and Modify a course and when we modify a course, only the cost of the course needs to be modified and nothing else.
I've gotten everything else to work but I can't fathom out how to modify the data.
I'm not here because I want someone to do the work, what I want is for someone to explain to me in simple to understand english how to do it in a way that relates to the code I already have so I don't have to go back and modify it all.
I'm sure there are better ways to do it, but due to time constrictions, this is the way it has to be done.
Problem is I really don't understand passing stuff back and forth in C so I'm just going round and round and round and round. I've got myself to a stage now where I don't have a clue what is being passed to what or why anymore :(
The code we have been given to follow is nigh on impossible to decipher or relate to and now I'm rather frustrated and stuck and not to mention stressed due to the time I have to finish.
If someone wouldn't mind taking a look at my program so far to see if they can suggest what I need to do and a rough idea of how to do it....
Also if they spot any mistakes I've maybe made, if they could point them out.
No code needs to be offered, I want to do this myself but I need a little nudge in the right direction.
Ideally, I would like my custom function modifyCourse() to deal with the modifying of the data
Code:
//C LIBRARIES HERE-------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//STRUCTURES HERE-------
//Course structure
typedef struct
{
char courseCode[4];
char courseName[30];
int maxPlaces;
float courseCost;
} Course;
//VARIABLES AND OTHER STUFF HERE-------
//Build Course Array with a 5 courses limit
Course courses[5];
//Array / index position keeper
int position = 0;
//Specify file for storing course data
char fileName[] = "courses.dat";
//FUNCTION PROTOTYPES HERE-------
void mainMenu();
void viewCourses();
void modifyCourse();
void addCourse();
void filterInput(Course courses[]);
int readData(char fileName[], Course courses[], int position);
void writeData(char fileName[], Course courses[], int position);
void limitError(); //Extra function not in Structure Chart YET!!!
void parseData(); //Extra function not in Structure Chart YET!!!
//MAIN PROGRAM HERE-------
int main()
{
//Calls main menu function - First port of call for the program user
mainMenu();
//System stuff - pauses program and returns an int value of 0 to main - (Tells the program it ran without fault)
system("pause");
return 0;
}
//FUNCTIONS CONSTRUCTED HERE-------
//Main Menu Function
void mainMenu()
{
//stores the menu selection
char mainMenuOption;
system("cls");
printf(" ----------------------------------\n");
printf("| TRAINING COURSE SYSTEM - ALPHA |\n");
printf(" ----------------------------------\n\n");
printf("PLEASE SELECT AN OPTION:\n\n");
printf("(1) View All Courses\n");
printf("(2) Modify Course\n");
printf("(3) Add New Course\n\n");
mainMenuOption = getchar();
_flushall();
//Read data file when program starts and update position keeper according to number of course records stored
//This only needs to be called here because it gathers and returns the data needed for all the other functions too!
position = readData(fileName, courses, position);
switch(mainMenuOption)
{
case '1':
if(position <= 0)
{
system("cls");
printf("\nThere are no courses to display yet! \n\n");
system("pause");
mainMenu();
}
//Show the stored data
viewCourses();
break;
case '2':
modifyCourse();
break;
case '3':
if(position < 5)
{
addCourse();
}
else
{
//Show limit reached error
limitError();
}
break;
default:
system("cls");
printf("Invalid menu option selected.\n\n");
sleep(2000);
mainMenu();
break;
}
}
//View Stored Courses Function
void viewCourses()
{
system("cls");
printf(" ---------------------------\n");
printf("| CURRENTLY STORED COURSES |\n");
printf(" ---------------------------\n");
//Mini storage limit check and warning
if(position >= 5)
{
printf("\nWARNING: Trial Storage Limit Reached \n\n");
}
printf("-------------------------------------\n");
// performed if the students array contains at least one record
parseData();
printf("\n\n");
printf("----------------------------------------------------\n");
printf("Total courses stored: %i\n", position);
printf("----------------------------------------------------\n\n");
printf("WOULD YOU LIKE TO ADD A COURSE?\n\n");
char addAnotherOption;
scanf("%c", &addAnotherOption);
_flushall();
//Check storage limit before allowing the storing of more courses
if(addAnotherOption == 'Y' || addAnotherOption == 'y')
{
if(position < 5)
{
addCourse();
}
else if(position >= 5)
{
//Show limit reached error
limitError();
}
}
if(addAnotherOption == 'N' || addAnotherOption == 'n')
{
mainMenu();
}
}
//Modify Course Function
void modifyCourse()
{
system("cls");
printf(" ------------------\n");
printf("| MODIFY A COURSE |\n");
printf(" ------------------\n\n");
if(position <= 0)
{
printf("No data to modify\n");
}
parseData();
printf("\n\nENTER THE COURSE CODE FOR THE COURSE TO BE CHANGED:\n");
char courseCode[4];
fgets(courseCode, sizeof(courseCode), stdin);
_flushall();
}
//Add a Course Function
void addCourse()
{
//Course entry data confirmation while loop option variable
char preConfirmationOption;
//Course entry data Confirmation while loop used for verification that the course details are correct before proceeding
while(preConfirmationOption != 'y' && preConfirmationOption !='Y')
{
system("cls");
printf(" --------------\n");
printf("| ADD A COURSE |\n");
printf(" --------------\n\n");
printf("You may add a course by following\n");
printf("the on screen instructions below.\n\n");
//Get courseCode
printf("ENTER COURSE CODE:\n");
fgets(courses[position].courseCode, sizeof(courses[position].courseCode), stdin);
_flushall();
//Get courseName
printf("\nENTER COURSE TITLE:\n");
fgets(courses[position].courseName, sizeof(courses[position].courseName), stdin);
_flushall();
//Get maxPlaces
printf("\nENTER MAXIMUM NUMBER OF PLACES:\n");
scanf("%i", &courses[position].maxPlaces);
_flushall();
//Get courseCost
printf("\nENTER COURSE COST:\n");
scanf("%f", &courses[position].courseCost);
_flushall();
//Show the current course entry and all its data - list number of courses e.g. 1 of 5, 2 of 5 etc
//Prompt user to review and verify course data is correct before storing to file
system("cls");
//Filter the input here before showing the data for verification and saving
//Now throwing incompatible pointer type argument!!!!!!!!!!!!!!!!!!!!!!!!!!
filterInput(courses);
//Display the course details entered back to user for verification
printf("----------------------------------------\n");
printf("| VERIFY COURSE DETAILS BEFORE STORING |\n");
printf("----------------------------------------\n");
printf("\t\n\n%s %s %i %c%0.2f\n\n\n",
courses[position].courseCode,
courses[position].courseName,
courses[position].maxPlaces, 156, //156 Displays Pound Sterling Symbol
courses[position].courseCost
);
//Prompt the user for some action to verify if the details are correct or incorrect
printf("IS THE COURSE DATA CORRECT? (Y or N):\n\n");
//Get users verification input response
scanf("%c", &preConfirmationOption);
_flushall();
}//End data verification while loop
//Adds one to array position keeper variable - Keeps a track of the array position e.g. stored course entry 1, 2, 3 etc...
position++;
//Verification & storage confirmation message
system("cls");
printf("-------------------------------------\n");
printf("| COURSE DETAILS HAVE BEEN VERIFIED |\n");
printf("-------------------------------------\n\n");
printf("The data has been written to file.\n");
//Start the write data process
writeData(fileName, courses, position);
//display course count - TESTING PURPOSES
printf("\nNumber of Courses Currently Stored: %i of 5\n\n", position);
//Add another course option variable
char addAnotherOption;
//Prompt user to enter another course
printf("WOULD YOU LIKE TO ADD ANOTHER COURSE?\n\n");
scanf("%c", &addAnotherOption);
_flushall();
//Conditional if blocks to check to see if the course limit has been reached before allowing another course entry
//If course limit has been reached, return user to main menu after brief warning / info message
if(addAnotherOption == 'Y' || addAnotherOption == 'y')
{
if(position < 5)
{
addCourse();
}
else if(position >= 5)
{
//Show limit reached error
limitError();
}
}
if(addAnotherOption == 'N' || addAnotherOption == 'n')
{
mainMenu();
}
}//End addCourse()
//Clean Input Data Before Saving Anywhere Function - (Deals with '/n' only at the moment)
//Wouldn't let me pass both courseName & courseCode to strlen so had to make two instances of the check code
void filterInput(Course courses[])
{
//CHECK courseCode VARIABLE FOR NEW LINE CHARACTER-----
int x = strlen(courses[position].courseCode) -1;
if(courses[position].courseCode[x] == '\n')
{
courses[position].courseCode[x] = '\0';
}
//CHECK courseName VARIABLE FOR NEW LINE CHARACTER-----
x = strlen(courses[position].courseName) -1;
if(courses[position].courseName[x] == '\n')
{
courses[position].courseName[x] = '\0';
}
}
//Read Data from file Function - Adapted from populate students example
int readData(char fileName[], Course courses[], int position)
{
FILE *courseFile;
courseFile = fopen(fileName, "rb");
if (courseFile != NULL)
{
int i = -1;
fread(&position, sizeof(int), 1, courseFile);
while (i < position)
{
i++;
fread(&courses[i], sizeof(Course), 1, courseFile);
}
fclose(courseFile);
}
return position;
}
//parses the stored data for showing anywhere when called
void parseData()
{
int i;
if (position >= 0)
{
printf("Code\t Name\t\t Places\t Cost\n");
printf("-------------------------------------\n");
// repeats until all of the students' details have been displayed
for (i = 0; i <= position -1; i++)
{
printf("%s\t%s\t%i\t%0.2f\n", courses[i].courseCode,
courses[i].courseName,
courses[i].maxPlaces,
courses[i].courseCost);
}
}
}
//Write Data to File Function
void writeData(char fileName[], Course courses[], int position)
{
if(position >= 0)
{
FILE *courseFile = fopen(fileName, "wb");
int i;
fwrite(&position, sizeof(int), 1, courseFile);
for(i = 0; i <= position; i++)
{
fwrite(&courses[i], sizeof(Course), 1, courseFile);
}
fclose(courseFile);
}
}
void limitError()
{
system("cls");
printf("---------------------------------------------------------------\n");
printf("WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING\n");
printf("---------------------------------------------------------------\n\n");
printf("You Have Reached Your Trial Version Course Storage Limit.\n\n");
printf("To unlock this program, please send me all your money!\n\n");
printf("You will now be returned to the main menu\n\n");
//Sleep tells the program to wait for a specified duration before proceeding
sleep(8000);
mainMenu();
}