Thread: help request, structs, arrays, and command line arguments

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    help request, structs, arrays, and command line arguments

    I'm trying to make a program that gets several lines of data from standard input, where each line represents an employee.
    Each line looks something like this: 081 1986 71 143 029198 Baum Adam
    (ID number, year of birth, height, weight, salary, lastname, firstname).
    Standard input will be a text file with several lines like that, and I have to create a text file where all the employees are organized by ID number ascending. (ID is 1-99 only).

    Hopefully its clear enough, here's what I have so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     typedef struct
            {
                 int id;
                 int birthyear;
                 int height;
                 int weight;
                 int salary;
            } Person;
    int main( int argc, char *argv[] )
    {
      if ( argc != 5 )
      {
        printf("reorder id birthyear height weight salary lastname firstname\n");
        return 0;
      }
      int i;
      Person data[99];
      
      data[1].id =  ;
      data[1].birthyear = ;
      data[1].height = ;
      data[1].weight = ;
      data[1].salary = ;
      
    }
    I still need to figure out how to incorporate the last name and first name since they'll be strings. I also could use some advice on how exactly to assign the elements of the array so that each of the lines in the input is 1 thing in the array, and the place in the array is the same as the ID number.

    My plan overall was/is to have the employee info in the same slot number as their ID, and the rest would be null. then later have an output that goes through the array and ignres the null slots.

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    There are many forum entries on using scanf() and fgets() that can be used to read in your elements per your ordered file inputs. Unless you are concerned with separating the first and last name, I would just read the full name as one long string, but you will have to account for the delimiter that separates them in your list. Your struct then should incorporate a pointer to char to hold the name

    Code:
       ...
       char *  EmpName;
      ...
    example of a scanf():

    [code]
    scanf("%d*%d*%d*%d*%d*%s*%s", ... );
    [\code]

    the use of '...' is me being lazy, but you will need to use your struct members as appropriate for each input.
    Last edited by slingerland3g; 11-25-2009 at 04:03 PM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    okay, so right now its this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     typedef struct
            {
                 int id;
                 int birthyear;
                 int height;
                 int weight;
                 int salary;
                 char* firstname;
                 char* lastname;
                 
            } Person;
    int main( int argc, char *argv[] )
    {
      if ( argc != 7 )
      {
        printf("reorder id birthyear height weight salary lastname firstname\n");
        return 0;
      }
      int i ,temp;
      Person data[99];
      for(i=0; i <99; i++)
      {
      scanf("%d*%d*%d*%d*%d*%s*%s", temp, data[temp].birthyear , data[temp].height , data[temp].weight , data[temp].salary , data[temp].lastname , data[temp].firstname);
      data[temp].id = temp;
      }
    }
    how can I get it so that it actually reads from the text file, right now it says there aren't 7 arguments.
    Last edited by DLH112; 11-25-2009 at 04:34 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He's confusing * with &. If you are scanning, you need an address, which is &.
    Code:
    scanf( "%d", &mystruct.myitem );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Reading from standard input is different from reading command line args. You should decide which you want.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    I had a feeling I couldn't do that, then standard input...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     typedef struct
            {
                 int id;
                 int birthyear;
                 int height;
                 int weight;
                 int salary;
                 char* firstname;
                 char* lastname;
                 
            } Person;
    int main( )
    {
      int i ,temp;
      Person data[99];
      for(i=0; i <99; i++)
      {
      scanf("%d %d %d %d %d %s %s", temp, &data[temp].birthyear , &data[temp].height , &data[temp].weight , &data[temp].salary , &data[temp].lastname , &data[temp].firstname);
      data[temp].id = temp;
      }
    }
    now it just crashes, and also, the loop needs to change somehow, because it will attempt to scan blank rows. I'm not sure how to fix it though.
    Last edited by DLH112; 11-25-2009 at 05:06 PM.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Still doesn't work, but maybe with this it's clearer what I'm trying to accomplish:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     typedef struct
            {
                 int id;
                 int birthyear;
                 int height;
                 int weight;
                 int salary;
                 char* firstname;
                 char* lastname;
                 
            } Person;
    int main( )
    {
      int i ,temp;
      Person data[99];
      while(getchar() != '\n')
      {
      scanf("%d %d %d %d %s %s", &data[temp].birthyear , &data[temp].height , &data[temp].weight , &data[temp].salary , &data[temp].lastname , &data[temp].firstname);
      data[temp].id = temp;
      }
      for(i=0; i<99;i++)
       {
       if(data[i].firstname != NULL)
       printf("%d %d %d %d %s %s",data[i].birthyear ,data[i].height ,data[i].weight ,data[i].salary ,data[i].lastname ,data[i].firstname);
      }
    }
    the while loop should be something like while((temp = getchar()) != NULL) but that wouldn't compile, since its a comparison between a pointer and an int

    Edit: Stupid realization: it should be EOF, I blanked out before on what it should be, woops. the program compiles now but it still crashes on running.
    Last edited by DLH112; 11-25-2009 at 07:59 PM.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    That's because temp is a local variable and will contain garbage unless explicitly initialized.
    So why isn't scanf() part of the for loop with array data[] indexed by i instead of temp.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Because I need to put the data into the same slot of the array as the ID number(i.e. employee with ID # 56 goes in data[56]). The way you stated what to do makes it sound like that would be impossible that way.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     typedef struct
            {
                 int id;
                 int birthyear;
                 int height;
                 int weight;
                 int salary;
                 char* firstname;
                 char* lastname;
                 
            } Person;
    int main( )
    {
      int i ,temp;
      Person data[99];
      while((temp = getchar()) != EOF)
      {
      scanf("%d %d %d %d %s %s", &data[temp].birthyear , &data[temp].height , &data[temp].weight , &data[temp].salary , &data[temp].lastname , &data[temp].firstname);
      data[temp].id = temp;
      }
      for(i=0; i<99;i++)
       {
       if(data[i].firstname != NULL)
       printf("%d %d %d %d %s %s",data[i].birthyear ,data[i].height ,data[i].weight ,data[i].salary ,data[i].lastname ,data[i].firstname);
      }
    }
    Last edited by DLH112; 11-25-2009 at 10:13 PM.

Popular pages Recent additions subscribe to a feed