2013-03-04 225 views
1

我想爲結構employeeData(小時)的每個數組填充第三個參數。通過函數傳遞結構

我想通過詢問「請爲員工x輸入小時數」來提示手動輸入小時段,如下所示。它似乎並沒有工作。

void Get_input (long id_number[], float hours[], int num_empl) 
    { 
    /*Local Variable Declaration */ 

int i; /* Variable used in loop counter */ 

    /* Gets number of employee hours and stores them in an array. */ 

for (i = 0; i < num_empl; ++i) 
    { 
    printf("\nEnter the numbers of hours worked by employee # %06li: ", 
      employeeData[i].id_number); 
    scanf ("%f", &employeeData[i].hours); 
    } 

printf("\n\n"); 
} 

這裏是我的全部「計劃」至今:

/*Define and Includes */ 

#include <stdio.h> 
#include <cstdlib> 

/* Define Constants */ 
#define NUM_EMPL 5 
#define OVERTIME_RATE 1.5f 
#define STD_WORK_WEEK 40f 

/* Define a global structure to pass employee data between functions */ 

struct employee 
{ 
long id_number; 
float wage; 
float hours; 
float overtime; 
float gross; 
}; 

/* define prototypes here for each function except main */ 

/************************************************************************/ 
/*      Function: Get_input        */ 
/*                  */ 
/* Purpose: Obtains input from user, the number of hours worked per */ 
/*    employee and stores the results in an array that is  */ 
/*    passed back to the calling program by reference.  */ 
/*                  */ 
/* Parameters: clockNum- Array of employee clock numbers.    */ 
/*    hours- Array of number of hours worked by an employee */ 
/*    size- Number of employees to process     */ 
/*                  */ 
/* Returns: Nothing, since 'clockNum' & 'hours' arrays are passed by */ 
/*    reference.            */ 
/************************************************************************/ 

void Get_input (long id_number[], float hours[], int num_empl) 
{ 
    /*Local Variable Declaration */ 

int i; /* Variable used in loop counter */ 

    /* Gets number of employee hours and stores them in an array. */ 

for (i = 0; i < num_empl; ++i) 
    { 
    printf("\nEnter the numbers of hours worked by employee # %06li: ", 
      employeeData[i].id_number); 
    scanf ("%f", &employeeData[i].hours); 
    } 

    printf("\n\n"); 
    } 

    void Output_results_screen (struct employee [ ], int num_empl); 


    /************************************************************************* 
    **      Function: Output_results_screen     
    **                  
    ** Purpose: Outputs to screen in a table format the following   
    **      information about an employee: Clock, Wage,  
    **      Hours, Overtime, and Gross Pay.     
    **                  
    ** Parameters: employeeData - an array of structures containing   
    **        employee information      
    **    size - number of employees to process     
    **                  
    ** Returns: Nothing (void)           
    **                  
    *************************************************************************/ 

    void Output_results_screen (struct employee employeeData[], int num_empl) 
    { 
    int i; /* loop index */ 

     printf ("-----------------------------------------\n"); /*Print Header To Screen */ 
     printf ("Clock# \t Wage \t Hours \t OT \t Gross\n"); 
     printf ("-----------------------------------------\n"); 

    /* printf information about each employee */ 
    for (i = 0; i < num_empl ; ++i) 
    { 


     printf("%06li %5.2f %4.1f %4.1f %8.2f \n", 
        employeeData[i].id_number, employeeData[i].wage, employeeData[i].hours, 
        employeeData[i].overtime, employeeData[i].gross); 
    } /* for */ 

    } /* Output_results_screen */ 


    int main() 
    { 


    /* Variable Declaration and initialization */ 
    struct employee employeeData[NUM_EMPL] = { 
    { 98401, 10.60 }, 
    { 526488, 9.75 }, 
    { 765349, 10.50 }, 
    { 34645, 12.25 }, 
    { 127615, 8.35 } 
    }; 


    /* Call various functions needed to reading, calculating, and printing as needed */ 

     /* Function call to get input from user. */ 
Get_input (id_number, hours, NUM_EMPL); 

    /* Function call to output results to the screen in table format. */ 
    Output_results_screen (employeeData, NUM_EMPL); 
    system("pause"); 
    return(0); /* success */ 

    } /* main */ 
+0

確切的錯誤是什麼?我似乎沒有看到代碼 – TravellingGeek 2013-03-04 03:01:54

+0

中的任何錯誤,第一個錯誤是66'employeeData'未聲明(首先使用此函數),它在第66行,它說「employeeData [i] .id_number」 – Americo 2013-03-04 03:14:15

+0

我同意@ LaceySnr的答案在下面,你需要傳遞employeeData到你的Get_Input,就像你在Output_results_screen中做的那樣,因爲它在本函數中是主函數 – TravellingGeek 2013-03-04 03:17:48

回答

2

employeeData是本地的main,不能看到裏面GetInput()所以我假設你得到一個編譯器錯誤。

將該數組放在所有函數之外(即使其成爲全局函數)並且它應該可以工作。或者將一個指向數組的指針傳遞給GetInput,這樣就可以讀入適當的記錄。

+0

問題解決了!就像@GeraldSV所說的那樣,這裏的方法是將指針傳遞給數組GetInput。一旦我通過整個結構,功能就可以識別參數。非常感謝! – Americo 2013-03-04 03:48:49