'lo people,
I have this code, it's a work in progress, that's why bits are missing..
Code:
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_VEH 26000
#define C_SCANF while(getchar() != '\n');
#define FILENAME "VEHMAIT.DAT"
#define STR_SIZE sizeof(struct VEH)
struct VEH{
char regno[8];
char make[21];
char model[21];
char chassis[21];
struct INS_DATE{
int day;
int month;
int year;
}Ins_date;
struct NEXT_DATE{
int day;
int month;
int year;
}Next_date;
int Ins_freq;
int brake_freq;
} veh[MAX_VEH];
/*Define Functions*/
void C_fgets(char *s_ptr); /* Removes the '/n' from the end of fgets() input*/
void Add_veh(FILE *vehicle);
void Amd_veh(FILE *vehicle);
void Del_veh(FILE *vehicle);
void Shw_veh(FILE *vehicle, int pos);
void Shw_sch(FILE *vehicle);
void Calc_date();
int Calc_pos (char regno[]);
int main (void)
{
FILE *vehicle;
if ( (vehicle=fopen(FILENAME,"rb+"))== NULL)
{
if ( (vehicle=fopen(FILENAME,"wb"))== NULL)
{
printf("\nUnable to create key file");
return 0;
}
}
int option = 0;
while(option != 5)
{
clrscr();
printf("\n\n\t\t\t Vehicle maintenance program\n");
printf("\n\t\t\t 1. Add a vehicle to schedule");
printf("\n\t\t\t 2. Amend a vehicles details");
printf("\n\t\t\t 3. Delete a vehicle");
printf("\n\t\t\t 4. Show current schedule");
printf("\n\t\t\t 5. Save changes and exit program");
printf("\n\t\t\t >> ");
scanf("%d", &option);
C_SCANF;
switch(option)
{
case 1: Add_veh(vehicle);
break;
case 2: Amd_veh(vehicle);
break;
case 3: Del_veh(vehicle);
break;
case 4: Shw_sch(vehicle);
break;
case 5: printf("\n\t\t\tExiting program");
getch();
fclose(vehicle);
return 1;
default: printf("No such option implemented");
}
}
}
/* Define functions*/
void C_fgets(char *s_ptr)
{
char *p = strchr(s_ptr, '\n' );
if ( p != NULL ) *p = '\0';
}
void Add_veh(FILE *vehicle)
{
char regno[8];
int pos;
clrscr();
printf("\n\n\t\t\t ADD A VEHICLE\n\n");
printf("\n VEHICLE DETAILS\n");
printf("\n Enter vehicle registration number: ");
fgets(regno,9,stdin);
pos = Calc_pos(regno);
strcpy(veh[pos].regno,regno);
printf("\n Enter vehicle make : ");
fgets(veh[pos].make,21,stdin);
printf("\n Enter vehicle model: ");
fgets(veh[pos].model,21,stdin);
printf("\n Enter vehicle chassis number: ");
fgets(veh[pos].chassis,21,stdin);
printf("\n\n INSPECTION DETAILS\n");
printf("Enter date of first inspection: ");
/*Date*/
printf("Enter frequency of inspections: ");
scanf("%d", veh[pos].Ins_freq);
C_SCANF;
printf("Enter brake check frequency: ");
scanf("%d", veh[pos].brake_freq);
C_SCANF;
}
void Amd_veh(FILE *vehicle)
{}
void Del_veh(FILE *vehicle)
{}
void Shw_veh(FILE *vehicle, int pos)
{}
void Shw_sch(FILE *vehicle)
{}
void Calc_date(FILE *vehicle)
{}
int Calc_pos(char regno[])
{
int pos;
pos = regno[0] - 'A' *1000+
regno[1] - '0' *100+
regno[2] - '0' *10+
regno[3] - '0' *1;
return pos;
}
Thing is, when I run it, after typing in the reg the program crashes, with an access violation.
Any help, random abuse, kind words, latin quotations... all are welcome...