Thread: Structures help.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Structures help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct{
    	int dd, mm, yy;
    }DATE;
    
    typedef struct{
    	int idno;
    	char name[40];
    	DATE dob;
    	float salary;
    	int dependents;
    }EMPLOYEE;
    
    
    void main()
    {
    	float temp, newsal=0;
    	int k, percent, id;
    	
    	EMPLOYEE *emp[3];
    
    	for(k=0;k<3;k++){
    		emp[k] = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
    		printf("Enter employee data.\n");
    		puts("I.D Number:");
    		scanf("%d",&emp[k]->idno);
    		puts("Name:");
    		scanf("%s",emp[k]->name[40]);
    		puts("Birthday MM DD YYYY");
    		scanf("%d%d%d",&emp[k]->dob.mm, &emp[k]->dob.dd, &emp[k]->dob.yy);
    		puts("Salary:");
    		scanf("%f",&emp[k]->salary);
    		puts("Dependents:");
    		scanf("%d",&emp[k]->dependents);
    	}
    	puts("Calculating salary raise.");
    	puts("Enter I.D Number of employee: ");
    	scanf("%d",&id);
    	puts("Raise percentage: ");
    	scanf("%f",&percent);
    	for(k=0;k<3;k++){
    		if(emp[k]->idno == id){
    			temp = emp[k]->salary * percent;
    			newsal = emp[k]->salary + temp;
    		}
    	}
    	for(k=0;k<3;k++){
    		printf("%d %40s %d/%d/%d %f %f",emp[k]->idno, emp[k]->name, emp[k]->dob.mm, \
    			emp[k]->dob.dd, emp[k]->dob.yy, emp[k]->salary, newsal);
    	}
    }
    Error every time I try to input a name, could you help me locate what's wrong?

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Well...
    emp[k]->name[40] ---> emp[k]->name
    that'll be ok

    and no void main. check the faq
    and no need to cast malloc. also check the faq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM