Thread: Passing and using a Array nested Structure

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Passing and using a Array nested Structure

    This project requires me to take an input file and scan its information into an nested array structure. So far I have finally been able to fill the nested structure after converting the file's strings to the appropriate types. Now however, I don't understand how to pass the nested structure, STUDENT for use in other functions. Placing the structure in the header as a global declaration is out of the question for this project. Any help is appreciated.

    Code:
       This program requires three command arguments: The text file name, 
       the number of entries to be scanned from the input file, and the input 
       file name.  The program will scan the input file's entries into a 
       structured array and sort the arrays.  It will also print the unsorted and
       sorted arrays.  Lasltly, it will allow the user to run queries for certain
       entries using the SSN.  
    *****************************************************************************/
    
    /* Preprocessing Directives */
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #define LEN 30
    
    void displayName ();
    bool openFile (char *argv[], FILE* *pfpIn);
    bool readFile (char *argv[], FILE* afpIn, STUDENT *info);
    void displayFile ();
    void sortName ();
    void displaySortName ();
    void sortSSN ();
    void displaySortSSN ();
    void binSearch ();
    
    // Function main calls other functions and returns control back to OS.
    int main (int argc, char *argv[])
    {
       FILE* afpIn;
       int quant = atoi (argv[1]);
    
       displayName ();
      
       if (argc == 3 && quant > 1 && quant < 10)
          {
     printf("\nNumber of command arguments: %d", argc);
     printf("\nNumber of strings to scan from file: %s", argv[1]);
     printf("\nName of input file: %s", argv[2]);
          if ( openFile (argv, &afpIn) && readFile(argv, afpIn, STUDENT *info) )
             {
             displayFile();
             }
          }
       else
          ("\nPlease try again later\n");
    
       return 0;
    }
    
    // Function displayName displays the Programmer's name.
    void displayName()
    {
       printf("\nThomas Ciocco\n");
    
    }
    
    // Function openFile opens the input file
    bool openFile (char *argv[], FILE* *pfpIn)
    {
       bool openFileSuccess = false;
    
       if ((*pfpIn = fopen (argv[2], "r")) != NULL)
          {
          openFileSuccess = true;
          printf ("\nFile open success\n\n");
          }
       else 
          printf("\nFile did not open successfully\n");
    
       return openFileSuccess;
    }
    
    // Function readFile scans the input file data into a structured array
    bool readFile (char *argv[], FILE* afpIn, STUDENT *info)
    {
       typedef struct
       {
       int month;
       int day;
       int year;
       } BDATE;
    
       typedef struct
       {
       char firstName[LEN];
       char lastName[LEN] ;
       int grade;
       char SSN [10];
       BDATE bDate;
       } STUDENT;
       
       bool readFileSuccess = true;
       int counter;
       int quant = atoi (argv[1]);
       STUDENT info[quant];
    
       char* tempMonth[quant][LEN];
       char* tempDay[quant][LEN];
       char* tempYear[quant][LEN];
      
       for (counter = 0; counter < quant ; counter++)     
          {
          fscanf(afpIn, "%s %s %d %s %s %s %s", 
                 &info[counter].firstName, &info[counter].lastName, 
                 &info[counter].grade, &info[counter].SSN, 
                 &tempMonth[counter], &tempDay[counter], &tempYear[counter]);
         printf("%s %s %d %s %s %s %s\n",
                 info[counter].firstName, info[counter].lastName, 
                 info[counter].grade, info[counter].SSN,
                 tempMonth[counter], tempDay[counter], tempYear[counter]);
          }   
       
       // Converts file month strings to ints and fills bDate.month
       for (counter = 0; counter < quant; counter++)
          {
          if(!strcmp(tempMonth[counter],"January"))
             {
             info[counter].bDate.month = 1;
             }
          if(!strcmp(tempMonth[counter],"February"))
             {
             info[counter].bDate.month = 2;
             }
          if(!strcmp(tempMonth[counter],"March"))
             {
             info[counter].bDate.month = 3;
             }
          if(!strcmp(tempMonth[counter],"April"))
             {
             info[counter].bDate.month = 4;
             }
          if(!strcmp(tempMonth[counter],"May"))
             {
             info[counter].bDate.month = 5;
             }
          if(!strcmp(tempMonth[counter],"June"))
             {
             info[counter].bDate.month = 6;
             }  
          if(!strcmp(tempMonth[counter],"July"))
             {
             info[counter].bDate.month = 7;
             } 
          if(!strcmp(tempMonth[counter],"August"))
             {
             info[counter].bDate.month = 8;
             }
          if(!strcmp(tempMonth[counter],"September"))
             {
             info[counter].bDate.month = 9;
             }
          if(!strcmp(tempMonth[counter],"October"))
             {
             info[counter].bDate.month = 10;
             }
          if(!strcmp(tempMonth[counter],"November"))
             {
             info[counter].bDate.month = 11;      
             }
          if(!strcmp(tempMonth[counter],"December"))
             {
             info[counter].bDate.month = 12;
             }          
    
          } 
       
       // Converts string days and months to ints and fills bDate.day and year
       for (counter = 0; counter < quant; counter++)
          {
          info[counter].bDate.day = atoi(tempDay[counter]);
          info[counter].bDate.year = atoi(tempYear[counter]);
          printf("%d %d, %d\n", info[counter].bDate.month ,
                 info[counter].bDate.day, info[counter].bDate.year);
          }
    
       return readFileSuccess;
    }
    
    // Function displayFile prints the file data
    void displayFile ( )
    {
    
    }
    
    // Function sortName sorts the array by Name data
    void sortName ( )
    {
    
    }
    
    // Function displaySortName prints the array sorted by name 
    void display (sortName)
    {
    
    }
    
    // Function sortSSN sorts the array by the SSN
    void sortSSN ( )
    {
    
    }
    
    // Function displaySortSSN prints the array sorted by SSN
    void displaySortSSN ( )
    {
    
    }
    
    // Function binSearch allows user to search database for entry via SSN
    void binSearch ( )
    {
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You won't be able to if the structs are defined inside another function. Just move the struct definitions to the top of the file after the #include directives.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Figured it out. Thanks
    Last edited by Jaxtomtom89; 11-30-2010 at 04:21 AM.

Popular pages Recent additions subscribe to a feed