Thread: structure array

  1. #1
    Unregistered
    Guest

    Question structure array

    I'm trying to get the info and put it into a structure and the structure is an array. I don't have any experience in this. Any help would be greatly appreciated!! The error I get is: error C2664: 'display' : cannot convert parameter 1 from 'struct employee' to 'struct employee []'. It is pointing to case 2 in the main function.


    code:--------------------------------------------------------------------------------
    struct employee {
    char name[30];
    char ssn[12];
    float rate;
    float hours;
    float gross;
    float tax;
    float net;
    };
    #include<stdio.h>
    int menu();
    struct employee info(void);
    void display(struct employee emp[10]);
    int main(void)
    {
    int sel;
    struct employee emp[10];
    while ((sel = menu()) != -1)
    {
    switch (sel)
    {
    case 0: return 0; break;
    case 1: emp[10] = info(); break;
    case 2: display(emp[10]); break;
    default: puts("\a\nInvalid Selection\n");break;
    }
    }
    return 0;
    }
    struct employee info(void)
    {
    int num;
    struct employee emp[10];
    for(num = 0; (num < 10); num++){
    printf("Enter employee's name (no spaces): ");
    scanf("%s", &emp[num].name);
    printf("Enter employee's SSN (999-99-9999): ");
    scanf("%s", &emp[num].ssn);
    printf("Enter employee's payrate: ");
    scanf("%f", &emp[num].rate);
    printf("Enter employee's hours worked: ");
    scanf("%f", &emp[num].hours);
    emp[num].gross = emp[num].hours * emp[num].rate;
    emp[num].tax = emp[num].gross * 0.21;
    emp[num].net = emp[num].gross * 0.79;
    return emp[num];
    }
    }
    int menu()
    {
    int sel;
    puts("\nMenu\nPress 1 to input data\nPress 2 to display data\nPress 0 to Quit");
    scanf("%d", &sel);
    return sel;
    }

    void display(struct employee emp[10])
    {

    int choice;
    puts("Name SSN Gross Pay Taxes Net");
    for ( choice = 0 ; (choice < 10) ; choice++ ) {
    printf( "%s %s %f %f %f", emp[choice].name, emp[choice].ssn, emp[choice].gross, emp[choice].tax, emp[choice].net);
    }

    }
    --------------------------------------------------------------------------------

  2. #2
    *
    Guest
    Your structure is _not_ an array. It is a nearly an actual 'type' of variable. It needs to be correctly declared, and then you can actually create an array of its type--

    Change your declaration to this correction:

    Code:
    typedef struct
       { 
       char name[30]; 
       char ssn[12]; 
       float rate; 
       float hours; 
       float gross; 
       float tax; 
       float net; 
       }employee;
    And create your array:

    Code:
    employee emp[10];
    And I noticed you have mutliple declarations of your 'struct employee xxxx' statement-- this is not C++, this is not correct. You only need the one at the top.

    You need to learn how to pass the array by address, or it's elements by address so you can access them in each function.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Two problems
    1. your input function was caught between inputting a single employee, and inputting all of them. I made it input all
    2. your passing array parameters was a little off

    Code:
    #include<stdio.h> 
    
    #define NUM_EMP 10
    
    struct employee { 
        char name[30]; 
        char ssn[12]; 
        float rate; 
        float hours; 
        float gross; 
        float tax; 
        float net; 
    }; 
    
    int menu(); 
    void info(struct employee emp[NUM_EMP]); 
    void display(struct employee emp[NUM_EMP]); 
    
    int main(void) 
    { 
        int sel; 
        struct employee emp[NUM_EMP]; 
        while ((sel = menu()) != -1) 
        { 
            switch (sel) 
            {
            case 0: return 0; break;
            case 1: info(emp); break;       // pass the whole array
            case 2: display(emp); break;    // pass the whole array
            default: puts("\a\nInvalid Selection\n");break; 
            } 
        } 
        return 0; 
    } 
    
    // instead of trying to return just one employee, it now
    // returns all of them
    // your for loop suggested that it might
    void info(struct employee emp[NUM_EMP]) 
    { 
        int num; 
    //    use the parameter array instead
    //    struct employee emp[10]; 
        for(num = 0; (num < NUM_EMP); num++){ 
            printf("Enter employee's name (no spaces): "); 
            scanf("%s", &emp[num].name); 
            printf("Enter employee's SSN (999-99-9999): "); 
            scanf("%s", &emp[num].ssn); 
            printf("Enter employee's payrate: "); 
            scanf("%f", &emp[num].rate); 
            printf("Enter employee's hours worked: "); 
            scanf("%f", &emp[num].hours); 
            emp[num].gross = emp[num].hours * emp[num].rate; 
            emp[num].tax = emp[num].gross * 0.21; 
            emp[num].net = emp[num].gross * 0.79; 
    // very bad, you're returning inside your for loop
    //        return emp[num]; 
        } 
    } 
    
    int menu() 
    { 
        int sel; 
        puts("\nMenu\n"
             "Press 1 to input data\n"
             "Press 2 to display data\n"
             "Press 0 to Quit"); 
        scanf("%d", &sel); 
        return sel; 
    } 
    
    void display(struct employee emp[NUM_EMP]) 
    { 
        int choice; 
        puts("Name SSN Gross Pay Taxes Net"); 
        for ( choice = 0 ; (choice < NUM_EMP) ; choice++ ) { 
            printf( "%s %s %f %f %f\n",
                emp[choice].name, emp[choice].ssn,
                emp[choice].gross, emp[choice].tax, emp[choice].net); 
        } 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM