Thread: Help please?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Help please?

    this is what i came up with so far, creating a struct and array to enter and store data,but i'm having trouble calculating the total salary expenses for ALL the employees that were entered. I dunno what to do, can someone please help me with this?




    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct
    {
         char firstname[10];
         char lastname[20];
         char jobtitle[20];	
         int empyrs;
         double salary;
    } EMPLOYEE;
    
    void print_employees (EMPLOYEE values[], int size);
    
    int main()
    {
    	int number_of_employees = 0;
    
    	printf("How many employees will you be storing?\n");
    	scanf("%d", &number_of_employees);
    
    	EMPLOYEE employees[number_of_employees];
    
    	int i;
    	for (i = 0; i < number_of_employees; i++)
    	{
    		printf("Enter employees first name (%d): \n", i + 1);
    		scanf("%s", employees[i].firstname);
    
    		printf("Enter employees last name: \n");
    		scanf("%s", employees[i].lastname);
    		
    		printf("Enter employees jobtitle: \n");
    		scanf("%s", employees[i].jobtitle);
    		
    		printf("Enter employees salary: \n");
    		scanf("%lf", &employees[i].salary);
    		
    		printf("Enter employed years: \n");
    		scanf("%d", &employees[i].empyrs);
    	}
    
    	print_employees(employees, number_of_employees);
    	
    }
    
    void print_employees(EMPLOYEE values[], int size)
    {
    	int i;
    
    	printf("Number of employees recorded: %d \n\n");
    	for (i = 0; i < size; i++)
    	{
    		printf("-------------------------------\n");
    		printf("Employees first name is \'%s\' \n", values[i].firstname);
    		printf("Employees last name is \'%s\' \n", values[i].lastname);
    		printf("Employees jobtitle is \'%s\' \n", values[i].jobtitle);
    		printf("Employees salary is $%.2lf \n", values[i].salary);
    		printf("Employed years %d \n\n", values[i].empyrs);
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A simple for loop is all you need:

    Code:
    for(i = 0, totalSalary = 0; i < number_of_employees; i++)
      totalSalary += EMPLOYEE[i].salary;

Popular pages Recent additions subscribe to a feed

Tags for this Thread