-
functions
i need help with my code, i want to rearrange my functions so that i pass the structure and file to each function as required throughout program ? im not sure how to do this with structure members and files , ive had a go in my code(attached) but im going round in circles with it , please help me #include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include<ctype.h>
#define FILENAME "MASTER.PNC"
#define STR_SIZE sizeof (struct pnc)
typedef struct pnc{
char reg[8];
int reg_date;
char make[15];
char model[15];
char colour[15];
char owner[30];
char address[55];
int expiry;
}pnc;
int show_menu (void);
void new_record(void);
int create_hash(struct pnc *data );
void delete_record(void);
void amend_record(void);
int main()
{
int menuchoice;
menuchoice = show_menu();
switch(menuchoice)
{
case 1:clrscr();
amend_record(); /*amend details*/
menuchoice = show_menu();
break;
case 2:clrscr();
delete_record(); /*delete details*/
menuchoice = show_menu();
break;
case 3:clrscr();
new_record(); /*new details*/
menuchoice = show_menu();
break;
case 4:clrscr();
printf("\n\n\t\tPROGRAM\tEXITING !");
exit(1);
default:clrscr();
printf("\n\nINVALID CHOICE ENTER AGAIN -> ");
menuchoice = show_menu();
} /*end switch*/
return (0);
} /*end main()*/
int show_menu(void) /*function definition, returns an int for menu choice*/
{
int menuchoice;
clrscr();
printf("\n\n\t\t_______________________________\n" );
printf("\t\t| |\n");
printf("\t\t| MENU |\n");
printf("\t\t| ~~~~ |\n");
printf("\t\t| 1: AMEND VEHICLE DETAILS |\n");
printf("\t\t| 2: DELETE VEHICLE DETAILS |\n");
printf("\t\t| 3: NEW VEHICLE DETAILS |\n");
printf("\t\t| 4: EXIT THE SYSTYEM |\n");
printf("\t\t| |\n");
printf("\t\t|______________________________|\n");
printf("\n\n\t PLEASE ENTER YOUR CHOICE 1-4 -> ");
while(scanf("%d",&menuchoice)<1 || menuchoice >4)
{
printf("\n\tINVALID CHOICE!! ENTER AGAIN -> ");
printf("\n\tCHOOSE FROM OPTIONS ABOVE PLEASE 1-4 ONLY ->");
scanf("%d",&menuchoice);
}
return (menuchoice);
} /*end function show_menu*/
void new_record(void) /*function definitions start here*/
{
FILE *fp;
struct pnc temp;
char ch;
int hash=0;
printf("\n\n\t\tPLEASE ENTER YOUR NEW VEHICLES DETAILS BELOW");
printf("\n\n\tEnter registration number ->");
fflush(stdin);
gets(temp.reg);
check:
if(isalpha(temp.reg[0])&&isdigit(temp.reg[1])&&isdigit(temp.reg[2])&&isdigit(temp.reg[3])&&
isalpha(temp.reg[4])&&isalpha(temp.reg[5])&&isalpha(temp.reg[6]))
create_hash(&temp);
else
{ clrscr();
printf("\n\t\tINCORRECT PLEASE ENTER AGAIN!\n\n\t\t1 LETTER / 3 DIGITS / 3 LETTERS");
printf("\n\n\t\tEnter registration number ->");
fflush(stdin);
gets(temp.reg);
goto check;}
printf("\n\tEnter registration date (DD/MM) -> ");
scanf("%d",&temp.reg_date);fflush(stdin);
printf("\n\tEnter the make of vehicle ->");
gets(temp.make);
printf("\n\tEnter the model of vehicle ->");
gets(temp.model);
printf("\n\tEnter the colour of the vehicle ->");
gets(temp.colour);
printf("\n\tEnter the owners name ->");
gets(temp.owner);
printf("\n\tEnter the owners address ->");
gets(temp.owner);
printf("\n\tEnter the expiry date (DD/MM) ->");
scanf("%d",&temp.expiry);
printf("\n\tIF DETAILS CORRECT PRESS - (Y) TO SAVE RECORD \n\tOR (N) TO EXIT SYSTEM -> ");
ch=toupper(getch());
clrscr();
if(ch=='Y')
{ if((fp=fopen(FILENAME,"rb"))==NULL)
{printf("\n\t\tERROR ON OPENING FILE !\n\t\tPROGRAM WILL EXIT NOW ");
exit(1);}
else
if(fseek(fp,(hash-1)*(STR_SIZE),SEEK_SET)!='\O')
{printf("\n\t\tFILE ALREADY EXISTS ");
exit(1);}
else
fwrite(&temp,(STR_SIZE),1,fp);
}else
{clrscr();
printf("\n\t\tTHANK YOU PROGRAM EXITING NOW ");
exit(1);}
}
int create_hash(struct pnc *data)
{
int hash_index=0, hash=0, loop;
struct pnc temp;
hash += (temp.reg[0] - 'A') * 1000;
for(loop=1;loop<4;loop++)
{
hash_index = temp.reg[loop];
hash += hash_index;
}
return(hash);
}
void delete_record(void)
{
char ch, str[7];
FILE *fp;
int hash = 0;
struct pnc temp;
if((fp=fopen(FILENAME,"rb"))==NULL)
{printf("\n\n\t\tERROR OPENING FILE EXITING ->");
exit(1);}
clrscr(); fflush(stdin);
printf("\n\n\tPLEASE ENTER THE REGISTRATION NUMBER OF THE ");
printf("\n\tVEHICLE -> ");
gets(str);
check:
if(isalpha(str[0])&&isdigit(str[1])&&isdigit(str[2])&&isdigit(str[3])&&
isalpha(str[4])&&isalpha(str[5])&&isalpha(str[6]))
hash += create_hash(&str);
else
{ clrscr();
printf("\n\t\tINCORRECT PLEASE ENTER AGAIN!\n\n\t\t1 LETTER / 3 DIGITS / 3 LETTERS");
printf("\n\n\t\tEnter registration number ->");
fflush(stdin);
gets(str);
goto check;}
if ((fseek(fp,(hash-1)* STR_SIZE,SEEK_SET))!= 0)
{ printf("\n\tDATA ON THIS VEHICLE DOES NOT EXIST");
printf("\n\tRETURNING TO MAIN MENU -> ");
exit(1);}
else
{ printf("\n\tENTER 'Y' TO CONFIRM DELETION ");
printf("\n\tOR 'N' TO EXIT SYSTEM -> ");
ch = toupper(getch());
if(ch =='Y'){
fseek(fp,(-1) * STR_SIZE, SEEK_SET);
temp.reg_date == "9999";
fwrite(&temp.reg, STR_SIZE,1,fp);}
else
exit(1);}
exit(1);
}
void amend_record(void)
{
}
-