structure array

This is a discussion on structure array within the C Programming forums, part of the General Programming Boards category; I'm trying to get the info and put it into a structure and the structure is an array. I don't ...

  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
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >display(emp[10]);

    Here you pass the 10th struct of emp to the function display. But the function display expects an array of structs instead of a struct.
    Also note that there is no struct with index 10, indices start with 0. So the last index is 9.

    The for-loop in function info is also wrong, I guess you want to fill 10 structs and send the array back. In your function, the for-loop is immediately exited because of the return. You could send back the address of the array.

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, 03:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 08: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, 01:04 PM

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