functions

This is a discussion on functions within the C Programming forums, part of the General Programming Boards category; i need help with my code, i want to rearrange my functions so that i pass the structure and file ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    2

    Question 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)
    {

    }




  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    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 ,
    A nice way to organize the program would be to define an instance of the structure and file pointer in 'main()', than pass them into calling functions. After quickly looking through your code, I can see that you have it mostly right, so I'm not sure if this will help. If not than I'll try to answer it again.

    You structure declaration is in the right place. You can create a structure array like so (but I'd get rid of the label and specify that it is a 'type'):
    Code:
    //header files go here
    typedef struct { 
      char reg[8]; 
      int reg_date; 
      char make[15]; 
      char model[15]; 
      char colour[15]; 
      char owner[30]; 
      char address[55]; 
      int expiry; 
    }Pnc_t; 
    //function prototypes go here (example)
    FILE * OpenTheFile(FILE *, char []);
    
    int main()
    {
      Pnc_t Srecords[10]; //structure array
      FILE *Fptr = NULL; //file pointer
      ...
      Fptr = OpenTheFile(Fptr, Cfilename);
      ...
      return 0;
    }
    Now on the other hand, you need a label in your structure if you want to create a 'self referential structure'. This is a structure that you can use to build a linked lists.
    Code:
    //header files go here
    typedef struct node_s { 
      char reg[8]; 
      int reg_date; 
      char make[15]; 
      char model[15]; 
      char colour[15]; 
      char owner[30]; 
      char address[55]; 
      int expiry;
      struct node_s *linkp; 
    }Pnc_t; 
    //function prototypes go here (example)
    Pnc_t * BuildList(Pnc_t *, FILE *);
    
    int main()
    {
      Pnc_t *Slist = NULL;
      FILE *Fptr = NULL;
      ...
      //call function to open the file here
      Slist = BuildList(Slist,Fptr);
      ...
      return 0;
    }
    Last edited by Witch_King; 08-18-2001 at 09:28 AM.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    2

    functions

    Thanks for looking at my code , wot im trying to do is create funtions to write to FILE + create_hash, + delete records etc, wot i want is to call the functons as required in code and then use the structure member as function arg. and FILE if needed, im not sure how to change my code to achieve this . As i think this will improve my code e.g. i dont need to open the file for each function , just the one function would be used to open file and either delete the record or write to file the record, im a bit confused about how to use structure members within functions as args and how to call the funcs. with these, im sorry to sound thick but ive been going over my code so long now that im not seeing things clearly anymore. need a bit of guidance

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    After looking at this piece of code, I'm wondering why you didn't use the pointer to the structure that you passed into the function. You defined an instance of the structure called temp, however it is unilitialized. Unlike C++ there is no constructor.
    If you want to access the data in the 'parameter', you would have to use this syntax: 'data->reg[0]'. Or else to print the whole string for example: 'printf("%s",data->reg);. The arrow is necessary because a pointer to the structure was passed into the function.

    Code:
    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); 
    }
    struct pnc temp contains garbage. It is uninitialized. Indeed the syntax to access members of temp is correct, for exampe: 'temp.reg[0]' will reveal whatever garbage character is stored in the uninitialized instance of the structure, but why would you want to do that?
    Last edited by Witch_King; 08-18-2001 at 06:46 PM.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21