2017-05-28 101 views
1

因此,與一流的工作進展,學習CC - 參數名稱省略?

我決定progressivly把函數按我的簡短從我的課堂作業,如下所示的背景下,試圖通過一塊排查一段代碼:

Structure Chart

Functions Brief

僞碼香港專業教育學院被告知要遵循:

use #define SIZE 3 
function : main 
----------------------- 
Local variables: 
- emp_array (an array of 3 employee detail values) 
- i (an integer used as the index for the arrays) 
- char str[20] to read in name of employee for search 
----------------------- 
1: call read_all_employee, passing in emp_array and SIZE 
2: Print the message ‘Employee details are’ 
3: call print_all_employee, passing in emp_array and SIZE 
4: Print 'Total : ', employee_total_salary (emp_array, SIZE) 
5: Print the message '—Employee with the largest salary is --' 
6: Store in i, the search_largest_salary_index passing in emp_array and SIZE 
7: Call print_employee, passing in emp_array at index i 
8: Print the message '— Enter employee name for the search--' 
9: read in the name in str array 
10: Store in i, the search_an_employee_salary passing in emp_array, SIZE and str 
11: if something was found 
12: Print the message 'The salary of xxxx is xxxx’ 
13: else 
14: Print the message "Array does not contain an employee named xxxx" 
15: Print the message '—Employee details in reverse order are --' 
16: Loop i starting from 2 to 0 for each index of emp_array 
17: Call print_employee, passing in emp_array at index i 

但是編譯程序,我把整個誤差對尺寸爲#define size 3後聲明的每個函數「省略的參數名稱」來插入,和「)」的名字預期

這是我到目前爲止寫的代碼:

#include <stdio.h> 
#define size 3 

struct employee{ 
    char name[20]; 
    int emp_id; 
    float salary; 
}; 

struct employee read_employee(){ 
    struct employee r_employee; 
    printf("Enter Employee Name: "); 
    scanf("%s", &r_employee.name); 
    printf("Enter ID: "); 
    scanf("%d", &r_employee.emp_id); 
    printf("Enter Salary: "); 
    scanf("%f", &r_employee.salary); 

    while (r_employee.salary < 0){ 
     printf("This is not a valid price, enter again\n"); 
     scanf("%f", &r_employee.salary); 
     } 
    return r_employee; 
} 

struct employee read_all_employee(struct employee emp_array[], int size){ 
    for (int i = 0; i < size; ++i) 
    { 
     emp_array[i] = read_employee(); 
    } 
} 

void print_employee(struct employee employee_data){ 
    printf("%s(%d): %f\n", employee_data.name, employee_data.emp_id, employee_data.salary); 
    if (employee_data.salary > 5000) 
    { 
     printf("Level A\n"); 
    } 
    if (employee_data.salary < 4000) 
    { 
     printf("Level B\n"); 
    } 
} 

float employee_total_salary(struct employee emp_array[], int size){ 
    int i; 
    float sum = 0; 
    for (int i = 0; i < size; i++) 
    { 
     sum = sum + employee_array[i].salary; 
    } 
    return sum; 
} 

int employee_index_search(struct employee emp_array[], int id, int size){ 
    int i; 
    for (int i = 0; i < size; i++) 
    { 
     if (employee_array[i].emp_id == id) 
     { 
      return i; 
     } 
    } 
    return -1; 
} 

int main(){ 
    struct employee emp_array[3]; 
    int i; 
    char str[20]; 

    printf("Line 1:\n"); 
    read_all_employee(emp_array, size); 
    printf("Employee Details are:\n"); 
return 0; 
} 

有人可以請更正我的代碼到目前爲止?

回答

2

參數名稱略

手段,編譯器不明白一個參數名稱,其中,預計...

這是因爲你不能使用define和參數名稱與同名。當寫#define size 3預處理器替換它看到的代碼3每個size然後,當你調用與參數size的功能,而不是struct employee read_all_employee(..., int size),你struct employee read_all_employee(..., int 3),導致int類型的參數沒有有效的名稱( '3'不是有效的名稱)

我會建議使用define與CAPS,或者用一些獨特的名字,這樣你就不會感到困惑,像SIZE或者只是記住,你有size符號並使用你的函數參數的其他名稱,如input_size