Here is my code. The problem is when I run the Addrecords function the first time it works, but the next time I run it, it goes straight back to the main menu. I don't understand why!
Can someone have a look and see if they can spot why....
/* Header files defined */
#include <stdio.h>
/* Constants defined */
#define makemax 50
#define modelmax 50
#define yearregmax 10
#define regmarkmax 10
#define mileagemax 10
#define pricemax 10
#define maxrecords 30
/* Structed defined */
struct carrecords
{
char make[makemax];
char model[modelmax];
char yearreg[yearregmax];
char regmark[regmarkmax];
char mileage[mileagemax];
char price[pricemax];
int used;
}record[maxrecords];
/* Variables defined */
int i, k, count;
char quit, option;
int makel, modell, yearregl, regmarkl, mileagel, pricel;
/* File Pointer defined */
FILE *fp;
/* Function to add records to that database */
void addrecord(void)
{
if ((fp = fopen("car.data", "a+b"))==NULL) /* Opens the database file for writing to */
{
printf("\n\nCannot open file!\n\n"); /* Error statement if database file can not be opened */
}
else
{
char sync = getchar(); /* Captures carriage return from menu */
fread(&record, sizeof(struct carrecords), maxrecords, fp); /* Reads database file into structure records */
for (k=0;k <= maxrecords;k++)
{
if (record[k].used & 1) /* Searches through the structure for first available record */
{
;
}
else
{
break; /* Breaks when it finds an available record. */
}
}
for(i = k; quit != 'q'; i++) /* Starts at that available record to enter record data */
{
printf("\n\nEnter details for each car\n\n");
printf("Make of Car: ");
fgets(record[i].make, makemax, stdin);
makel = strlen(record[i].make);
record[i].make[makel - 1] = '\0';
printf("Model of Car: ");
fgets(record[i].model, modelmax, stdin);
modell = strlen(record[i].model);
record[i].model[modell - 1] = '\0';
printf("Year of Registration: ");
fgets(record[i].yearreg, yearregmax, stdin);
yearregl = strlen(record[i].yearreg);
record[i].yearreg[yearregl - 1] = '\0';
printf("Registration Number: ");
fgets(record[i].regmark, regmarkmax, stdin);
regmarkl = strlen(record[i].regmark);
record[i].regmark[regmarkl - 1] = '\0';
printf("Car Mileage: ");
fgets(record[i].mileage, mileagemax, stdin);
mileagel = strlen(record[i].mileage);
record[i].mileage[mileagel - 1] = '\0';
printf("Price: ");
fgets(record[i].price, pricemax, stdin);
pricel = strlen(record[i].price);
record[i].price[pricel - 1] = '\0';
record[i].used = 1;
printf("\nPress q to display records or Enter to enter more data:");
scanf("%c", &quit);
}
}
fwrite(&record, sizeof(struct carrecords), maxrecords, fp); /* Writes the structure to the database file */
fclose(fp); /* Closes the file */
main(); /* Goes back to the main menu */
}
void browserecord(void)
{
if ((fp = fopen("car.data", "r+b"))==NULL)
{
printf("\n\nCannot open file!\n\n");
main();
}
fread(&record, sizeof(struct carrecords), maxrecords, fp);
for(i=0; i<=maxrecords; i++)
{
printf("%s, %s, %s, %s, %s, %s\n", record[i].make, record[i].model, record[i].yearreg, record[i].regmark, record[i].mileage, record[i].price);
}
fclose(fp);
}
int main(void)
{
printf("\nCar Database\n");
printf("===============================\n");
printf("1. Add Records\n");
printf("2. Browse Records\n");
printf("3. Exit Program\n");
printf("\n");
while(1)
{
printf("Please enter an option 1,2 or 3: ");
scanf("%s", &option);
switch(option)
{
case '1':
addrecord();
break;
case '2':
browserecord();
break;
case '3':
exit(0);
break;
default:
printf("\n\nBad Input. Please try again!\n\n");
break;
}
}
}