Thanks for the reply. I know about the error checking, just didn't put it in here for some reason.
Anyway, I used 64 because at the start of my main method, I created an array of structs as
Code:
struct engineer engineerTable[64];
I used getch() as a test in that function while trying to debug.
Full code so far:
Code:
#include <stdio.h>
#include "printh.h"
struct engineer {
int contractNumber;
char name[16];
int age;
char sex;
char language[20];
char assignmentState;
};
void loadData();
void clearEngineers();
void addEngineer(struct engineer *, int);
void assignEngineer(struct engineer *);
void addField();
void reportStats(struct engineer *);
void saveData();
void displayMenu();
void displayHeader();
int main() {
char choice;
int count;
struct engineer engineerTable[64];
do {
displayHeader();
displayMenu();
scanf("%c", &choice);
fflush(stdin);
if(choice == 'e') {
system("cls");
addEngineer(engineerTable, count);
system("cls");
}
else if(choice == 'o') {
system("cls");
addField();
system("cls");
}
else if(choice == 'a') {
system("cls");
assignEngineer(engineerTable);
system("cls");
}
else if(choice == 'r') {
system("cls");
reportStats(engineerTable);
system("cls");
}
else if(choice == 'c') {
system("cls");
clearEngineers();
system("cls");
}
else if(choice == 'x') {
break;
}
else {
printf("\n\nThat is not a valid option...\n\n");
sleep(600);
system("cls");
}
}
while(choice != 'x');
return 0;
}
void displayHeader() {
printH("BOC Pipeline Engineer Management Console");
}
void displayMenu() {
printf("\te\tEnter a new engineer\n");
printf("\to\tEnter a new oil field\n");
printf("\ta\tAssign an engineer to field\n");
printf("\tr\tGenerate a report\n");
printf("\tc\tClear the engineer table\n");
printf("\tx\tExit and save\n\n");
printf("Please enter your choice: ");
}
void loadData() {
}
void clearEngineers() {
char temp;
FILE *filePtr;
filePtr = fopen("BOCTable.dat", "w");
if(filePtr == NULL) {
printf("There was an error while opening the file, please try again.");
}
else {
printf("Are you sure you wish to clear the database? (y/n): ");
scanf("%c", &temp);
fflush(stdin);
if(temp == 'y') {
fprintf(filePtr, "");
fclose(filePtr);
printf("\n\nData successfully erased :)");
sleep(700);
}
else if(temp == 'n') {
printf("\nExiting to main menu");
sleep(700);
}
}
}
void addEngineer(struct engineer *engineers, int counter) {
FILE *filePtr;
filePtr = fopen("BOCTable.dat", "ab+");
if(filePtr == NULL) {
printf("There was an error while opening the file. Please try again");
}
else {
fread(counter, sizeof(int), 1, filePtr);
printf("counter is %d\n\n", &counter);
printf("\nPlease enter the name of the engineer: ");
scanf("%s", engineers[counter].name);
printf("\nPlease enter the age of the engineer: ");
scanf("%d", &engineers[counter].age);
fflush(stdin);
printf("\nPlease enter the sex of the engineer (m or f): ");
scanf("%c", &engineers[counter].sex);
fflush(stdin);
printf("\nPlease enter the contract number of the engineer: ");
scanf("%s", &engineers[counter].contractNumber);
fflush(stdin);
printf("\nPlease enter the language spoken by the engineer: ");
scanf("%c", &engineers[counter].language);
fflush(stdin);
printf("\nPlease enter engineer assignment type (a - assigned/c - on call): ");
scanf("%c", &engineers[counter].assignmentState);
fflush(stdin);
counter++;
fwrite(&counter, sizeof(int), 1, filePtr);
fwrite(engineers, sizeof(struct engineer), 64, filePtr);
fclose(filePtr);
}
}
void assignEngineer(struct engineer *engineers) {
}
void addField() {
}
void reportStats(struct engineer *engineers) {
FILE *filePtr;
filePtr = fopen("BOCTable.dat", "rb");
if(filePtr == NULL) {
printf("There was an error opening the file, please try again");
exit(1);
}
else {
char choice;
int i;
printf("Please select the type of report you require\n\n");
printf("\ta\tList of all assigned engineers\n");
printf("\tc\tList of all on-call engineers\n\n");
printf("Please enter your choice: ");
scanf("%c", &choice);
fflush(stdin);
if(choice == 'a') {
fread(engineers, sizeof(struct engineer), 64, filePtr);
for(i=0; i<64; i++) {
if(engineers[i].assignmentState == 'a') {
printf("\n\n%s\n", engineers[i].name);
}
}
}
else if(choice == 'c') {
fread(engineers, sizeof(struct engineer), 64, filePtr);
for(i=0; i<64; i++) {
if(engineers[i].assignmentState == 'c') {
printf("\n\n%s\n", engineers[i].name);
}
}
}
getch();
}
}
void saveData() {
}